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