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