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