Replay information correct immediately after viewing
[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 #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 lastv = 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 (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(lastv);
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 }
281
282 static void demo_point(int id, int x, int y, int dx, int dy)
283 {
284     int jd = shared_point_basic(id, x, y);
285     int i  = gui_token(jd);
286
287     if (jd && i >= 0)
288     {
289         lastv = i;
290         gui_demo_update_status(i);
291     }
292 }
293
294 static void demo_stick(int id, int a, int v)
295 {
296     int jd = shared_stick_basic(id, a, v);
297     int i  = gui_token(jd);
298
299     if (jd && i >= 0)
300         gui_demo_update_status(i);
301 }
302
303 static int demo_buttn(int b, int d)
304 {
305     if (d)
306     {
307         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
308             return demo_action(total ? gui_token(gui_click()) : GUI_BACK);
309         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
310             return demo_action(GUI_BACK);
311     }
312     return 1;
313 }
314
315 /*---------------------------------------------------------------------------*/
316
317 static int standalone;
318 static int demo_paused;
319 static int show_hud;
320
321 void demo_play_goto(int s)
322 {
323     standalone = s;
324 }
325
326 static int demo_play_enter(void)
327 {
328     int id;
329
330     if (demo_paused)
331     {
332         demo_paused = 0;
333         audio_music_fade_in(0.5f);
334         return 0;
335     }
336
337     if ((id = gui_vstack(0)))
338     {
339         gui_label(id, _("Replay"), GUI_LRG, GUI_ALL, gui_blu, gui_grn);
340         gui_layout(id, 0, 0);
341         gui_pulse(id, 1.2f);
342     }
343
344     show_hud = 1;
345     hud_update(0);
346     game_set_fly(0.f);
347
348     return id;
349 }
350
351 static void demo_play_paint(int id, float t)
352 {
353     game_draw(0, t);
354
355     if (show_hud)
356         hud_paint();
357
358     if (time_state() < 1.f)
359         gui_paint(id);
360 }
361
362 static void demo_play_timer(int id, float dt)
363 {
364     game_step_fade(dt);
365     gui_timer(id, dt);
366     hud_timer(dt);
367
368     /* Spin or skip depending on how fast the demo wants to run. */
369
370     if (!demo_replay_step(dt))
371     {
372         demo_paused = 0;
373         goto_state(&st_demo_end);
374     }
375 }
376
377 static int demo_play_keybd(int c, int d)
378 {
379     if (d)
380     {
381         if (config_tst_d(CONFIG_KEY_PAUSE, c))
382         {
383             demo_paused = 1;
384             return goto_state(&st_demo_end);
385         }
386
387         if (c == SDLK_F6)
388             show_hud = !show_hud;
389     }
390     return 1;
391 }
392
393 static int demo_play_buttn(int b, int d)
394 {
395     if (d)
396     {
397         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
398         {
399             if (!(SDL_GetModState() & KMOD_SHIFT))
400                 demo_paused = 1;
401             return goto_state(&st_demo_end);
402         }
403     }
404     return 1;
405 }
406
407 /*---------------------------------------------------------------------------*/
408
409 #define DEMO_KEEP      0
410 #define DEMO_DEL       1
411 #define DEMO_QUIT      2
412 #define DEMO_REPLAY    3
413 #define DEMO_CONTINUE  4
414
415 static int demo_end_action(int i)
416 {
417     audio_play(AUD_MENU, 1.0f);
418
419     switch (i)
420     {
421     case DEMO_DEL:
422         demo_paused = 0;
423         return goto_state(&st_demo_del);
424     case DEMO_KEEP:
425         demo_paused = 0;
426         demo_replay_stop(0);
427         return goto_state(&st_demo);
428     case DEMO_QUIT:
429         demo_replay_stop(0);
430         return 0;
431     case DEMO_REPLAY:
432         demo_replay_stop(0);
433         level_replay(curr_demo_replay()->filename);
434         return goto_state(&st_demo_play);
435     case DEMO_CONTINUE:
436         return goto_state(&st_demo_play);
437     }
438     return 1;
439 }
440
441 static int demo_end_enter(void)
442 {
443     int id, jd, kd;
444
445     if ((id = gui_vstack(0)))
446     {
447         if (demo_paused)
448             kd = gui_label(id, _("Replay Paused"), GUI_LRG, GUI_ALL,
449                            gui_gry, gui_red);
450         else
451             kd = gui_label(id, _("Replay Ends"),   GUI_LRG, GUI_ALL,
452                            gui_gry, gui_red);
453
454         if ((jd = gui_harray(id)))
455         {
456             int start_id = 0;
457
458             if (standalone)
459             {
460                 start_id = gui_start(jd, _("Quit"), GUI_SML, DEMO_QUIT, 1);
461             }
462             else
463             {
464                 start_id = gui_start(jd, _("Keep"), GUI_SML, DEMO_KEEP, 1);
465                 gui_state(jd, _("Delete"), GUI_SML, DEMO_DEL, 0);
466             }
467
468             if (demo_paused)
469             {
470                 gui_start(jd, _("Continue"), GUI_SML, DEMO_CONTINUE, 1);
471                 gui_toggle(start_id);
472             }
473             else
474                 gui_state(jd, _("Repeat"),   GUI_SML, DEMO_REPLAY,   0);
475         }
476
477         gui_pulse(kd, 1.2f);
478         gui_layout(id, 0, 0);
479     }
480
481     audio_music_fade_out(demo_paused ? 0.2f : 2.0f);
482
483     return id;
484 }
485
486 static void demo_end_paint(int id, float t)
487 {
488     game_draw(0, t);
489     gui_paint(id);
490
491     if (demo_paused)
492         hud_paint();
493 }
494
495 static int demo_end_keybd(int c, int d)
496 {
497     if (d)
498     {
499         if (demo_paused && config_tst_d(CONFIG_KEY_PAUSE, c))
500             return demo_end_action(DEMO_CONTINUE);
501     }
502     return 1;
503 }
504
505 static int demo_end_buttn(int b, int d)
506 {
507     if (d)
508     {
509         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
510             return demo_end_action(gui_token(gui_click()));
511         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
512         {
513             if (demo_paused)
514                 return demo_end_action(DEMO_CONTINUE);
515             else
516                 return demo_end_action(standalone ? DEMO_QUIT : DEMO_KEEP);
517         }
518     }
519     return 1;
520 }
521
522 /*---------------------------------------------------------------------------*/
523
524 static int demo_del_action(int i)
525 {
526     audio_play(AUD_MENU, 1.0f);
527
528     demo_replay_stop(i == DEMO_DEL);
529     return goto_state(&st_demo);
530 }
531
532 static int demo_del_enter(void)
533 {
534     int id, jd, kd;
535
536     if ((id = gui_vstack(0)))
537     {
538         kd = gui_label(id, _("Delete Replay?"), GUI_MED, GUI_ALL, gui_red, gui_red);
539
540         if ((jd = gui_harray(id)))
541         {
542             gui_start(jd, _("No"),  GUI_SML, DEMO_KEEP, 1);
543             gui_state(jd, _("Yes"), GUI_SML, DEMO_DEL,  0);
544         }
545
546         gui_pulse(kd, 1.2f);
547         gui_layout(id, 0, 0);
548     }
549     audio_music_fade_out(2.0f);
550
551     return id;
552 }
553
554 static int demo_del_buttn(int b, int d)
555 {
556     if (d)
557     {
558         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
559             return demo_del_action(gui_token(gui_click()));
560         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
561             return demo_del_action(DEMO_KEEP);
562     }
563     return 1;
564 }
565
566 /*---------------------------------------------------------------------------*/
567
568 struct state st_demo = {
569     demo_enter,
570     shared_leave,
571     shared_paint,
572     demo_timer,
573     demo_point,
574     demo_stick,
575     shared_angle,
576     shared_click,
577     NULL,
578     demo_buttn,
579     1, 0
580 };
581
582 struct state st_demo_play = {
583     demo_play_enter,
584     shared_leave,
585     demo_play_paint,
586     demo_play_timer,
587     NULL,
588     NULL,
589     NULL,
590     NULL,
591     demo_play_keybd,
592     demo_play_buttn,
593     1, 0
594 };
595
596 struct state st_demo_end = {
597     demo_end_enter,
598     shared_leave,
599     demo_end_paint,
600     shared_timer,
601     shared_point,
602     shared_stick,
603     shared_angle,
604     shared_click,
605     demo_end_keybd,
606     demo_end_buttn,
607     1, 0
608 };
609
610 struct state st_demo_del = {
611     demo_del_enter,
612     shared_leave,
613     shared_paint,
614     shared_timer,
615     shared_point,
616     shared_stick,
617     shared_angle,
618     shared_click,
619     NULL,
620     demo_del_buttn,
621     1, 0
622 };