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