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