Import from neverball-1.3.1.tar.gz
[neverball-archive.git] / ball / st_demo.c
blobb41b82ec5917a14be210605e475e89794411bd03
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 "set.h"
18 #include "game.h"
19 #include "demo.h"
20 #include "level.h"
21 #include "audio.h"
22 #include "solid.h"
23 #include "config.h"
25 #include "st_demo.h"
26 #include "st_title.h"
28 /*---------------------------------------------------------------------------*/
30 #define DEMO_BACK (MAXDEMO+1)
31 #define DEMO_NEXT (MAXDEMO+2)
32 #define DEMO_PREV (MAXDEMO+3)
34 #define DEMO_LINE 4
35 #define DEMO_STEP 8
37 static int first = 0;
38 static int total = 0;
40 static float replay_time;
41 static float global_time;
43 /*---------------------------------------------------------------------------*/
45 static int demo_action(int i)
47 audio_play(AUD_MENU, 1.0f);
49 switch (i)
51 case DEMO_BACK:
52 return goto_state(&st_title);
54 case DEMO_NEXT:
55 first += DEMO_STEP;
56 return goto_state(&st_demo);
57 break;
59 case DEMO_PREV:
60 first -= DEMO_STEP;
61 return goto_state(&st_demo);
62 break;
64 default:
65 if (level_replay(demo_name(i)))
66 return goto_state(&st_demo_play);
68 return 1;
71 static void demo_replay(int id, int i)
73 int w = config_get_d(CONFIG_WIDTH);
74 int h = config_get_d(CONFIG_HEIGHT);
75 int jd;
77 if ((jd = gui_vstack(id)))
79 gui_space(jd);
81 gui_image(jd, demo_shot(i), w / 6, h / 6);
82 gui_state(jd, demo_name(i), GUI_SML, i, 0);
84 gui_clock(jd, demo_clock(i), GUI_SML, GUI_TOP);
85 gui_count(jd, demo_coins(i), GUI_SML, GUI_BOT);
87 gui_active(jd, i, 0);
91 static int demo_enter(void)
93 int i, j;
94 int id, jd, kd, ld;
96 total = demo_scan();
98 if ((id = gui_vstack(0)))
100 if ((jd = gui_hstack(id)))
102 if ((kd = gui_vstack(jd)))
104 gui_filler(kd);
105 if (first + DEMO_STEP < total)
106 gui_state(kd, " Next ", GUI_SML, DEMO_NEXT, 0);
107 else
108 gui_state(kd, " Back ", GUI_SML, DEMO_BACK, 0);
111 gui_filler(jd);
112 ld = gui_label(jd, "Select Replay", GUI_MED, GUI_ALL, 0,0);
113 gui_filler(jd);
115 if ((kd = gui_vstack(jd)))
117 gui_filler(kd);
118 if (first > 0)
119 gui_start(kd, " Prev ", GUI_SML, DEMO_PREV, 0);
120 else
121 gui_start(kd, " Back ", GUI_SML, DEMO_BACK, 0);
124 gui_pulse(ld, 1.2f);
126 if ((jd = gui_varray(id)))
127 for (i = first; i < first + DEMO_STEP && i < total; i += DEMO_LINE)
128 if ((kd = gui_harray(jd)))
130 for (j = i + DEMO_LINE - 1; j >= i; j--)
131 if (j < total)
132 demo_replay(kd, j);
133 else
134 gui_filler(kd);
137 gui_layout(id, 0, 0);
140 audio_music_fade_to(0.5f, "bgm/inter.ogg");
142 return id;
145 static void demo_leave(int id)
147 gui_delete(id);
150 static void demo_paint(int id, float st)
152 game_draw(0, st);
153 config_pop_matrix();
154 gui_paint(id);
157 static void demo_timer(int id, float dt)
159 gui_timer(id, dt);
160 audio_timer(dt);
163 static void demo_point(int id, int x, int y, int dx, int dy)
165 gui_pulse(gui_point(id, x, y), 1.2f);
168 static void demo_stick(int id, int a, int v)
170 if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
171 gui_pulse(gui_stick(id, v, 0), 1.2f);
172 if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
173 gui_pulse(gui_stick(id, 0, v), 1.2f);
176 static int demo_click(int b, int d)
178 if (b <= 0 && d == 1)
179 return demo_action(gui_token(gui_click()));
180 return 1;
183 static int demo_keybd(int c, int d)
185 if (d && c == SDLK_ESCAPE)
186 goto_state(&st_title);
187 return 1;
190 static int demo_buttn(int b, int d)
192 if (d)
194 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
195 return demo_click(0, 1);
196 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
197 return goto_state(&st_title);
199 return 1;
202 /*---------------------------------------------------------------------------*/
204 static int demo_play_enter(void)
206 int id;
208 if ((id = gui_label(0, "Replay", GUI_LRG, GUI_ALL, gui_blu, gui_grn)))
210 gui_layout(id, 0, 0);
211 gui_pulse(id, 1.2f);
214 global_time = -1.f;
215 replay_time = 0.f;
217 game_set_fly(0.f);
219 return id;
222 static void demo_play_leave(int id)
224 gui_delete(id);
227 static void demo_play_paint(int id, float st)
229 game_draw(0, st);
230 hud_paint();
232 if (time_state() < 1.f)
233 gui_paint(id);
236 static void demo_play_timer(int id, float dt)
238 float t;
240 game_step_fade(dt);
241 gui_timer(id, dt);
242 audio_timer(dt);
244 global_time += dt;
246 /* Spin or skip depending on how fast the demo wants to run. */
248 while (replay_time < global_time)
249 if (demo_replay_step(&t))
251 hud_timer(t);
252 replay_time += t;
254 else
256 goto_state(&st_demo_end);
257 break;
261 static int demo_play_keybd(int c, int d)
263 if (d && c == SDLK_ESCAPE)
264 goto_state(&st_demo_end);
265 return 1;
268 static int demo_play_buttn(int b, int d)
270 if (d)
272 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
273 return goto_state(&st_demo_end);
275 return 1;
278 /*---------------------------------------------------------------------------*/
280 #define DEMO_KEEP 0
281 #define DEMO_DEL 1
283 static int demo_end_action(int i)
285 audio_play(AUD_MENU, 1.0f);
287 if (i == DEMO_DEL)
288 return goto_state(&st_demo_del);
290 else
292 demo_replay_stop(0);
293 return goto_state(&st_demo);
297 static int demo_end_enter(void)
299 int id, jd, kd;
301 if ((id = gui_vstack(0)))
303 kd = gui_label(id, "Replay Ends", GUI_MED, GUI_ALL, gui_gry, gui_red);
305 if ((jd = gui_harray(id)))
307 gui_state(jd, "Delete", GUI_SML, DEMO_DEL, 0);
308 gui_start(jd, "Keep", GUI_SML, DEMO_KEEP, 1);
311 gui_pulse(kd, 1.2f);
312 gui_layout(id, 0, 0);
314 audio_music_fade_out(2.0f);
316 return id;
319 static void demo_end_leave(int id)
321 gui_delete(id);
324 static void demo_end_paint(int id, float st)
326 game_draw(0, st);
327 gui_paint(id);
330 static void demo_end_timer(int id, float dt)
332 gui_timer(id, dt);
333 audio_timer(dt);
336 static int demo_end_keybd(int c, int d)
338 return (d && c == SDLK_ESCAPE) ? demo_end_action(DEMO_KEEP) : 1;
341 static void demo_end_point(int id, int x, int y, int dx, int dy)
343 gui_pulse(gui_point(id, x, y), 1.2f);
346 static void demo_end_stick(int id, int a, int v)
348 if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
349 gui_pulse(gui_stick(id, v, 0), 1.2f);
350 if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
351 gui_pulse(gui_stick(id, 0, v), 1.2f);
354 static int demo_end_click(int b, int d)
356 if (d && b < 0)
357 return demo_end_action(gui_token(gui_click()));
358 return 1;
361 static int demo_end_buttn(int b, int d)
363 if (d)
365 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
366 return demo_end_action(gui_token(gui_click()));
367 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
368 return demo_end_action(DEMO_KEEP);
370 return 1;
373 /*---------------------------------------------------------------------------*/
375 static int demo_del_action(int i)
377 audio_play(AUD_MENU, 1.0f);
379 demo_replay_stop(i == DEMO_DEL);
380 return goto_state(&st_demo);
383 static int demo_del_enter(void)
385 int id, jd, kd;
387 if ((id = gui_vstack(0)))
389 kd = gui_label(id, "Delete Replay?", GUI_MED, GUI_ALL, gui_red, gui_red);
391 if ((jd = gui_harray(id)))
393 gui_state(jd, "Yes", GUI_SML, DEMO_DEL, 0);
394 gui_start(jd, "No", GUI_SML, DEMO_KEEP, 1);
397 gui_pulse(kd, 1.2f);
398 gui_layout(id, 0, 0);
400 audio_music_fade_out(2.0f);
402 return id;
405 static void demo_del_leave(int id)
407 gui_delete(id);
410 static void demo_del_paint(int id, float st)
412 game_draw(0, st);
413 gui_paint(id);
416 static void demo_del_timer(int id, float dt)
418 gui_timer(id, dt);
419 audio_timer(dt);
422 static int demo_del_keybd(int c, int d)
424 return (d && c == SDLK_ESCAPE) ? demo_del_action(DEMO_KEEP) : 1;
427 static void demo_del_point(int id, int x, int y, int dx, int dy)
429 gui_pulse(gui_point(id, x, y), 1.2f);
432 static void demo_del_stick(int id, int a, int v)
434 if (config_tst_d(CONFIG_JOYSTICK_AXIS_X, a))
435 gui_pulse(gui_stick(id, v, 0), 1.2f);
436 if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
437 gui_pulse(gui_stick(id, 0, v), 1.2f);
440 static int demo_del_click(int b, int d)
442 if (d && b < 0)
443 return demo_del_action(gui_token(gui_click()));
444 return 1;
447 static int demo_del_buttn(int b, int d)
449 if (d)
451 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
452 return demo_del_action(gui_token(gui_click()));
453 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
454 return demo_del_action(DEMO_KEEP);
456 return 1;
459 /*---------------------------------------------------------------------------*/
461 struct state st_demo = {
462 demo_enter,
463 demo_leave,
464 demo_paint,
465 demo_timer,
466 demo_point,
467 demo_stick,
468 demo_click,
469 demo_keybd,
470 demo_buttn,
474 struct state st_demo_play = {
475 demo_play_enter,
476 demo_play_leave,
477 demo_play_paint,
478 demo_play_timer,
479 NULL,
480 NULL,
481 NULL,
482 demo_play_keybd,
483 demo_play_buttn,
487 struct state st_demo_end = {
488 demo_end_enter,
489 demo_end_leave,
490 demo_end_paint,
491 demo_end_timer,
492 demo_end_point,
493 demo_end_stick,
494 demo_end_click,
495 demo_end_keybd,
496 demo_end_buttn,
497 1, 0
500 struct state st_demo_del = {
501 demo_del_enter,
502 demo_del_leave,
503 demo_del_paint,
504 demo_del_timer,
505 demo_del_point,
506 demo_del_stick,
507 demo_del_click,
508 demo_del_keybd,
509 demo_del_buttn,
510 1, 0