Import from neverball-1.3.7.tar.gz
[neverball-archive.git] / ball / st_conf.c
blob76123fe1c887947f14217ee4b1247f0870b5f98d
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 "audio.h"
21 #include "config.h"
23 #include "st_conf.h"
24 #include "st_title.h"
26 /*---------------------------------------------------------------------------*/
28 #define CONF_FULL 1
29 #define CONF_WIN 2
30 #define CONF_16x12 3
31 #define CONF_12x10 4
32 #define CONF_10x7 5
33 #define CONF_8x6 6
34 #define CONF_6x4 7
35 #define CONF_TEXHI 8
36 #define CONF_TEXLO 9
37 #define CONF_GEOHI 10
38 #define CONF_GEOLO 11
39 #define CONF_REFON 12
40 #define CONF_REFOF 13
41 #define CONF_BACON 14
42 #define CONF_BACOF 15
43 #define CONF_SHDON 16
44 #define CONF_SHDOF 17
45 #define CONF_AUDHI 18
46 #define CONF_AUDLO 19
47 #define CONF_BACK 20
49 static int audlo_id;
50 static int audhi_id;
51 static int music_id[11];
52 static int sound_id[11];
54 static int conf_action(int i)
56 int f = config_get_d(CONFIG_FULLSCREEN);
57 int w = config_get_d(CONFIG_WIDTH);
58 int h = config_get_d(CONFIG_HEIGHT);
59 int s = config_get_d(CONFIG_SOUND_VOLUME);
60 int m = config_get_d(CONFIG_MUSIC_VOLUME);
61 int r = 1;
63 audio_play(AUD_MENU, 1.0f);
65 switch (i)
67 case CONF_FULL:
68 goto_state(&st_null);
69 r = config_mode(1, w, h);
70 goto_state(&st_conf);
71 break;
73 case CONF_WIN:
74 goto_state(&st_null);
75 r = config_mode(0, w, h);
76 goto_state(&st_conf);
77 break;
79 case CONF_16x12:
80 goto_state(&st_null);
81 r = config_mode(f, 1600, 1200);
82 goto_state(&st_conf);
83 break;
85 case CONF_12x10:
86 goto_state(&st_null);
87 r = config_mode(f, 1280, 1024);
88 goto_state(&st_conf);
89 break;
91 case CONF_10x7:
92 goto_state(&st_null);
93 r = config_mode(f, 1024, 768);
94 goto_state(&st_conf);
95 break;
97 case CONF_8x6:
98 goto_state(&st_null);
99 r = config_mode(f, 800, 600);
100 goto_state(&st_conf);
101 break;
103 case CONF_6x4:
104 goto_state(&st_null);
105 r = config_mode(f, 640, 480);
106 goto_state(&st_conf);
107 break;
109 case CONF_TEXHI:
110 goto_state(&st_null);
111 config_set_d(CONFIG_TEXTURES, 1);
112 goto_state(&st_conf);
113 break;
115 case CONF_TEXLO:
116 goto_state(&st_null);
117 config_set_d(CONFIG_TEXTURES, 2);
118 goto_state(&st_conf);
119 break;
121 case CONF_GEOHI:
122 goto_state(&st_null);
123 config_set_d(CONFIG_GEOMETRY, 1);
124 goto_state(&st_conf);
125 break;
127 case CONF_GEOLO:
128 goto_state(&st_null);
129 config_set_d(CONFIG_GEOMETRY, 0);
130 goto_state(&st_conf);
131 break;
133 case CONF_REFON:
134 goto_state(&st_null);
135 config_set_d(CONFIG_REFLECTION, 1);
136 goto_state(&st_conf);
137 break;
139 case CONF_REFOF:
140 goto_state(&st_null);
141 config_set_d(CONFIG_REFLECTION, 0);
142 goto_state(&st_conf);
143 break;
145 case CONF_BACON:
146 goto_state(&st_null);
147 config_set_d(CONFIG_BACKGROUND, 1);
148 goto_state(&st_conf);
149 break;
151 case CONF_BACOF:
152 goto_state(&st_null);
153 config_set_d(CONFIG_BACKGROUND, 0);
154 goto_state(&st_conf);
155 break;
157 case CONF_SHDON:
158 goto_state(&st_null);
159 config_set_d(CONFIG_SHADOW, 1);
160 goto_state(&st_conf);
161 break;
163 case CONF_SHDOF:
164 goto_state(&st_null);
165 config_set_d(CONFIG_SHADOW, 0);
166 goto_state(&st_conf);
167 break;
169 case CONF_AUDHI:
170 audio_free();
171 config_set_d(CONFIG_AUDIO_RATE, 44100);
172 config_set_d(CONFIG_AUDIO_BUFF, AUDIO_BUFF_HI);
173 gui_toggle(audlo_id);
174 gui_toggle(audhi_id);
175 audio_init();
176 break;
178 case CONF_AUDLO:
179 audio_free();
180 config_set_d(CONFIG_AUDIO_RATE, 22050);
181 config_set_d(CONFIG_AUDIO_BUFF, AUDIO_BUFF_LO);
182 gui_toggle(audlo_id);
183 gui_toggle(audhi_id);
184 audio_init();
185 break;
187 case CONF_BACK:
188 goto_state(&st_title);
189 break;
191 default:
192 if (100 <= i && i <= 110)
194 int n = i - 100;
196 config_set_d(CONFIG_SOUND_VOLUME, n);
197 audio_volume(n, m);
198 audio_play(AUD_BUMP, 1.f);
200 gui_toggle(sound_id[n]);
201 gui_toggle(sound_id[s]);
203 if (200 <= i && i <= 210)
205 int n = i - 200;
207 config_set_d(CONFIG_MUSIC_VOLUME, n);
208 audio_volume(s, n);
209 audio_play(AUD_BUMP, 1.f);
211 gui_toggle(music_id[n]);
212 gui_toggle(music_id[m]);
216 return r;
219 static int conf_enter(void)
221 int id, jd, kd;
223 /* Initialize the configuration GUI. */
225 if ((id = gui_harray(0)))
227 if ((jd = gui_varray(id)))
229 int w = config_get_d(CONFIG_WIDTH);
230 int t = config_get_d(CONFIG_TEXTURES);
231 int g = config_get_d(CONFIG_GEOMETRY);
232 int r = config_get_d(CONFIG_REFLECTION);
233 int b = config_get_d(CONFIG_BACKGROUND);
234 int h = config_get_d(CONFIG_SHADOW);
235 int a = config_get_d(CONFIG_AUDIO_RATE);
236 int s = config_get_d(CONFIG_SOUND_VOLUME);
237 int m = config_get_d(CONFIG_MUSIC_VOLUME);
239 if ((kd = gui_harray(jd)))
241 gui_label(kd, "Options", GUI_SML, GUI_ALL, 0, 0);
242 gui_filler(kd);
245 /* Add mode selectors only for existing modes. */
247 if (SDL_VideoModeOK(1600, 1200, 16, SDL_HWSURFACE))
248 gui_state(jd, "1600 x 1200", GUI_SML, CONF_16x12, (w == 1600));
249 if (SDL_VideoModeOK(1280, 1024, 16, SDL_HWSURFACE))
250 gui_state(jd, "1280 x 1024", GUI_SML, CONF_12x10, (w == 1280));
251 if (SDL_VideoModeOK(1024, 768, 16, SDL_HWSURFACE))
252 gui_state(jd, "1024 x 768", GUI_SML, CONF_10x7, (w == 1024));
253 if (SDL_VideoModeOK(800, 600, 16, SDL_HWSURFACE))
254 gui_state(jd, "800 x 600", GUI_SML, CONF_8x6, (w == 800));
255 if (SDL_VideoModeOK(640, 480, 16, SDL_HWSURFACE))
256 gui_state(jd, "640 x 480", GUI_SML, CONF_6x4, (w == 640));
258 if ((kd = gui_harray(jd)))
260 gui_state(kd, "Low", GUI_SML, CONF_TEXLO, (t == 2));
261 gui_state(kd, "High", GUI_SML, CONF_TEXHI, (t == 1));
263 if ((kd = gui_harray(jd)))
265 gui_state(kd, "Low", GUI_SML, CONF_GEOLO, (g == 0));
266 gui_state(kd, "High", GUI_SML, CONF_GEOHI, (g == 1));
268 if ((kd = gui_harray(jd)))
270 gui_state(kd, "Off", GUI_SML, CONF_REFOF, (r == 0));
271 gui_state(kd, "On", GUI_SML, CONF_REFON, (r == 1));
273 if ((kd = gui_harray(jd)))
275 gui_state(kd, "Off", GUI_SML, CONF_BACOF, (b == 0));
276 gui_state(kd, "On", GUI_SML, CONF_BACON, (b == 1));
278 if ((kd = gui_harray(jd)))
280 gui_state(kd, "Off", GUI_SML, CONF_SHDOF, (h == 0));
281 gui_state(kd, "On", GUI_SML, CONF_SHDON, (h == 1));
283 if ((kd = gui_harray(jd)))
285 int lo = (a == 22050);
286 int hi = (a == 44100);
288 audlo_id = gui_state(kd, "Low", GUI_SML, CONF_AUDLO, lo);
289 audhi_id = gui_state(kd, "High", GUI_SML, CONF_AUDHI, hi);
291 if ((kd = gui_harray(jd)))
293 /* A series of empty buttons forms the sound volume control. */
295 sound_id[10] = gui_state(kd, NULL, GUI_SML, 110, (s == 10));
296 sound_id[ 9] = gui_state(kd, NULL, GUI_SML, 109, (s == 9));
297 sound_id[ 8] = gui_state(kd, NULL, GUI_SML, 108, (s == 8));
298 sound_id[ 7] = gui_state(kd, NULL, GUI_SML, 107, (s == 7));
299 sound_id[ 6] = gui_state(kd, NULL, GUI_SML, 106, (s == 6));
300 sound_id[ 5] = gui_state(kd, NULL, GUI_SML, 105, (s == 5));
301 sound_id[ 4] = gui_state(kd, NULL, GUI_SML, 104, (s == 4));
302 sound_id[ 3] = gui_state(kd, NULL, GUI_SML, 103, (s == 3));
303 sound_id[ 2] = gui_state(kd, NULL, GUI_SML, 102, (s == 2));
304 sound_id[ 1] = gui_state(kd, NULL, GUI_SML, 101, (s == 1));
305 sound_id[ 0] = gui_state(kd, NULL, GUI_SML, 100, (s == 0));
307 if ((kd = gui_harray(jd)))
309 /* A series of empty buttons forms the music volume control. */
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));
324 if ((jd = gui_vstack(id)))
326 int f = config_get_d(CONFIG_FULLSCREEN);
328 if ((kd = gui_harray(jd)))
330 gui_filler(kd);
331 gui_start(kd, "Back", GUI_SML, CONF_BACK, 0);
334 gui_state(jd, "Fullscreen", GUI_SML, CONF_FULL, (f == 1));
335 gui_state(jd, "Window", GUI_SML, CONF_WIN, (f == 0));
337 /* This filler expands to accomodate an unknown number of modes. */
338 gui_filler(jd);
340 gui_label(jd, "Textures", GUI_SML, GUI_ALL, 0, 0);
341 gui_label(jd, "Geometry", GUI_SML, GUI_ALL, 0, 0);
342 gui_label(jd, "Reflection", GUI_SML, GUI_ALL, 0, 0);
343 gui_label(jd, "Background", GUI_SML, GUI_ALL, 0, 0);
344 gui_label(jd, "Shadow", GUI_SML, GUI_ALL, 0, 0);
345 gui_label(jd, "Audio", GUI_SML, GUI_ALL, 0, 0);
346 gui_label(jd, "Sound Volume", GUI_SML, GUI_ALL, 0, 0);
347 gui_label(jd, "Music Volume", GUI_SML, GUI_ALL, 0, 0);
349 gui_layout(id, 0, 0);
352 /* Initialize the background. */
354 back_init("png/blues.png", config_get_d(CONFIG_GEOMETRY));
356 audio_music_fade_to(0.5f, "bgm/inter.ogg");
358 return id;
361 static void conf_leave(int id)
363 back_free();
364 gui_delete(id);
367 static void conf_paint(int id, float st)
369 config_push_persp((float) config_get_d(CONFIG_VIEW_FOV), 0.1f, FAR_DIST);
371 back_draw(0);
373 config_pop_matrix();
374 gui_paint(id);
377 static void conf_timer(int id, float dt)
379 gui_timer(id, dt);
380 audio_timer(dt);
383 static void conf_point(int id, int x, int y, int dx, int dy)
385 gui_pulse(gui_point(id, x, y), 1.2f);
388 static void conf_stick(int id, int a, int v)
390 if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
391 gui_pulse(gui_stick(id, v, 0), 1.2f);
392 if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
393 gui_pulse(gui_stick(id, 0, v), 1.2f);
396 static int conf_click(int b, int d)
398 if (b < 0 && d == 1)
399 return conf_action(gui_token(gui_click()));
400 return 1;
403 static int conf_keybd(int c, int d)
405 return (d && c == SDLK_ESCAPE) ? goto_state(&st_title) : 1;
408 static int conf_buttn(int b, int d)
410 if (d)
412 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
413 return conf_action(gui_token(gui_click()));
414 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_B, b))
415 return goto_state(&st_title);
416 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
417 return goto_state(&st_title);
419 return 1;
422 /*---------------------------------------------------------------------------*/
424 static int null_enter(void)
426 hud_free();
427 gui_free();
428 swch_free();
429 jump_free();
430 goal_free();
431 coin_free();
432 ball_free();
433 shad_free();
434 part_free();
436 return 0;
439 static void null_leave(int id)
441 int g = config_get_d(CONFIG_GEOMETRY);
443 part_init(GOAL_HEIGHT);
444 shad_init();
445 ball_init(g);
446 coin_init(g);
447 goal_init(g);
448 jump_init(g);
449 swch_init(g);
450 gui_init();
451 hud_init();
454 /*---------------------------------------------------------------------------*/
456 struct state st_conf = {
457 conf_enter,
458 conf_leave,
459 conf_paint,
460 conf_timer,
461 conf_point,
462 conf_stick,
463 conf_click,
464 conf_keybd,
465 conf_buttn,
466 1, 0
469 struct state st_null = {
470 null_enter,
471 null_leave,
472 NULL,
473 NULL,
474 NULL,
475 NULL,
476 NULL,
477 NULL,
478 NULL,
479 1, 0