Left Shift as the only modifier key.
[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 <string.h>
16
17 #include "gui.h"
18 #include "hud.h"
19 #include "set.h"
20 #include "game.h"
21 #include "demo.h"
22 #include "levels.h"
23 #include "audio.h"
24 #include "solid.h"
25 #include "config.h"
26 #include "st_shared.h"
27 #include "util.h"
28
29 #include "st_demo.h"
30 #include "st_title.h"
31
32 /*---------------------------------------------------------------------------*/
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 GUI_BACK:
52         return goto_state(&st_title);
53
54     case GUI_NEXT:
55         first += DEMO_STEP;
56         return goto_state(&st_demo);
57         break;
58
59     case GUI_PREV:
60         first -= DEMO_STEP;
61         return goto_state(&st_demo);
62         break;
63
64     case GUI_NULL:
65         return 1;
66         break;
67
68     default:
69         if (level_replay(demo_get(i)->filename))
70             demo_play_goto(0);
71             return goto_state(&st_demo_play);
72     }
73     return 1;
74 }
75
76 /*---------------------------------------------------------------------------*/
77
78 static void demo_replay(int id, int i)
79 {
80     int w = config_get_d(CONFIG_WIDTH);
81     int h = config_get_d(CONFIG_HEIGHT);
82     int jd;
83
84     char nam[MAXNAM + 3];
85
86     if ((jd = gui_vstack(id)))
87     {
88         gui_space(jd);
89         gui_image(jd, demo_get(i)->shot, w / 6, h / 6);
90
91         nam[MAXNAM - 1] = '\0';
92         strncpy(nam, demo_get(i)->name, MAXNAM);
93         if (nam[MAXNAM - 1] != '\0')
94         {
95             nam[MAXNAM - 2] = '.';
96             nam[MAXNAM - 1] = '.';
97             nam[MAXNAM + 0] = '.';
98             nam[MAXNAM + 1] = '\0';
99         }
100         gui_state(jd, nam, GUI_SML, i, 0);
101
102         gui_active(jd, i, 0);
103     }
104 }
105
106 static int name_id;
107 static int time_id;
108 static int coin_id;
109 static int date_id;
110 static int mode_id;
111 static int status_id;
112 static int player_id;
113
114 static int gui_demo_status(int id, const struct demo *d)
115 {
116     char noname[MAXNAM];
117     const char *mode, *status;
118     int i, j, k;
119     int jd, kd, ld, md;
120
121     if (d == NULL)
122     {
123         /* Build a long name */
124         memset(noname, 'M', MAXNAM - 1);
125         noname[MAXNAM - 1] = '\0';
126
127         /* Get a long mode */
128         mode = mode_to_str(0, 0);
129         j = strlen(mode);
130
131         for (i = 1; i <= MODE_COUNT; i++)
132         {
133             k = strlen(mode_to_str(i, 0));
134
135             if (k > j)
136             {
137                 j = k;
138                 mode = mode_to_str(i, 0);
139             }
140         }
141
142         /* Get a long status */
143         status = status_to_str(0);
144         j = strlen(status);
145         for (i = 1; i <= GAME_FALL; i++)
146         {
147             k = strlen(status_to_str(i));
148             if (k > j)
149             {
150                 j = k;
151                 status = status_to_str(i);
152             }
153         }
154     }
155     else
156     {
157         mode = mode_to_str(d->mode, 0);
158         status = status_to_str(d->status);
159     }
160
161     if ((jd = gui_hstack(id)))
162     {
163         if ((kd = gui_vstack(jd)))
164         {
165             if ((ld = gui_harray(kd)))
166             {
167                 if ((md = gui_vstack(ld)))
168                 {
169                     player_id = gui_label(md, (d ? d->player : noname),
170                                           GUI_SML, GUI_RGT, 0, 0);
171                     coin_id = gui_count(md, (d ? d->coins : 100),
172                                         GUI_SML, GUI_RGT);
173                     status_id = gui_label(md, status, GUI_SML, GUI_RGT,
174                                          gui_red, gui_red);
175                 }
176                 if ((md = gui_vstack(ld)))
177                 {
178                     gui_label(md, _("Player"), GUI_SML, GUI_LFT,
179                               gui_wht, gui_wht);
180                     gui_label(md, _("Coins"), GUI_SML, GUI_LFT,
181                               gui_wht, gui_wht);
182                     gui_label(md, _("State"), GUI_SML, GUI_LFT,
183                               gui_wht, gui_wht);
184                 }
185                 if ((md = gui_vstack(ld)))
186                 {
187                     name_id = gui_label(md, (d ? d->name : noname),
188                                         GUI_SML, GUI_RGT, 0, 0);
189                     time_id = gui_clock(md, (d ? d->timer : 35000),
190                                         GUI_SML, GUI_RGT);
191                     mode_id = gui_label(md, mode, GUI_SML, GUI_RGT, 0, 0);
192                 }
193             }
194             date_id = gui_label(kd, (d ? date_to_str(d->date) : "M"),
195                                 GUI_SML, GUI_RGT, 0, 0);
196         }
197         if ((kd = gui_vstack(jd)))
198         {
199             gui_label(kd, _("Replay"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
200             gui_label(kd, _("Time"),   GUI_SML, GUI_LFT, gui_wht, gui_wht);
201             gui_label(kd, _("Mode"),   GUI_SML, GUI_LFT, gui_wht, gui_wht);
202             gui_label(kd, _("Date"),   GUI_SML, GUI_LFT, gui_wht, gui_wht);
203         }
204         if (d && d->status == GAME_GOAL)
205             gui_set_color(status_id, gui_grn, gui_grn);
206     }
207     return jd;
208 }
209
210 static void gui_demo_update_status(int i)
211 {
212     const struct demo *d = demo_get(i);
213
214     gui_set_label(name_id,   d->name);
215     gui_set_label(date_id,   date_to_str(d->date));
216     gui_set_label(player_id, d->player);
217     gui_set_label(mode_id,   mode_to_str(d->mode, 0));
218
219     if (d->status == GAME_GOAL)
220         gui_set_color(status_id, gui_grn, gui_grn);
221     else
222         gui_set_color(status_id, gui_red, gui_red);
223
224     gui_set_label(status_id, status_to_str(d->status));
225     gui_set_count(coin_id, d->coins);
226     gui_set_clock(time_id, d->timer);
227 }
228
229 /*---------------------------------------------------------------------------*/
230
231 static int demo_enter(void)
232 {
233     int i, j;
234     int id, jd, kd;
235
236     id = gui_vstack(0);
237
238     if ((total = demo_scan()))
239     {
240         if ((jd = gui_hstack(id)))
241         {
242
243             gui_label(jd, _("Select Replay"), GUI_SML, GUI_ALL, 0,0);
244             gui_filler(jd);
245             gui_back_prev_next(jd, first > 0, first + DEMO_STEP < total);
246         }
247
248         if ((jd = gui_varray(id)))
249             for (i = first; i < first + DEMO_STEP ; i += DEMO_LINE)
250                 if ((kd = gui_harray(jd)))
251                 {
252                     for (j = i + DEMO_LINE - 1; j >= i; j--)
253                         if (j < total)
254                             demo_replay(kd, j);
255                         else
256                             gui_space(kd);
257                 }
258         gui_filler(id);
259         gui_demo_status(id, NULL);
260         gui_layout(id, 0, 0);
261         gui_demo_update_status(0);
262     }
263     else
264     {
265         gui_label(id, _("No Replays"), GUI_MED, GUI_ALL, 0, 0);
266         gui_layout(id, 0, 0);
267     }
268
269     audio_music_fade_to(0.5f, "bgm/inter.ogg");
270
271     return id;
272 }
273
274 static void demo_timer(int id, float dt)
275 {
276     if (total == 0 && time_state() > 4.0f)
277         goto_state(&st_title);
278
279     gui_timer(id, dt);
280     audio_timer(dt);
281 }
282
283 static void demo_point(int id, int x, int y, int dx, int dy)
284 {
285     int jd = shared_point_basic(id, x, y);
286     int i  = gui_token(jd);
287
288     if (jd && i >= 0)
289         gui_demo_update_status(i);
290 }
291
292 static void demo_stick(int id, int a, int v)
293 {
294     int jd = shared_stick_basic(id, a, v);
295     int i  = gui_token(jd);
296
297     if (jd && i >= 0)
298         gui_demo_update_status(i);
299 }
300
301 static int demo_buttn(int b, int d)
302 {
303     if (d)
304     {
305         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
306             return demo_action(total ? gui_token(gui_click()) : GUI_BACK);
307         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
308             return demo_action(GUI_BACK);
309     }
310     return 1;
311 }
312
313 /*---------------------------------------------------------------------------*/
314
315 static int standalone;
316 static int demo_paused;
317
318 void demo_play_goto(int s)
319 {
320     standalone = s;
321 }
322
323 static int demo_play_enter(void)
324 {
325     int id;
326
327     if (demo_paused)
328     {
329         demo_paused = 0;
330         audio_music_fade_in(0.5f);
331         return 0;
332     }
333
334     if ((id = gui_vstack(0)))
335     {
336         gui_label(id, _("Replay"), GUI_LRG, GUI_ALL, gui_blu, gui_grn);
337         gui_layout(id, 0, 0);
338         gui_pulse(id, 1.2f);
339     }
340
341     global_time = -1.f;
342     replay_time =  0.f;
343
344     hud_update(0);
345
346     game_set_fly(0.f);
347
348     return id;
349 }
350
351 static void demo_play_paint(int id, float st)
352 {
353     game_draw(0, st);
354     hud_paint();
355
356     if (time_state() < 1.f)
357         gui_paint(id);
358 }
359
360 static void demo_play_timer(int id, float dt)
361 {
362     float t;
363
364     game_step_fade(dt);
365     gui_timer(id, dt);
366     audio_timer(dt);
367
368     global_time += dt;
369     hud_timer(dt);
370
371     /* Spin or skip depending on how fast the demo wants to run. */
372
373     while (replay_time < global_time)
374         if (demo_replay_step(&t))
375         {
376             replay_time += t;
377         }
378         else
379         {
380             demo_paused = 0;
381             goto_state(&st_demo_end);
382             break;
383         }
384 }
385
386 static int demo_play_keybd(int c, int d)
387 {
388     if (d)
389     {
390         if (config_tst_d(CONFIG_KEY_PAUSE, c))
391         {
392             demo_paused = 1;
393             return goto_state(&st_demo_end);
394         }
395     }
396     return 1;
397 }
398
399 static int demo_play_buttn(int b, int d)
400 {
401     if (d)
402     {
403         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
404         {
405             if (!(SDL_GetModState() & KMOD_SHIFT))
406                 demo_paused = 1;
407             return goto_state(&st_demo_end);
408         }
409     }
410     return 1;
411 }
412
413 /*---------------------------------------------------------------------------*/
414
415 #define DEMO_KEEP      0
416 #define DEMO_DEL       1
417 #define DEMO_QUIT      2
418 #define DEMO_REPLAY    3
419 #define DEMO_CONTINUE  4
420
421 static int demo_end_action(int i)
422 {
423     audio_play(AUD_MENU, 1.0f);
424
425     switch (i)
426     {
427     case DEMO_DEL:
428         demo_paused = 0;
429         return goto_state(&st_demo_del);
430     case DEMO_KEEP:
431         demo_paused = 0;
432         demo_replay_stop(0);
433         return goto_state(&st_demo);
434     case DEMO_QUIT:
435         demo_replay_stop(0);
436         return 0;
437     case DEMO_REPLAY:
438         demo_replay_stop(0);
439         level_replay(curr_demo_replay()->filename);
440         return goto_state(&st_demo_play);
441     case DEMO_CONTINUE:
442         return goto_state(&st_demo_play);
443     }
444     return 1;
445 }
446
447 static int demo_end_enter(void)
448 {
449     int id, jd, kd;
450
451     if ((id = gui_vstack(0)))
452     {
453         if (demo_paused)
454             kd = gui_label(id, _("Replay Paused"), GUI_LRG, GUI_ALL,
455                            gui_gry, gui_red);
456         else
457             kd = gui_label(id, _("Replay Ends"),   GUI_LRG, GUI_ALL,
458                            gui_gry, gui_red);
459
460         if ((jd = gui_harray(id)))
461         {
462             int start_id = 0;
463
464             if (standalone)
465             {
466                 start_id = gui_start(jd, _("Quit"), GUI_SML, DEMO_QUIT, 1);
467             }
468             else
469             {
470                 start_id = gui_start(jd, _("Keep"), GUI_SML, DEMO_KEEP, 1);
471                 gui_state(jd, _("Delete"), GUI_SML, DEMO_DEL, 0);
472             }
473
474             if (demo_paused)
475             {
476                 gui_start(jd, _("Continue"), GUI_SML, DEMO_CONTINUE, 1);
477                 gui_toggle(start_id);
478             }
479             else
480                 gui_state(jd, _("Repeat"),   GUI_SML, DEMO_REPLAY,   0);
481         }
482
483         gui_pulse(kd, 1.2f);
484         gui_layout(id, 0, 0);
485     }
486
487     audio_music_fade_out(demo_paused ? 0.2f : 2.0f);
488
489     return id;
490 }
491
492 static void demo_end_paint(int id, float st)
493 {
494     game_draw(0, st);
495     gui_paint(id);
496
497     if (demo_paused)
498         hud_paint();
499 }
500
501 static int demo_end_keybd(int c, int d)
502 {
503     if (d)
504     {
505         if (demo_paused && config_tst_d(CONFIG_KEY_PAUSE, c))
506             return demo_end_action(DEMO_CONTINUE);
507     }
508     return 1;
509 }
510
511 static int demo_end_buttn(int b, int d)
512 {
513     if (d)
514     {
515         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
516             return demo_end_action(gui_token(gui_click()));
517         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
518         {
519             if (demo_paused)
520                 return demo_end_action(DEMO_CONTINUE);
521             else
522                 return demo_end_action(standalone ? DEMO_QUIT : DEMO_KEEP);
523         }
524     }
525     return 1;
526 }
527
528 /*---------------------------------------------------------------------------*/
529
530 static int demo_del_action(int i)
531 {
532     audio_play(AUD_MENU, 1.0f);
533
534     demo_replay_stop(i == DEMO_DEL);
535     return goto_state(&st_demo);
536 }
537
538 static int demo_del_enter(void)
539 {
540     int id, jd, kd;
541
542     if ((id = gui_vstack(0)))
543     {
544         kd = gui_label(id, _("Delete Replay?"), GUI_MED, GUI_ALL, gui_red, gui_red);
545
546         if ((jd = gui_harray(id)))
547         {
548             gui_start(jd, _("No"),  GUI_SML, DEMO_KEEP, 1);
549             gui_state(jd, _("Yes"), GUI_SML, DEMO_DEL,  0);
550         }
551
552         gui_pulse(kd, 1.2f);
553         gui_layout(id, 0, 0);
554     }
555     audio_music_fade_out(2.0f);
556
557     return id;
558 }
559
560 static int demo_del_buttn(int b, int d)
561 {
562     if (d)
563     {
564         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
565             return demo_del_action(gui_token(gui_click()));
566         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
567             return demo_del_action(DEMO_KEEP);
568     }
569     return 1;
570 }
571
572 /*---------------------------------------------------------------------------*/
573
574 struct state st_demo = {
575     demo_enter,
576     shared_leave,
577     shared_paint,
578     demo_timer,
579     demo_point,
580     demo_stick,
581     shared_click,
582     NULL,
583     demo_buttn,
584     1, 0
585 };
586
587 struct state st_demo_play = {
588     demo_play_enter,
589     shared_leave,
590     demo_play_paint,
591     demo_play_timer,
592     NULL,
593     NULL,
594     NULL,
595     demo_play_keybd,
596     demo_play_buttn,
597     1, 0
598 };
599
600 struct state st_demo_end = {
601     demo_end_enter,
602     shared_leave,
603     demo_end_paint,
604     shared_timer,
605     shared_point,
606     shared_stick,
607     shared_click,
608     demo_end_keybd,
609     demo_end_buttn,
610     1, 0
611 };
612
613 struct state st_demo_del = {
614     demo_del_enter,
615     shared_leave,
616     shared_paint,
617     shared_timer,
618     shared_point,
619     shared_stick,
620     shared_click,
621     NULL,
622     demo_del_buttn,
623     1, 0
624 };