Import from neverball-1.2.6-9.tar.gz
[neverball-archive.git] / ball / st_conf.c
blob5cfcf8c693b66efbdf7bd317ed4bf157b408d187
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 "audio.h"
20 #include "config.h"
22 #include "st_conf.h"
23 #include "st_title.h"
25 /*---------------------------------------------------------------------------*/
27 #define CONF_FULL 1
28 #define CONF_WIN 2
29 #define CONF_16x12 3
30 #define CONF_12x10 4
31 #define CONF_10x7 5
32 #define CONF_8x6 6
33 #define CONF_6x4 7
34 #define CONF_TEXHI 8
35 #define CONF_TEXLO 9
36 #define CONF_GEOHI 10
37 #define CONF_GEOLO 11
38 #define CONF_REFON 12
39 #define CONF_REFOF 13
40 #define CONF_AUDHI 14
41 #define CONF_AUDLO 15
42 #define CONF_BACK 16
44 static int audlo_id;
45 static int audhi_id;
46 static int music_id[11];
47 static int sound_id[11];
49 static int conf_action(int i)
51 int f = config_get(CONFIG_FULLSCREEN);
52 int w = config_get(CONFIG_WIDTH);
53 int h = config_get(CONFIG_HEIGHT);
54 int s = config_get(CONFIG_SOUND_VOLUME);
55 int m = config_get(CONFIG_MUSIC_VOLUME);
56 int r = 1;
58 switch (i)
60 case CONF_FULL:
61 goto_state(&st_null);
62 r = config_mode(1, w, h);
63 goto_state(&st_conf);
64 break;
66 case CONF_WIN:
67 goto_state(&st_null);
68 r = config_mode(0, w, h);
69 goto_state(&st_conf);
70 break;
72 case CONF_16x12:
73 goto_state(&st_null);
74 r = config_mode(f, 1600, 1200);
75 goto_state(&st_conf);
76 break;
78 case CONF_12x10:
79 goto_state(&st_null);
80 r = config_mode(f, 1280, 1024);
81 goto_state(&st_conf);
82 break;
84 case CONF_10x7:
85 goto_state(&st_null);
86 r = config_mode(f, 1024, 768);
87 goto_state(&st_conf);
88 break;
90 case CONF_8x6:
91 goto_state(&st_null);
92 r = config_mode(f, 800, 600);
93 goto_state(&st_conf);
94 break;
96 case CONF_6x4:
97 goto_state(&st_null);
98 r = config_mode(f, 640, 480);
99 goto_state(&st_conf);
100 break;
102 case CONF_TEXHI:
103 goto_state(&st_null);
104 config_set(CONFIG_TEXTURES, 1);
105 goto_state(&st_conf);
106 break;
108 case CONF_TEXLO:
109 goto_state(&st_null);
110 config_set(CONFIG_TEXTURES, 2);
111 goto_state(&st_conf);
112 break;
114 case CONF_GEOHI:
115 goto_state(&st_null);
116 config_set(CONFIG_GEOMETRY, 1);
117 goto_state(&st_conf);
118 break;
120 case CONF_GEOLO:
121 goto_state(&st_null);
122 config_set(CONFIG_GEOMETRY, 0);
123 goto_state(&st_conf);
124 break;
126 case CONF_REFON:
127 goto_state(&st_null);
128 config_set(CONFIG_REFLECTION, 1);
129 goto_state(&st_conf);
130 break;
132 case CONF_REFOF:
133 goto_state(&st_null);
134 config_set(CONFIG_REFLECTION, 0);
135 goto_state(&st_conf);
136 break;
138 case CONF_AUDHI:
139 audio_free();
140 config_set(CONFIG_AUDIO_RATE, 44100);
141 config_set(CONFIG_AUDIO_BUFF, AUDIO_BUFF_HI);
142 gui_toggle(audlo_id);
143 gui_toggle(audhi_id);
144 audio_init();
145 break;
147 case CONF_AUDLO:
148 audio_free();
149 config_set(CONFIG_AUDIO_RATE, 22050);
150 config_set(CONFIG_AUDIO_BUFF, AUDIO_BUFF_LO);
151 gui_toggle(audlo_id);
152 gui_toggle(audhi_id);
153 audio_init();
154 break;
156 case CONF_BACK:
157 goto_state(&st_title);
158 break;
160 default:
161 if (100 <= i && i <= 110)
163 int n = i - 100;
165 config_set(CONFIG_SOUND_VOLUME, n);
166 audio_volume(n, m);
167 audio_play(AUD_BUMP, 1.f);
169 gui_toggle(sound_id[n]);
170 gui_toggle(sound_id[s]);
172 if (200 <= i && i <= 210)
174 int n = i - 200;
176 config_set(CONFIG_MUSIC_VOLUME, n);
177 audio_volume(m, n);
178 audio_play(AUD_BUMP, 1.f);
180 gui_toggle(music_id[n]);
181 gui_toggle(music_id[m]);
185 return r;
188 static int conf_enter(void)
190 int id, jd, kd;
192 /* Initialize the configuration GUI. */
194 if ((id = gui_harray(0)))
196 if ((jd = gui_varray(id)))
198 int w = config_get(CONFIG_WIDTH);
199 int t = config_get(CONFIG_TEXTURES);
200 int g = config_get(CONFIG_GEOMETRY);
201 int r = config_get(CONFIG_REFLECTION);
202 int a = config_get(CONFIG_AUDIO_RATE);
203 int s = config_get(CONFIG_SOUND_VOLUME);
204 int m = config_get(CONFIG_MUSIC_VOLUME);
206 if ((kd = gui_harray(jd)))
208 gui_label(kd, "Options", GUI_SML, GUI_ALL, 0, 0);
209 gui_filler(kd);
212 /* Add mode selectors only for existing modes. */
214 if (SDL_VideoModeOK(1600, 1200, 16, SDL_HWSURFACE))
215 gui_state(jd, "1600 x 1200", GUI_SML, CONF_16x12, (w == 1600));
216 if (SDL_VideoModeOK(1280, 1024, 16, SDL_HWSURFACE))
217 gui_state(jd, "1280 x 1024", GUI_SML, CONF_12x10, (w == 1280));
218 if (SDL_VideoModeOK(1024, 768, 16, SDL_HWSURFACE))
219 gui_state(jd, "1024 x 768", GUI_SML, CONF_10x7, (w == 1024));
220 if (SDL_VideoModeOK(800, 600, 16, SDL_HWSURFACE))
221 gui_state(jd, "800 x 600", GUI_SML, CONF_8x6, (w == 800));
222 if (SDL_VideoModeOK(640, 480, 16, SDL_HWSURFACE))
223 gui_state(jd, "640 x 480", GUI_SML, CONF_6x4, (w == 640));
225 if ((kd = gui_harray(jd)))
227 gui_state(kd, "Low", GUI_SML, CONF_TEXLO, (t == 2));
228 gui_state(kd, "High", GUI_SML, CONF_TEXHI, (t == 1));
230 if ((kd = gui_harray(jd)))
232 gui_state(kd, "Low", GUI_SML, CONF_GEOLO, (g == 0));
233 gui_state(kd, "High", GUI_SML, CONF_GEOHI, (g == 1));
235 if ((kd = gui_harray(jd)))
237 gui_state(kd, "Off", GUI_SML, CONF_REFOF, (r == 0));
238 gui_state(kd, "On", GUI_SML, CONF_REFON, (r == 1));
240 if ((kd = gui_harray(jd)))
242 int lo = (a == 22050);
243 int hi = (a == 44100);
245 audlo_id = gui_state(kd, "Low", GUI_SML, CONF_AUDLO, lo);
246 audhi_id = gui_state(kd, "High", GUI_SML, CONF_AUDHI, hi);
248 if ((kd = gui_harray(jd)))
250 /* A series of empty buttons forms the sound volume control. */
252 sound_id[10] = gui_state(kd, " ", GUI_SML, 110, (s == 10));
253 sound_id[ 9] = gui_state(kd, " ", GUI_SML, 109, (s == 9));
254 sound_id[ 8] = gui_state(kd, " ", GUI_SML, 108, (s == 8));
255 sound_id[ 7] = gui_state(kd, " ", GUI_SML, 107, (s == 7));
256 sound_id[ 6] = gui_state(kd, " ", GUI_SML, 106, (s == 6));
257 sound_id[ 5] = gui_state(kd, " ", GUI_SML, 105, (s == 5));
258 sound_id[ 4] = gui_state(kd, " ", GUI_SML, 104, (s == 4));
259 sound_id[ 3] = gui_state(kd, " ", GUI_SML, 103, (s == 3));
260 sound_id[ 2] = gui_state(kd, " ", GUI_SML, 102, (s == 2));
261 sound_id[ 1] = gui_state(kd, " ", GUI_SML, 101, (s == 1));
262 sound_id[ 0] = gui_state(kd, " ", GUI_SML, 100, (s == 0));
264 if ((kd = gui_harray(jd)))
266 /* A series of empty buttons forms the music volume control. */
268 music_id[10] = gui_state(kd, " ", GUI_SML, 210, (m == 10));
269 music_id[ 9] = gui_state(kd, " ", GUI_SML, 209, (m == 9));
270 music_id[ 8] = gui_state(kd, " ", GUI_SML, 208, (m == 8));
271 music_id[ 7] = gui_state(kd, " ", GUI_SML, 207, (m == 7));
272 music_id[ 6] = gui_state(kd, " ", GUI_SML, 206, (m == 6));
273 music_id[ 5] = gui_state(kd, " ", GUI_SML, 205, (m == 5));
274 music_id[ 4] = gui_state(kd, " ", GUI_SML, 204, (m == 4));
275 music_id[ 3] = gui_state(kd, " ", GUI_SML, 203, (m == 3));
276 music_id[ 2] = gui_state(kd, " ", GUI_SML, 202, (m == 2));
277 music_id[ 1] = gui_state(kd, " ", GUI_SML, 201, (m == 1));
278 music_id[ 0] = gui_state(kd, " ", GUI_SML, 200, (m == 0));
281 if ((jd = gui_vstack(id)))
283 int f = config_get(CONFIG_FULLSCREEN);
285 if ((kd = gui_harray(jd)))
287 gui_filler(kd);
288 gui_start(kd, "Back", GUI_SML, CONF_BACK, 0);
291 gui_state(jd, "Fullscreen", GUI_SML, CONF_FULL, (f == 1));
292 gui_state(jd, "Window", GUI_SML, CONF_WIN, (f == 0));
294 /* This filler expands to accomodate an unknown number of modes. */
295 gui_filler(jd);
297 gui_label(jd, "Textures", GUI_SML, GUI_ALL, 0, 0);
298 gui_label(jd, "Geometry", GUI_SML, GUI_ALL, 0, 0);
299 gui_label(jd, "Reflection", GUI_SML, GUI_ALL, 0, 0);
300 gui_label(jd, "Audio", GUI_SML, GUI_ALL, 0, 0);
301 gui_label(jd, "Sound Volume", GUI_SML, GUI_ALL, 0, 0);
302 gui_label(jd, "Music Volume", GUI_SML, GUI_ALL, 0, 0);
304 gui_layout(id, 0, 0);
307 /* Initialize the background. */
309 back_init("png/blues.png", config_get(CONFIG_GEOMETRY));
311 /* Start the title screen music. */
313 audio_music_play("bgm/title.ogg");
315 return id;
318 static void conf_leave(int id)
320 back_free();
321 gui_delete(id);
324 static void conf_paint(int id, float st)
326 config_push_persp((float) config_get(CONFIG_VIEW_FOV), 0.1f, FAR_DIST);
328 back_draw(time_state());
330 config_pop_matrix();
331 gui_paint(id);
334 static void conf_timer(int id, float dt)
336 gui_timer(id, dt);
339 static void conf_point(int id, int x, int y, int dx, int dy)
341 gui_pulse(gui_point(id, x, y), 1.2f);
344 static void conf_stick(int id, int a, int v)
346 if (config_tst(CONFIG_JOYSTICK_AXIS_X, a))
347 gui_pulse(gui_stick(id, v, 0), 1.2f);
348 if (config_tst(CONFIG_JOYSTICK_AXIS_Y, a))
349 gui_pulse(gui_stick(id, 0, v), 1.2f);
352 static int conf_click(int b, int d)
354 if (b < 0 && d == 1)
355 return conf_action(gui_token(gui_click()));
356 return 1;
359 static int conf_keybd(int c, int d)
361 return (d && c == SDLK_ESCAPE) ? goto_state(&st_title) : 1;
364 static int conf_buttn(int b, int d)
366 if (d)
368 if (config_tst(CONFIG_JOYSTICK_BUTTON_A, b))
369 return conf_action(gui_token(gui_click()));
370 if (config_tst(CONFIG_JOYSTICK_BUTTON_B, b))
371 return goto_state(&st_title);
372 if (config_tst(CONFIG_JOYSTICK_BUTTON_EXIT, b))
373 return goto_state(&st_title);
375 return 1;
378 /*---------------------------------------------------------------------------*/
380 static int null_enter(void)
382 hud_free();
383 gui_free();
384 swch_free();
385 jump_free();
386 goal_free();
387 coin_free();
388 ball_free();
390 return 0;
393 static void null_leave(int id)
395 int g = config_get(CONFIG_GEOMETRY);
397 ball_init(g);
398 coin_init(g);
399 goal_init(g);
400 jump_init(g);
401 swch_init(g);
402 gui_init();
403 hud_init();
406 /*---------------------------------------------------------------------------*/
408 struct state st_conf = {
409 conf_enter,
410 conf_leave,
411 conf_paint,
412 conf_timer,
413 conf_point,
414 conf_stick,
415 conf_click,
416 conf_keybd,
417 conf_buttn,
418 1, 0
421 struct state st_null = {
422 null_enter,
423 null_leave,
424 NULL,
425 NULL,
426 NULL,
427 NULL,
428 NULL,
429 NULL,
430 NULL,
431 1, 0