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