Import from neverball-1.4.0.tar.gz
[neverball-archive.git] / ball / st_conf.c
blob846066fd4d868cf76012689ee2b7e61d4eaca8f3
1 /*
2 * Copyright (C) 2003 Robert Kooima
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.
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.
15 #include "gui.h"
16 #include "hud.h"
17 #include "back.h"
18 #include "geom.h"
19 #include "part.h"
20 #include "game.h"
21 #include "audio.h"
22 #include "config.h"
24 #include "st_conf.h"
25 #include "st_title.h"
27 /*---------------------------------------------------------------------------*/
29 #define CONF_FULL 1
30 #define CONF_WIN 2
31 #define CONF_16x12 3
32 #define CONF_12x10 4
33 #define CONF_10x7 5
34 #define CONF_8x6 6
35 #define CONF_6x4 7
36 #define CONF_TEXHI 8
37 #define CONF_TEXLO 9
38 #define CONF_GEOHI 10
39 #define CONF_GEOLO 11
40 #define CONF_REFON 12
41 #define CONF_REFOF 13
42 #define CONF_BACON 14
43 #define CONF_BACOF 15
44 #define CONF_SHDON 16
45 #define CONF_SHDOF 17
46 #define CONF_AUDHI 18
47 #define CONF_AUDLO 19
48 #define CONF_BACK 20
50 static int audlo_id;
51 static int audhi_id;
52 static int music_id[11];
53 static int sound_id[11];
55 static int conf_action(int i)
57 int f = config_get_d(CONFIG_FULLSCREEN);
58 int w = config_get_d(CONFIG_WIDTH);
59 int h = config_get_d(CONFIG_HEIGHT);
60 int s = config_get_d(CONFIG_SOUND_VOLUME);
61 int m = config_get_d(CONFIG_MUSIC_VOLUME);
62 int r = 1;
64 audio_play(AUD_MENU, 1.0f);
66 switch (i)
68 case CONF_FULL:
69 goto_state(&st_null);
70 r = config_mode(1, w, h);
71 goto_state(&st_conf);
72 break;
74 case CONF_WIN:
75 goto_state(&st_null);
76 r = config_mode(0, w, h);
77 goto_state(&st_conf);
78 break;
80 case CONF_16x12:
81 goto_state(&st_null);
82 r = config_mode(f, 1600, 1200);
83 goto_state(&st_conf);
84 break;
86 case CONF_12x10:
87 goto_state(&st_null);
88 r = config_mode(f, 1280, 1024);
89 goto_state(&st_conf);
90 break;
92 case CONF_10x7:
93 goto_state(&st_null);
94 r = config_mode(f, 1024, 768);
95 goto_state(&st_conf);
96 break;
98 case CONF_8x6:
99 goto_state(&st_null);
100 r = config_mode(f, 800, 600);
101 goto_state(&st_conf);
102 break;
104 case CONF_6x4:
105 goto_state(&st_null);
106 r = config_mode(f, 640, 480);
107 goto_state(&st_conf);
108 break;
110 case CONF_TEXHI:
111 goto_state(&st_null);
112 config_set_d(CONFIG_TEXTURES, 1);
113 goto_state(&st_conf);
114 break;
116 case CONF_TEXLO:
117 goto_state(&st_null);
118 config_set_d(CONFIG_TEXTURES, 2);
119 goto_state(&st_conf);
120 break;
122 case CONF_GEOHI:
123 goto_state(&st_null);
124 config_set_d(CONFIG_GEOMETRY, 1);
125 goto_state(&st_conf);
126 break;
128 case CONF_GEOLO:
129 goto_state(&st_null);
130 config_set_d(CONFIG_GEOMETRY, 0);
131 goto_state(&st_conf);
132 break;
134 case CONF_REFON:
135 goto_state(&st_null);
136 config_set_d(CONFIG_REFLECTION, 1);
137 goto_state(&st_conf);
138 break;
140 case CONF_REFOF:
141 goto_state(&st_null);
142 config_set_d(CONFIG_REFLECTION, 0);
143 goto_state(&st_conf);
144 break;
146 case CONF_BACON:
147 goto_state(&st_null);
148 config_set_d(CONFIG_BACKGROUND, 1);
149 goto_state(&st_conf);
150 break;
152 case CONF_BACOF:
153 goto_state(&st_null);
154 config_set_d(CONFIG_BACKGROUND, 0);
155 goto_state(&st_conf);
156 break;
158 case CONF_SHDON:
159 goto_state(&st_null);
160 config_set_d(CONFIG_SHADOW, 1);
161 goto_state(&st_conf);
162 break;
164 case CONF_SHDOF:
165 goto_state(&st_null);
166 config_set_d(CONFIG_SHADOW, 0);
167 goto_state(&st_conf);
168 break;
170 case CONF_AUDHI:
171 audio_free();
172 config_set_d(CONFIG_AUDIO_RATE, 44100);
173 config_set_d(CONFIG_AUDIO_BUFF, AUDIO_BUFF_HI);
174 gui_toggle(audlo_id);
175 gui_toggle(audhi_id);
176 audio_init();
177 break;
179 case CONF_AUDLO:
180 audio_free();
181 config_set_d(CONFIG_AUDIO_RATE, 22050);
182 config_set_d(CONFIG_AUDIO_BUFF, AUDIO_BUFF_LO);
183 gui_toggle(audlo_id);
184 gui_toggle(audhi_id);
185 audio_init();
186 break;
188 case CONF_BACK:
189 goto_state(&st_title);
190 break;
192 default:
193 if (100 <= i && i <= 110)
195 int n = i - 100;
197 config_set_d(CONFIG_SOUND_VOLUME, n);
198 audio_volume(n, m);
199 audio_play(AUD_BUMP, 1.f);
201 gui_toggle(sound_id[n]);
202 gui_toggle(sound_id[s]);
204 if (200 <= i && i <= 210)
206 int n = i - 200;
208 config_set_d(CONFIG_MUSIC_VOLUME, n);
209 audio_volume(s, n);
210 audio_play(AUD_BUMP, 1.f);
212 gui_toggle(music_id[n]);
213 gui_toggle(music_id[m]);
217 return r;
220 static int conf_enter(void)
222 int id, jd, kd;
224 back_init("back/gui.png", config_get_d(CONFIG_GEOMETRY));
226 /* Initialize the configuration GUI. */
228 if ((id = gui_harray(0)))
230 if ((jd = gui_varray(id)))
232 int w = config_get_d(CONFIG_WIDTH);
233 int t = config_get_d(CONFIG_TEXTURES);
234 int g = config_get_d(CONFIG_GEOMETRY);
235 int r = config_get_d(CONFIG_REFLECTION);
236 int b = config_get_d(CONFIG_BACKGROUND);
237 int h = config_get_d(CONFIG_SHADOW);
238 int a = config_get_d(CONFIG_AUDIO_RATE);
239 int s = config_get_d(CONFIG_SOUND_VOLUME);
240 int m = config_get_d(CONFIG_MUSIC_VOLUME);
242 if ((kd = gui_harray(jd)))
244 gui_label(kd, "Options", GUI_SML, GUI_ALL, 0, 0);
245 gui_filler(kd);
248 /* Add mode selectors only for existing modes. */
250 if (SDL_VideoModeOK(1600, 1200, 16, SDL_HWSURFACE))
251 gui_state(jd, "1600 x 1200", GUI_SML, CONF_16x12, (w == 1600));
252 if (SDL_VideoModeOK(1280, 1024, 16, SDL_HWSURFACE))
253 gui_state(jd, "1280 x 1024", GUI_SML, CONF_12x10, (w == 1280));
254 if (SDL_VideoModeOK(1024, 768, 16, SDL_HWSURFACE))
255 gui_state(jd, "1024 x 768", GUI_SML, CONF_10x7, (w == 1024));
256 if (SDL_VideoModeOK(800, 600, 16, SDL_HWSURFACE))
257 gui_state(jd, "800 x 600", GUI_SML, CONF_8x6, (w == 800));
258 if (SDL_VideoModeOK(640, 480, 16, SDL_HWSURFACE))
259 gui_state(jd, "640 x 480", GUI_SML, CONF_6x4, (w == 640));
261 if ((kd = gui_harray(jd)))
263 gui_state(kd, "Low", GUI_SML, CONF_TEXLO, (t == 2));
264 gui_state(kd, "High", GUI_SML, CONF_TEXHI, (t == 1));
266 if ((kd = gui_harray(jd)))
268 gui_state(kd, "Low", GUI_SML, CONF_GEOLO, (g == 0));
269 gui_state(kd, "High", GUI_SML, CONF_GEOHI, (g == 1));
271 if ((kd = gui_harray(jd)))
273 gui_state(kd, "Off", GUI_SML, CONF_REFOF, (r == 0));
274 gui_state(kd, "On", GUI_SML, CONF_REFON, (r == 1));
276 if ((kd = gui_harray(jd)))
278 gui_state(kd, "Off", GUI_SML, CONF_BACOF, (b == 0));
279 gui_state(kd, "On", GUI_SML, CONF_BACON, (b == 1));
281 if ((kd = gui_harray(jd)))
283 gui_state(kd, "Off", GUI_SML, CONF_SHDOF, (h == 0));
284 gui_state(kd, "On", GUI_SML, CONF_SHDON, (h == 1));
286 if ((kd = gui_harray(jd)))
288 int lo = (a == 22050);
289 int hi = (a == 44100);
291 audlo_id = gui_state(kd, "Low", GUI_SML, CONF_AUDLO, lo);
292 audhi_id = gui_state(kd, "High", GUI_SML, CONF_AUDHI, hi);
294 if ((kd = gui_harray(jd)))
296 /* A series of empty buttons forms the sound volume control. */
298 sound_id[10] = gui_state(kd, NULL, GUI_SML, 110, (s == 10));
299 sound_id[ 9] = gui_state(kd, NULL, GUI_SML, 109, (s == 9));
300 sound_id[ 8] = gui_state(kd, NULL, GUI_SML, 108, (s == 8));
301 sound_id[ 7] = gui_state(kd, NULL, GUI_SML, 107, (s == 7));
302 sound_id[ 6] = gui_state(kd, NULL, GUI_SML, 106, (s == 6));
303 sound_id[ 5] = gui_state(kd, NULL, GUI_SML, 105, (s == 5));
304 sound_id[ 4] = gui_state(kd, NULL, GUI_SML, 104, (s == 4));
305 sound_id[ 3] = gui_state(kd, NULL, GUI_SML, 103, (s == 3));
306 sound_id[ 2] = gui_state(kd, NULL, GUI_SML, 102, (s == 2));
307 sound_id[ 1] = gui_state(kd, NULL, GUI_SML, 101, (s == 1));
308 sound_id[ 0] = gui_state(kd, NULL, GUI_SML, 100, (s == 0));
310 if ((kd = gui_harray(jd)))
312 /* A series of empty buttons forms the music volume control. */
314 music_id[10] = gui_state(kd, NULL, GUI_SML, 210, (m == 10));
315 music_id[ 9] = gui_state(kd, NULL, GUI_SML, 209, (m == 9));
316 music_id[ 8] = gui_state(kd, NULL, GUI_SML, 208, (m == 8));
317 music_id[ 7] = gui_state(kd, NULL, GUI_SML, 207, (m == 7));
318 music_id[ 6] = gui_state(kd, NULL, GUI_SML, 206, (m == 6));
319 music_id[ 5] = gui_state(kd, NULL, GUI_SML, 205, (m == 5));
320 music_id[ 4] = gui_state(kd, NULL, GUI_SML, 204, (m == 4));
321 music_id[ 3] = gui_state(kd, NULL, GUI_SML, 203, (m == 3));
322 music_id[ 2] = gui_state(kd, NULL, GUI_SML, 202, (m == 2));
323 music_id[ 1] = gui_state(kd, NULL, GUI_SML, 201, (m == 1));
324 music_id[ 0] = gui_state(kd, NULL, GUI_SML, 200, (m == 0));
327 if ((jd = gui_vstack(id)))
329 int f = config_get_d(CONFIG_FULLSCREEN);
331 if ((kd = gui_harray(jd)))
333 gui_filler(kd);
334 gui_start(kd, "Back", GUI_SML, CONF_BACK, 0);
337 gui_state(jd, "Fullscreen", GUI_SML, CONF_FULL, (f == 1));
338 gui_state(jd, "Window", GUI_SML, CONF_WIN, (f == 0));
340 /* This filler expands to accomodate an unknown number of modes. */
341 gui_filler(jd);
343 gui_label(jd, "Textures", GUI_SML, GUI_ALL, 0, 0);
344 gui_label(jd, "Geometry", GUI_SML, GUI_ALL, 0, 0);
345 gui_label(jd, "Reflection", GUI_SML, GUI_ALL, 0, 0);
346 gui_label(jd, "Background", GUI_SML, GUI_ALL, 0, 0);
347 gui_label(jd, "Shadow", GUI_SML, GUI_ALL, 0, 0);
348 gui_label(jd, "Audio", GUI_SML, GUI_ALL, 0, 0);
349 gui_label(jd, "Sound Volume", GUI_SML, GUI_ALL, 0, 0);
350 gui_label(jd, "Music Volume", GUI_SML, GUI_ALL, 0, 0);
352 gui_layout(id, 0, 0);
355 audio_music_fade_to(0.5f, "bgm/inter.ogg");
357 return id;
360 static void conf_leave(int id)
362 back_free();
363 gui_delete(id);
366 static void conf_paint(int id, float st)
368 config_push_persp((float) config_get_d(CONFIG_VIEW_FOV), 0.1f, FAR_DIST);
370 back_draw(0);
372 config_pop_matrix();
373 gui_paint(id);
376 static void conf_timer(int id, float dt)
378 gui_timer(id, dt);
379 audio_timer(dt);
382 static void conf_point(int id, int x, int y, int dx, int dy)
384 gui_pulse(gui_point(id, x, y), 1.2f);
387 static void conf_stick(int id, int a, int v)
389 if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
390 gui_pulse(gui_stick(id, v, 0), 1.2f);
391 if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
392 gui_pulse(gui_stick(id, 0, v), 1.2f);
395 static int conf_click(int b, int d)
397 if (b < 0 && d == 1)
398 return conf_action(gui_token(gui_click()));
399 return 1;
402 static int conf_keybd(int c, int d)
404 return (d && c == SDLK_ESCAPE) ? goto_state(&st_title) : 1;
407 static int conf_buttn(int b, int d)
409 if (d)
411 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
412 return conf_action(gui_token(gui_click()));
413 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_B, b))
414 return goto_state(&st_title);
415 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
416 return goto_state(&st_title);
418 return 1;
421 /*---------------------------------------------------------------------------*/
423 static int null_enter(void)
425 hud_free();
426 gui_free();
427 swch_free();
428 jump_free();
429 goal_free();
430 coin_free();
431 ball_free();
432 shad_free();
433 part_free();
435 return 0;
438 static void null_leave(int id)
440 int g = config_get_d(CONFIG_GEOMETRY);
442 part_init(GOAL_HEIGHT);
443 shad_init();
444 ball_init(g);
445 coin_init(g);
446 goal_init(g);
447 jump_init(g);
448 swch_init(g);
449 gui_init();
450 hud_init();
453 /*---------------------------------------------------------------------------*/
455 struct state st_conf = {
456 conf_enter,
457 conf_leave,
458 conf_paint,
459 conf_timer,
460 conf_point,
461 conf_stick,
462 conf_click,
463 conf_keybd,
464 conf_buttn,
465 1, 0
468 struct state st_null = {
469 null_enter,
470 null_leave,
471 NULL,
472 NULL,
473 NULL,
474 NULL,
475 NULL,
476 NULL,
477 NULL,
478 1, 0