Revert "Merge 'putt-collisions' branch"
[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 "back.h"
18 #include "geom.h"
19 #include "ball.h"
20 #include "part.h"
21 #include "game.h"
22 #include "audio.h"
23 #include "config.h"
24
25 #include "st_conf.h"
26 #include "st_all.h"
27 #include "st_resol.h"
28
29 /*---------------------------------------------------------------------------*/
30
31 enum {
32     CONF_FULL = 1,
33     CONF_WIN,
34     CONF_TEXHI,
35     CONF_TEXLO,
36     CONF_GEOHI,
37     CONF_GEOLO,
38     CONF_SHDON,
39     CONF_SHDOF,
40     CONF_BACK,
41     CONF_RESOL
42 };
43
44 static int music_id[11];
45 static int sound_id[11];
46
47 static int conf_action(int i)
48 {
49     int w = config_get_d(CONFIG_WIDTH);
50     int h = config_get_d(CONFIG_HEIGHT);
51     int s = config_get_d(CONFIG_SOUND_VOLUME);
52     int m = config_get_d(CONFIG_MUSIC_VOLUME);
53     int r = 1;
54
55     audio_play(AUD_MENU, 1.0f);
56
57     switch (i)
58     {
59     case CONF_FULL:
60         goto_state(&st_null);
61         r = config_mode(1, w, h);
62         goto_state(&st_conf);
63         break;
64
65     case CONF_WIN:
66         goto_state(&st_null);
67         r = config_mode(0, w, h);
68         goto_state(&st_conf);
69         break;
70
71     case CONF_TEXHI:
72         goto_state(&st_null);
73         config_set_d(CONFIG_TEXTURES, 1);
74         goto_state(&st_conf);
75         break;
76
77     case CONF_TEXLO:
78         goto_state(&st_null);
79         config_set_d(CONFIG_TEXTURES, 2);
80         goto_state(&st_conf);
81         break;
82
83     case CONF_GEOHI:
84         goto_state(&st_null);
85         config_set_d(CONFIG_GEOMETRY, 1);
86         goto_state(&st_conf);
87         break;
88
89     case CONF_GEOLO:
90         goto_state(&st_null);
91         config_set_d(CONFIG_GEOMETRY, 0);
92         goto_state(&st_conf);
93         break;
94
95     case CONF_SHDON:
96         goto_state(&st_null);
97         config_set_d(CONFIG_SHADOW, 1);
98         goto_state(&st_conf);
99         break;
100
101     case CONF_SHDOF:
102         goto_state(&st_null);
103         config_set_d(CONFIG_SHADOW, 0);
104         goto_state(&st_conf);
105         break;
106
107     case CONF_BACK:
108         goto_state(&st_title);
109         break;
110
111     case CONF_RESOL:
112         goto_state(&st_resol);
113         break;
114
115     default:
116         if (100 <= i && i <= 110)
117         {
118             int n = i - 100;
119
120             config_set_d(CONFIG_SOUND_VOLUME, n);
121             audio_volume(n, m);
122             audio_play(AUD_BUMP, 1.f);
123
124             gui_toggle(sound_id[n]);
125             gui_toggle(sound_id[s]);
126         }
127         if (200 <= i && i <= 210)
128         {
129             int n = i - 200;
130
131             config_set_d(CONFIG_MUSIC_VOLUME, n);
132             audio_volume(s, n);
133             audio_play(AUD_BUMP, 1.f);
134
135             gui_toggle(music_id[n]);
136             gui_toggle(music_id[m]);
137         }
138     }
139
140     return r;
141 }
142
143 static int conf_enter(void)
144 {
145     int id, jd, kd;
146
147     back_init("back/gui.png", config_get_d(CONFIG_GEOMETRY));
148
149     /* Initialize the configuration GUI. */
150
151     if ((id = gui_vstack(0)))
152     {
153         int f = config_get_d(CONFIG_FULLSCREEN);
154         int t = config_get_d(CONFIG_TEXTURES);
155         int g = config_get_d(CONFIG_GEOMETRY);
156         int h = config_get_d(CONFIG_SHADOW);
157         int s = config_get_d(CONFIG_SOUND_VOLUME);
158         int m = config_get_d(CONFIG_MUSIC_VOLUME);
159
160         char resolution[20];
161
162         sprintf(resolution, "%d x %d",
163                 config_get_d(CONFIG_WIDTH),
164                 config_get_d(CONFIG_HEIGHT));
165
166         if ((jd = gui_harray(id)))
167         {
168             gui_label(jd, _("Options"), GUI_SML, GUI_ALL, 0, 0);
169             gui_space(jd);
170             gui_start(jd, _("Back"),    GUI_SML, CONF_BACK, 0);
171         }
172
173         gui_space(id);
174
175         if ((jd = gui_harray(id)) &&
176             (kd = gui_harray(jd)))
177         {
178             gui_state(kd, _("No"),  GUI_SML, CONF_WIN,  (f == 0));
179             gui_state(kd, _("Yes"), GUI_SML, CONF_FULL, (f == 1));
180
181             gui_label(jd, _("Fullscreen"), GUI_SML, GUI_ALL, 0, 0);
182         }
183
184         if ((jd = gui_harray(id)) &&
185             (kd = gui_harray(jd)))
186         {
187             gui_state(kd, resolution, GUI_SML, CONF_RESOL, 0);
188
189             gui_label(jd, _("Resolution"), GUI_SML, GUI_ALL, 0, 0);
190         }
191
192         gui_space(id);
193
194         if ((jd = gui_harray(id)) &&
195             (kd = gui_harray(jd)))
196         {
197             gui_state(kd, _("Low"),  GUI_SML, CONF_TEXLO, (t == 2));
198             gui_state(kd, _("High"), GUI_SML, CONF_TEXHI, (t == 1));
199
200             gui_label(jd, _("Textures"), GUI_SML, GUI_ALL, 0, 0);
201         }
202
203         if ((jd = gui_harray(id)) &&
204             (kd = gui_harray(jd)))
205         {
206             gui_state(kd, _("Low"),  GUI_SML, CONF_GEOLO, (g == 0));
207             gui_state(kd, _("High"), GUI_SML, CONF_GEOHI, (g == 1));
208
209             gui_label(jd, _("Geometry"), GUI_SML, GUI_ALL, 0, 0);
210         }
211
212         if ((jd = gui_harray(id)) &&
213             (kd = gui_harray(jd)))
214         {
215             gui_state(kd, _("Off"),  GUI_SML, CONF_SHDOF, (h == 0));
216             gui_state(kd, _("On"),   GUI_SML, CONF_SHDON, (h == 1));
217
218             gui_label(jd, _("Shadow"), GUI_SML, GUI_ALL, 0, 0);
219         }
220
221         gui_space(id);
222
223         if ((jd = gui_harray(id)) &&
224             (kd = gui_harray(jd)))
225         {
226             /* A series of empty buttons forms the sound volume control. */
227
228             sound_id[10] = gui_state(kd, NULL, GUI_SML, 110, (s == 10));
229             sound_id[ 9] = gui_state(kd, NULL, GUI_SML, 109, (s ==  9));
230             sound_id[ 8] = gui_state(kd, NULL, GUI_SML, 108, (s ==  8));
231             sound_id[ 7] = gui_state(kd, NULL, GUI_SML, 107, (s ==  7));
232             sound_id[ 6] = gui_state(kd, NULL, GUI_SML, 106, (s ==  6));
233             sound_id[ 5] = gui_state(kd, NULL, GUI_SML, 105, (s ==  5));
234             sound_id[ 4] = gui_state(kd, NULL, GUI_SML, 104, (s ==  4));
235             sound_id[ 3] = gui_state(kd, NULL, GUI_SML, 103, (s ==  3));
236             sound_id[ 2] = gui_state(kd, NULL, GUI_SML, 102, (s ==  2));
237             sound_id[ 1] = gui_state(kd, NULL, GUI_SML, 101, (s ==  1));
238             sound_id[ 0] = gui_state(kd, NULL, GUI_SML, 100, (s ==  0));
239
240             gui_label(jd, _("Sound Volume"), GUI_SML, GUI_ALL, 0, 0);
241         }
242
243         if ((jd = gui_harray(id)) &&
244             (kd = gui_harray(jd)))
245         {
246             /* A series of empty buttons forms the music volume control. */
247
248             music_id[10] = gui_state(kd, NULL, GUI_SML, 210, (m == 10));
249             music_id[ 9] = gui_state(kd, NULL, GUI_SML, 209, (m ==  9));
250             music_id[ 8] = gui_state(kd, NULL, GUI_SML, 208, (m ==  8));
251             music_id[ 7] = gui_state(kd, NULL, GUI_SML, 207, (m ==  7));
252             music_id[ 6] = gui_state(kd, NULL, GUI_SML, 206, (m ==  6));
253             music_id[ 5] = gui_state(kd, NULL, GUI_SML, 205, (m ==  5));
254             music_id[ 4] = gui_state(kd, NULL, GUI_SML, 204, (m ==  4));
255             music_id[ 3] = gui_state(kd, NULL, GUI_SML, 203, (m ==  3));
256             music_id[ 2] = gui_state(kd, NULL, GUI_SML, 202, (m ==  2));
257             music_id[ 1] = gui_state(kd, NULL, GUI_SML, 201, (m ==  1));
258             music_id[ 0] = gui_state(kd, NULL, GUI_SML, 200, (m ==  0));
259
260             gui_label(jd, _("Music Volume"), GUI_SML, GUI_ALL, 0, 0);
261         }
262
263         gui_layout(id, 0, 0);
264     }
265
266     audio_music_fade_to(0.5f, "bgm/inter.ogg");
267
268     return id;
269 }
270
271 static void conf_leave(int id)
272 {
273     gui_delete(id);
274 }
275
276 static void conf_paint(int id, float st)
277 {
278     config_push_persp((float) config_get_d(CONFIG_VIEW_FOV), 0.1f, FAR_DIST);
279     {
280         back_draw(0);
281     }
282     config_pop_matrix();
283     gui_paint(id);
284 }
285
286 static void conf_timer(int id, float dt)
287 {
288     gui_timer(id, dt);
289 }
290
291 static void conf_point(int id, int x, int y, int dx, int dy)
292 {
293     gui_pulse(gui_point(id, x, y), 1.2f);
294 }
295
296 static void conf_stick(int id, int a, int v)
297 {
298     if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
299         gui_pulse(gui_stick(id, v, 0), 1.2f);
300     if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
301         gui_pulse(gui_stick(id, 0, v), 1.2f);
302 }
303
304 static int conf_click(int b, int d)
305 {
306     if (b < 0 && d == 1)
307         return conf_action(gui_token(gui_click()));
308     return 1;
309 }
310
311 static int conf_keybd(int c, int d)
312 {
313     return (d && c == SDLK_ESCAPE) ? goto_state(&st_title) : 1;
314 }
315
316 static int conf_buttn(int b, int d)
317 {
318     if (d)
319     {
320         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
321             return conf_action(gui_token(gui_click()));
322         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_B, b))
323             return goto_state(&st_title);
324         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
325             return goto_state(&st_title);
326     }
327     return 1;
328 }
329
330 /*---------------------------------------------------------------------------*/
331
332 static int null_enter(void)
333 {
334     gui_free();
335     swch_free();
336     jump_free();
337     flag_free();
338     mark_free();
339     ball_free();
340     shad_free();
341
342     return 0;
343 }
344
345 static void null_leave(int id)
346 {
347     int g = config_get_d(CONFIG_GEOMETRY);
348
349     shad_init();
350     ball_init();
351     mark_init(g);
352     flag_init(g);
353     jump_init(g);
354     swch_init(g);
355     gui_init();
356 }
357
358 /*---------------------------------------------------------------------------*/
359
360 struct state st_conf = {
361     conf_enter,
362     conf_leave,
363     conf_paint,
364     conf_timer,
365     conf_point,
366     conf_stick,
367     NULL,
368     conf_click,
369     conf_keybd,
370     conf_buttn,
371     1, 0
372 };
373
374 struct state st_null = {
375     null_enter,
376     null_leave,
377     NULL,
378     NULL,
379     NULL,
380     NULL,
381     NULL,
382     NULL,
383     NULL,
384     NULL,
385     1, 0
386 };