fix translation
[neverball] / ball / st_demo.c
1 /*   
2  * Copyright (C) 2003 Robert Kooima
3  *
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.
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 "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"
24
25 #include "st_demo.h"
26 #include "st_title.h"
27
28 /*---------------------------------------------------------------------------*/
29
30 #define DEMO_BACK (MAXDEMO+1)
31 #define DEMO_NEXT (MAXDEMO+2)
32 #define DEMO_PREV (MAXDEMO+3)
33
34 #define DEMO_LINE 4
35 #define DEMO_STEP 8
36
37 static int first = 0;
38 static int total = 0;
39
40 static float replay_time;
41 static float global_time;
42
43 /*---------------------------------------------------------------------------*/
44
45 static int demo_action(int i)
46 {
47     audio_play(AUD_MENU, 1.0f);
48
49     switch (i)
50     {
51     case DEMO_BACK:
52         return goto_state(&st_title);
53
54     case DEMO_NEXT:
55         first += DEMO_STEP;
56         return goto_state(&st_demo);
57         break;
58
59     case DEMO_PREV:
60         first -= DEMO_STEP;
61         return goto_state(&st_demo);
62         break;
63
64     default:
65         if (level_replay(demo_name(i)))
66             return goto_state(&st_demo_play);
67     }
68     return 1;
69 }
70
71 static void demo_replay(int id, int i)
72 {
73     int w = config_get_d(CONFIG_WIDTH);
74     int h = config_get_d(CONFIG_HEIGHT);
75     int jd;
76
77     if ((jd = gui_vstack(id)))
78     {
79         gui_space(jd);
80
81         gui_image(jd, demo_shot(i), w / 6, h / 6);
82         gui_state(jd, demo_name(i), GUI_SML, i, 0);
83
84         gui_clock(jd, demo_clock(i), GUI_SML, GUI_TOP);
85         gui_count(jd, demo_coins(i), GUI_SML, GUI_BOT);
86
87         gui_active(jd, i, 0);
88     }
89 }
90
91 static int demo_enter(void)
92 {
93     int i, j;
94     int id, jd, kd, ld;
95
96     total = demo_scan();
97
98     if ((id = gui_vstack(0)))
99     {
100         if ((jd = gui_hstack(id)))
101         {
102             if ((kd = gui_vstack(jd)))
103             {
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);
109             }
110
111             gui_filler(jd);
112             ld = gui_label(jd, _("Select Replay"), GUI_MED, GUI_ALL, 0,0);
113             gui_filler(jd);
114
115             if ((kd = gui_vstack(jd)))
116             {
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);
122             }
123
124             gui_pulse(ld, 1.2f);
125         }
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)))
129                 {
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);
135                 }
136
137         gui_layout(id, 0, 0);
138     }
139
140     audio_music_fade_to(0.5f, "bgm/inter.ogg");
141
142     return id;
143 }
144
145 static void demo_leave(int id)
146 {
147     gui_delete(id);
148 }
149
150 static void demo_paint(int id, float st)
151 {
152     game_draw(0, st);
153     config_pop_matrix();
154     gui_paint(id);
155 }
156
157 static void demo_timer(int id, float dt)
158 {
159     gui_timer(id, dt);
160     audio_timer(dt);
161 }
162
163 static void demo_point(int id, int x, int y, int dx, int dy)
164 {
165     gui_pulse(gui_point(id, x, y), 1.2f);
166 }
167
168 static void demo_stick(int id, int a, int v)
169 {
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);
174 }
175
176 static int demo_click(int b, int d)
177 {
178     if (b <= 0 && d == 1)
179         return demo_action(gui_token(gui_click()));
180     return 1;
181 }
182
183 static int demo_keybd(int c, int d)
184 {
185     if (d && c == SDLK_ESCAPE)
186         goto_state(&st_title);
187     return 1;
188 }
189
190 static int demo_buttn(int b, int d)
191 {
192     if (d)
193     {
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);
198     }
199     return 1;
200 }
201
202 /*---------------------------------------------------------------------------*/
203
204 static int demo_play_enter(void)
205 {
206     int id;
207
208     if ((id = gui_label(0, _("Replay"), GUI_LRG, GUI_ALL, gui_blu, gui_grn)))
209     {
210         gui_layout(id, 0, 0);
211         gui_pulse(id, 1.2f);
212     }
213
214     global_time = -1.f;
215     replay_time =  0.f;
216
217     game_set_fly(0.f);
218
219     return id;
220 }
221
222 static void demo_play_leave(int id)
223 {
224     gui_delete(id);
225 }
226
227 static void demo_play_paint(int id, float st)
228 {
229     game_draw(0, st);
230     hud_paint();
231
232     if (time_state() < 1.f)
233         gui_paint(id);
234 }
235
236 static void demo_play_timer(int id, float dt)
237 {
238     float t;
239
240     game_step_fade(dt);
241     gui_timer(id, dt);
242     audio_timer(dt);
243
244     global_time += dt;
245
246     /* Spin or skip depending on how fast the demo wants to run. */
247
248     while (replay_time < global_time)
249         if (demo_replay_step(&t))
250         {
251             hud_timer(t);
252             replay_time += t;
253         }
254         else 
255         {
256             goto_state(&st_demo_end);
257             break;
258         }
259 }
260
261 static int demo_play_keybd(int c, int d)
262 {
263     if (d && c == SDLK_ESCAPE)
264         goto_state(&st_demo_end);
265     return 1;
266 }
267
268 static int demo_play_buttn(int b, int d)
269 {
270     if (d)
271     {
272         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
273             return goto_state(&st_demo_end);
274     }
275     return 1;
276 }
277
278 /*---------------------------------------------------------------------------*/
279
280 #define DEMO_KEEP 0
281 #define DEMO_DEL  1
282
283 static int demo_end_action(int i)
284 {
285     audio_play(AUD_MENU, 1.0f);
286
287     if (i == DEMO_DEL)
288         return goto_state(&st_demo_del);
289
290     else
291     {
292         demo_replay_stop(0);
293         return goto_state(&st_demo);
294     }
295 }
296
297 static int demo_end_enter(void)
298 {
299     int id, jd, kd;
300
301     if ((id = gui_vstack(0)))
302     {
303         kd = gui_label(id, _("Replay Ends"), GUI_MED, GUI_ALL, gui_gry, gui_red);
304
305         if ((jd = gui_harray(id)))
306         {
307             gui_state(jd, _("Delete"), GUI_SML, DEMO_DEL,  0);
308             gui_start(jd, _("Keep"),   GUI_SML, DEMO_KEEP, 1);
309         }
310
311         gui_pulse(kd, 1.2f);
312         gui_layout(id, 0, 0);
313     }
314     audio_music_fade_out(2.0f);
315
316     return id;
317 }
318
319 static void demo_end_leave(int id)
320 {
321     gui_delete(id);
322 }
323
324 static void demo_end_paint(int id, float st)
325 {
326     game_draw(0, st);
327     gui_paint(id);
328 }
329
330 static void demo_end_timer(int id, float dt)
331 {
332     gui_timer(id, dt);
333     audio_timer(dt);
334 }
335
336 static int demo_end_keybd(int c, int d)
337 {
338     return (d && c == SDLK_ESCAPE) ? demo_end_action(DEMO_KEEP) : 1;
339 }
340
341 static void demo_end_point(int id, int x, int y, int dx, int dy)
342 {
343     gui_pulse(gui_point(id, x, y), 1.2f);
344 }
345
346 static void demo_end_stick(int id, int a, int v)
347 {
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);
352 }
353
354 static int demo_end_click(int b, int d)
355 {
356     if (d && b < 0)
357         return demo_end_action(gui_token(gui_click()));
358     return 1;
359 }
360
361 static int demo_end_buttn(int b, int d)
362 {
363     if (d)
364     {
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);
369     }
370     return 1;
371 }
372
373 /*---------------------------------------------------------------------------*/
374
375 static int demo_del_action(int i)
376 {
377     audio_play(AUD_MENU, 1.0f);
378
379     demo_replay_stop(i == DEMO_DEL);
380     return goto_state(&st_demo);
381 }
382
383 static int demo_del_enter(void)
384 {
385     int id, jd, kd;
386
387     if ((id = gui_vstack(0)))
388     {
389         kd = gui_label(id, _("Delete Replay?"), GUI_MED, GUI_ALL, gui_red, gui_red);
390
391         if ((jd = gui_harray(id)))
392         {
393             gui_state(jd, _("Yes"), GUI_SML, DEMO_DEL,  0);
394             gui_start(jd, _("No"),  GUI_SML, DEMO_KEEP, 1);
395         }
396
397         gui_pulse(kd, 1.2f);
398         gui_layout(id, 0, 0);
399     }
400     audio_music_fade_out(2.0f);
401
402     return id;
403 }
404
405 static void demo_del_leave(int id)
406 {
407     gui_delete(id);
408 }
409
410 static void demo_del_paint(int id, float st)
411 {
412     game_draw(0, st);
413     gui_paint(id);
414 }
415
416 static void demo_del_timer(int id, float dt)
417 {
418     gui_timer(id, dt);
419     audio_timer(dt);
420 }
421
422 static int demo_del_keybd(int c, int d)
423 {
424     return (d && c == SDLK_ESCAPE) ? demo_del_action(DEMO_KEEP) : 1;
425 }
426
427 static void demo_del_point(int id, int x, int y, int dx, int dy)
428 {
429     gui_pulse(gui_point(id, x, y), 1.2f);
430 }
431
432 static void demo_del_stick(int id, int a, int v)
433 {
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);
438 }
439
440 static int demo_del_click(int b, int d)
441 {
442     if (d && b < 0)
443         return demo_del_action(gui_token(gui_click()));
444     return 1;
445 }
446
447 static int demo_del_buttn(int b, int d)
448 {
449     if (d)
450     {
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);
455     }
456     return 1;
457 }
458
459 /*---------------------------------------------------------------------------*/
460
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,
471     0
472 };
473
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,
484     0
485 };
486
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
498 };
499
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
511 };