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