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