Merge branch 'gles'
[neverball] / putt / st_conf.c
1 /*
2  * Copyright (C) 2003 Robert Kooima
3  *
4  * NEVERPUTT is  free software; you can redistribute  it and/or modify
5  * it under the  terms of the GNU General  Public License as published
6  * by the Free  Software Foundation; either version 2  of the License,
7  * or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
11  * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
12  * General Public License for more details.
13  */
14
15 #include "gui.h"
16 #include "hud.h"
17 #include "geom.h"
18 #include "ball.h"
19 #include "part.h"
20 #include "game.h"
21 #include "audio.h"
22 #include "config.h"
23 #include "video.h"
24
25 #include "st_conf.h"
26 #include "st_all.h"
27 #include "st_resol.h"
28
29 /*---------------------------------------------------------------------------*/
30
31 enum
32 {
33     CONF_FULL = 1,
34     CONF_WIN,
35     CONF_TEXHI,
36     CONF_TEXLO,
37     CONF_SHDON,
38     CONF_SHDOF,
39     CONF_BACK,
40     CONF_RESOL
41 };
42
43 static int music_id[11];
44 static int sound_id[11];
45
46 static int conf_action(int i)
47 {
48     int w = config_get_d(CONFIG_WIDTH);
49     int h = config_get_d(CONFIG_HEIGHT);
50     int s = config_get_d(CONFIG_SOUND_VOLUME);
51     int m = config_get_d(CONFIG_MUSIC_VOLUME);
52     int r = 1;
53
54     audio_play(AUD_MENU, 1.0f);
55
56     switch (i)
57     {
58     case CONF_FULL:
59         goto_state(&st_null);
60         r = video_mode(1, w, h);
61         goto_state(&st_conf);
62         break;
63
64     case CONF_WIN:
65         goto_state(&st_null);
66         r = video_mode(0, w, h);
67         goto_state(&st_conf);
68         break;
69
70     case CONF_TEXHI:
71         goto_state(&st_null);
72         config_set_d(CONFIG_TEXTURES, 1);
73         goto_state(&st_conf);
74         break;
75
76     case CONF_TEXLO:
77         goto_state(&st_null);
78         config_set_d(CONFIG_TEXTURES, 2);
79         goto_state(&st_conf);
80         break;
81
82     case CONF_SHDON:
83         goto_state(&st_null);
84         config_set_d(CONFIG_SHADOW, 1);
85         goto_state(&st_conf);
86         break;
87
88     case CONF_SHDOF:
89         goto_state(&st_null);
90         config_set_d(CONFIG_SHADOW, 0);
91         goto_state(&st_conf);
92         break;
93
94     case CONF_BACK:
95         goto_state(&st_title);
96         break;
97
98     case CONF_RESOL:
99         goto_state(&st_resol);
100         break;
101
102     default:
103         if (100 <= i && i <= 110)
104         {
105             int n = i - 100;
106
107             config_set_d(CONFIG_SOUND_VOLUME, n);
108             audio_volume(n, m);
109             audio_play(AUD_BUMP, 1.f);
110
111             gui_toggle(sound_id[n]);
112             gui_toggle(sound_id[s]);
113         }
114         if (200 <= i && i <= 210)
115         {
116             int n = i - 200;
117
118             config_set_d(CONFIG_MUSIC_VOLUME, n);
119             audio_volume(s, n);
120             audio_play(AUD_BUMP, 1.f);
121
122             gui_toggle(music_id[n]);
123             gui_toggle(music_id[m]);
124         }
125     }
126
127     return r;
128 }
129
130 static int conf_enter(struct state *st, struct state *prev)
131 {
132     int id, jd, kd;
133
134     back_init("back/gui.png");
135
136     /* Initialize the configuration GUI. */
137
138     if ((id = gui_vstack(0)))
139     {
140         int f = config_get_d(CONFIG_FULLSCREEN);
141         int t = config_get_d(CONFIG_TEXTURES);
142         int h = config_get_d(CONFIG_SHADOW);
143         int s = config_get_d(CONFIG_SOUND_VOLUME);
144         int m = config_get_d(CONFIG_MUSIC_VOLUME);
145
146         char resolution[20];
147
148         sprintf(resolution, "%d x %d",
149                 config_get_d(CONFIG_WIDTH),
150                 config_get_d(CONFIG_HEIGHT));
151
152         if ((jd = gui_harray(id)))
153         {
154             gui_label(jd, _("Options"), GUI_SML, GUI_ALL, 0, 0);
155             gui_space(jd);
156             gui_start(jd, _("Back"),    GUI_SML, CONF_BACK, 0);
157         }
158
159         gui_space(id);
160
161         if ((jd = gui_harray(id)) &&
162             (kd = gui_harray(jd)))
163         {
164             gui_state(kd, _("No"),  GUI_SML, CONF_WIN,  (f == 0));
165             gui_state(kd, _("Yes"), GUI_SML, CONF_FULL, (f == 1));
166
167             gui_label(jd, _("Fullscreen"), GUI_SML, GUI_ALL, 0, 0);
168         }
169
170         if ((jd = gui_harray(id)) &&
171             (kd = gui_harray(jd)))
172         {
173             gui_state(kd, resolution, GUI_SML, CONF_RESOL, 0);
174
175             gui_label(jd, _("Resolution"), GUI_SML, GUI_ALL, 0, 0);
176         }
177
178         gui_space(id);
179
180         if ((jd = gui_harray(id)) &&
181             (kd = gui_harray(jd)))
182         {
183             gui_state(kd, _("Low"),  GUI_SML, CONF_TEXLO, (t == 2));
184             gui_state(kd, _("High"), GUI_SML, CONF_TEXHI, (t == 1));
185
186             gui_label(jd, _("Textures"), GUI_SML, GUI_ALL, 0, 0);
187         }
188
189         if ((jd = gui_harray(id)) &&
190             (kd = gui_harray(jd)))
191         {
192             gui_state(kd, _("Off"),  GUI_SML, CONF_SHDOF, (h == 0));
193             gui_state(kd, _("On"),   GUI_SML, CONF_SHDON, (h == 1));
194
195             gui_label(jd, _("Shadow"), GUI_SML, GUI_ALL, 0, 0);
196         }
197
198         gui_space(id);
199
200         if ((jd = gui_harray(id)) &&
201             (kd = gui_harray(jd)))
202         {
203             /* A series of empty buttons forms the sound volume control. */
204
205             sound_id[10] = gui_state(kd, NULL, GUI_SML, 110, (s == 10));
206             sound_id[ 9] = gui_state(kd, NULL, GUI_SML, 109, (s ==  9));
207             sound_id[ 8] = gui_state(kd, NULL, GUI_SML, 108, (s ==  8));
208             sound_id[ 7] = gui_state(kd, NULL, GUI_SML, 107, (s ==  7));
209             sound_id[ 6] = gui_state(kd, NULL, GUI_SML, 106, (s ==  6));
210             sound_id[ 5] = gui_state(kd, NULL, GUI_SML, 105, (s ==  5));
211             sound_id[ 4] = gui_state(kd, NULL, GUI_SML, 104, (s ==  4));
212             sound_id[ 3] = gui_state(kd, NULL, GUI_SML, 103, (s ==  3));
213             sound_id[ 2] = gui_state(kd, NULL, GUI_SML, 102, (s ==  2));
214             sound_id[ 1] = gui_state(kd, NULL, GUI_SML, 101, (s ==  1));
215             sound_id[ 0] = gui_state(kd, NULL, GUI_SML, 100, (s ==  0));
216
217             gui_label(jd, _("Sound Volume"), GUI_SML, GUI_ALL, 0, 0);
218         }
219
220         if ((jd = gui_harray(id)) &&
221             (kd = gui_harray(jd)))
222         {
223             /* A series of empty buttons forms the music volume control. */
224
225             music_id[10] = gui_state(kd, NULL, GUI_SML, 210, (m == 10));
226             music_id[ 9] = gui_state(kd, NULL, GUI_SML, 209, (m ==  9));
227             music_id[ 8] = gui_state(kd, NULL, GUI_SML, 208, (m ==  8));
228             music_id[ 7] = gui_state(kd, NULL, GUI_SML, 207, (m ==  7));
229             music_id[ 6] = gui_state(kd, NULL, GUI_SML, 206, (m ==  6));
230             music_id[ 5] = gui_state(kd, NULL, GUI_SML, 205, (m ==  5));
231             music_id[ 4] = gui_state(kd, NULL, GUI_SML, 204, (m ==  4));
232             music_id[ 3] = gui_state(kd, NULL, GUI_SML, 203, (m ==  3));
233             music_id[ 2] = gui_state(kd, NULL, GUI_SML, 202, (m ==  2));
234             music_id[ 1] = gui_state(kd, NULL, GUI_SML, 201, (m ==  1));
235             music_id[ 0] = gui_state(kd, NULL, GUI_SML, 200, (m ==  0));
236
237             gui_label(jd, _("Music Volume"), GUI_SML, GUI_ALL, 0, 0);
238         }
239
240         gui_layout(id, 0, 0);
241     }
242
243     audio_music_fade_to(0.5f, "bgm/inter.ogg");
244
245     return id;
246 }
247
248 static void conf_leave(struct state *st, struct state *next, int id)
249 {
250     gui_delete(id);
251 }
252
253 static void conf_paint(int id, float st)
254 {
255     video_push_persp((float) config_get_d(CONFIG_VIEW_FOV), 0.1f, FAR_DIST);
256     {
257         back_draw_easy();
258     }
259     video_pop_matrix();
260     gui_paint(id);
261 }
262
263 static void conf_timer(int id, float dt)
264 {
265     gui_timer(id, dt);
266 }
267
268 static void conf_point(int id, int x, int y, int dx, int dy)
269 {
270     gui_pulse(gui_point(id, x, y), 1.2f);
271 }
272
273 static void conf_stick(int id, int a, float v, int bump)
274 {
275     gui_pulse(gui_stick(id, a, v, bump), 1.2f);
276 }
277
278 static int conf_click(int b, int d)
279 {
280     if (b == SDL_BUTTON_LEFT && d == 1)
281         return conf_action(gui_token(gui_click()));
282     return 1;
283 }
284
285 static int conf_keybd(int c, int d)
286 {
287     return (d && c == SDLK_ESCAPE) ? goto_state(&st_title) : 1;
288 }
289
290 static int conf_buttn(int b, int d)
291 {
292     if (d)
293     {
294         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
295             return conf_action(gui_token(gui_click()));
296         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_B, b))
297             return goto_state(&st_title);
298         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
299             return goto_state(&st_title);
300     }
301     return 1;
302 }
303
304 /*---------------------------------------------------------------------------*/
305
306 static int null_enter(struct state *st, struct state *prev)
307 {
308     gui_free();
309     geom_free();
310     ball_free();
311     shad_free();
312
313     return 0;
314 }
315
316 static void null_leave(struct state *st, struct state *next, int id)
317 {
318     shad_init();
319     ball_init();
320     geom_init();
321     gui_init();
322 }
323
324 /*---------------------------------------------------------------------------*/
325
326 struct state st_conf = {
327     conf_enter,
328     conf_leave,
329     conf_paint,
330     conf_timer,
331     conf_point,
332     conf_stick,
333     NULL,
334     conf_click,
335     conf_keybd,
336     conf_buttn
337 };
338
339 struct state st_null = {
340     null_enter,
341     null_leave,
342     NULL,
343     NULL,
344     NULL,
345     NULL,
346     NULL,
347     NULL,
348     NULL,
349     NULL
350 };