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