Fixed a problem with Makefile.mingw where make wasn't rebuilding targets
[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 static void demo_replay(int id, int i)
77 {
78     int w = config_get_d(CONFIG_WIDTH);
79     int h = config_get_d(CONFIG_HEIGHT);
80     int jd;
81
82     char nam[MAXNAM + 3];
83
84     if ((jd = gui_vstack(id)))
85     {
86         gui_space(jd);
87         gui_image(jd, demo_get(i)->shot, w / 6, h / 6);
88
89         nam[MAXNAM - 1] = '\0';
90         strncpy(nam, demo_get(i)->name, MAXNAM);
91         if (nam[MAXNAM - 1] != '\0')
92         {
93             nam[MAXNAM - 2] = '.';
94             nam[MAXNAM - 1] = '.';
95             nam[MAXNAM + 0] = '.';
96             nam[MAXNAM + 1] = '\0';
97         }
98         gui_state(jd, nam, GUI_SML, i, 0);
99
100         gui_active(jd, i, 0);
101     }
102 }
103
104 static int name_id;
105 static int time_id;
106 static int coin_id;
107 static int date_id;
108 static int mode_id;
109 static int state_id;
110 static int player_id;
111
112 /* Create a layout for some demo info.  If d is NULL, try to reserve enough
113  * space. */
114
115 static int gui_demo_status(int id, const struct demo *d)
116 {
117     char noname[MAXNAM];
118     const char *mode, *state;
119     int i, j, k;
120     int jd, kd, ld, md;
121
122     if (d == NULL)
123     {
124         /* Build a long name */
125         memset(noname, 'M', MAXNAM - 1);
126         noname[MAXNAM - 1] = '\0';
127
128         /* Get a long mode */
129         mode = mode_to_str(0, 0);
130         j = strlen(mode);
131         for (i = 1; i <= MODE_SINGLE; i++)
132         {
133             k = strlen(mode_to_str(i, 0));
134             if (k > j)
135             {
136                 j = k;
137                 mode = mode_to_str(i, 0);
138             }
139         }
140
141         /* Get a long state */
142         state = state_to_str(0);
143         j = strlen(state);
144         for (i = 1; i <= GAME_FALL; i++)
145         {
146             k = strlen(state_to_str(i));
147             if (k > j)
148             {
149                 j = k;
150                 state = state_to_str(i);
151             }
152         }
153     }
154     else
155     {
156         mode = mode_to_str(d->mode, 0);
157         state = state_to_str(d->state);
158     }
159
160     if ((jd = gui_hstack(id)))
161     {
162         if ((kd = gui_vstack(jd)))
163         {
164             if ((ld = gui_harray(kd)))
165             {
166                 if ((md = gui_vstack(ld)))
167                 {
168                     player_id = gui_label(md, (d ? d->player : noname),
169                                           GUI_SML, GUI_RGT, 0, 0);
170                     coin_id = gui_count(md, (d ? d->coins : 100),
171                                         GUI_SML, GUI_RGT);
172                     state_id = gui_label(md, state, GUI_SML, GUI_RGT,
173                                          gui_red, gui_red);
174                 }
175                 if ((md = gui_vstack(ld)))
176                 {
177                     gui_label(md, _("Player"), GUI_SML, GUI_LFT,
178                               gui_wht, gui_wht);
179                     gui_label(md, _("Coins"), GUI_SML, GUI_LFT,
180                               gui_wht, gui_wht);
181                     gui_label(md, _("State"), GUI_SML, GUI_LFT,
182                               gui_wht, gui_wht);
183                 }
184                 if ((md = gui_vstack(ld)))
185                 {
186                     name_id = gui_label(md, (d ? d->name : noname),
187                                         GUI_SML, GUI_RGT, 0, 0);
188                     time_id = gui_clock(md, (d ? d->timer : 35000),
189                                         GUI_SML, GUI_RGT);
190                     mode_id = gui_label(md, mode, GUI_SML, GUI_RGT, 0, 0);
191                 }
192             }
193             date_id = gui_label(kd, (d ? date_to_str(d->date) : "M"),
194                                 GUI_SML, GUI_RGT, 0, 0);
195         }
196         if ((kd = gui_vstack(jd)))
197         {
198             gui_label(kd, _("Replay"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
199             gui_label(kd, _("Time"),   GUI_SML, GUI_LFT, gui_wht, gui_wht);
200             gui_label(kd, _("Mode"),   GUI_SML, GUI_LFT, gui_wht, gui_wht);
201             gui_label(kd, _("Date"),   GUI_SML, GUI_LFT, gui_wht, gui_wht);
202         }
203         if (d && (d->state == GAME_GOAL || d->state == GAME_SPEC))
204             gui_set_color(state_id, gui_grn, gui_grn);
205     }
206     return jd;
207 }
208
209 static void gui_demo_update_status(int i)
210 {
211     const struct demo *d = demo_get(i);
212
213     gui_set_label(name_id,   d->name);
214     gui_set_label(date_id,   date_to_str(d->date));
215     gui_set_label(player_id, d->player);
216     gui_set_label(mode_id,   mode_to_str(d->mode, 0));
217
218     if (d->state == GAME_GOAL || d->state == GAME_SPEC)
219         gui_set_color(state_id, gui_grn, gui_grn);
220     else
221         gui_set_color(state_id, gui_red, gui_red);
222
223     gui_set_label(state_id, state_to_str(d->state));
224     gui_set_count(coin_id, d->coins);
225     gui_set_clock(time_id, d->timer);
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_filler(id);
264         gui_start(id, _("Back"), GUI_SML, GUI_BACK, 0);
265         gui_layout(id, 0, 0);
266     }
267
268     audio_music_fade_to(0.5f, "bgm/inter.ogg");
269
270     return id;
271 }
272
273 static void demo_point(int id, int x, int y, int dx, int dy)
274 {
275     int jd = shared_point_basic(id, x, y);
276     int i  = gui_token(jd);
277
278     if (jd && i >= 0)
279         gui_demo_update_status(i);
280 }
281
282 static void demo_stick(int id, int a, int v)
283 {
284     int jd = shared_stick_basic(id, a, v);
285     int i  = gui_token(jd);
286
287     if (jd && i >= 0)
288         gui_demo_update_status(i);
289 }
290
291 static int demo_buttn(int b, int d)
292 {
293     if (d)
294     {
295         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
296             return demo_action(gui_token(gui_click()));
297         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
298             return demo_action(GUI_BACK);
299     }
300     return 1;
301 }
302
303 /*---------------------------------------------------------------------------*/
304
305 static int simple_play; /* play demo from command line */
306
307 void demo_play_goto(int simple)
308 {
309     simple_play = simple;
310 }
311
312 static int demo_play_enter(void)
313 {
314     int id;
315
316     if ((id = gui_vstack(0)))
317     {
318         gui_label(id, _("Replay"), GUI_LRG, GUI_ALL, gui_blu, gui_grn);
319         gui_layout(id, 0, 0);
320         gui_pulse(id, 1.2f);
321     }
322
323     global_time = -1.f;
324     replay_time =  0.f;
325
326     hud_update(0);
327
328     game_set_fly(0.f);
329
330     return id;
331 }
332
333 static void demo_play_paint(int id, float st)
334 {
335     game_draw(0, st);
336     hud_paint();
337
338     if (time_state() < 1.f)
339         gui_paint(id);
340 }
341
342 static void demo_play_timer(int id, float dt)
343 {
344     float t;
345
346     game_step_fade(dt);
347     gui_timer(id, dt);
348     audio_timer(dt);
349
350     global_time += dt;
351     hud_timer(dt);
352
353     /* Spin or skip depending on how fast the demo wants to run. */
354
355     while (replay_time < global_time)
356         if (demo_replay_step(&t))
357         {
358             replay_time += t;
359         }
360         else
361         {
362             goto_state(&st_demo_end);
363             break;
364         }
365 }
366
367 static int demo_play_buttn(int b, int d)
368 {
369     if (d)
370     {
371         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
372             return goto_state(&st_demo_end);
373     }
374     return 1;
375 }
376
377 /*---------------------------------------------------------------------------*/
378
379 #define DEMO_KEEP    0
380 #define DEMO_DEL     1
381 #define DEMO_QUIT    2
382 #define DEMO_REPLAY  3
383
384 static int demo_end_action(int i)
385 {
386     audio_play(AUD_MENU, 1.0f);
387
388     switch (i)
389     {
390     case DEMO_DEL:
391         return goto_state(&st_demo_del);
392     case DEMO_KEEP:
393         demo_replay_stop(0);
394         return goto_state(&st_demo);
395     case DEMO_QUIT:
396         demo_replay_stop(0);
397         return 0;
398     case DEMO_REPLAY:
399         demo_replay_stop(0);
400         level_replay(curr_demo_replay()->filename);
401         return goto_state(&st_demo_play);
402     }
403     return 1;
404 }
405
406 static int demo_end_enter(void)
407 {
408     int id, jd, kd;
409
410     if ((id = gui_vstack(0)))
411     {
412         kd = gui_label(id, _("Replay Ends"), GUI_LRG, GUI_ALL, gui_gry, gui_red);
413
414         if ((jd = gui_harray(id)))
415         {
416             gui_start(jd, _("Replay Again"), GUI_SML, DEMO_REPLAY, 0);
417
418             if (simple_play)
419                 gui_start(jd, _("OK"),       GUI_SML, DEMO_QUIT,   1);
420             else
421             {
422                 gui_start(jd, _("Keep"),     GUI_SML, DEMO_KEEP,   1);
423                 gui_state(jd, _("Delete"),   GUI_SML, DEMO_DEL,    0);
424             }
425         }
426
427         gui_filler(id);
428
429         if ((jd = gui_hstack(id)))
430         {
431             gui_filler(jd);
432             gui_demo_status(jd, curr_demo_replay());
433             gui_filler(jd);
434         }
435
436         gui_pulse(kd, 1.2f);
437         gui_layout(id, 0, 0);
438     }
439     audio_music_fade_out(2.0f);
440
441     return id;
442 }
443
444 static void demo_end_timer(int id, float dt)
445 {
446     float gg[3] = { 0.0f,  9.8f, 0.0f };
447     float gf[3] = { 0.0f, -9.8f, 0.0f };
448     int state = curr_demo_replay()->state;
449
450     if (time_state() < 2.f)
451     {
452         if (replay_time < global_time)
453         {
454             if (state != GAME_NONE && state != GAME_TIME)
455                 game_step(state == GAME_GOAL ? gg : gf, dt, NULL);
456         }
457     }
458
459     gui_timer(id, dt);
460     audio_timer(dt);
461 }
462
463 static int demo_end_buttn(int b, int d)
464 {
465     if (d)
466     {
467         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
468             return demo_end_action(gui_token(gui_click()));
469         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
470             return demo_end_action(simple_play ? DEMO_QUIT : DEMO_KEEP);
471     }
472     return 1;
473 }
474
475 /*---------------------------------------------------------------------------*/
476
477 static int demo_del_action(int i)
478 {
479     audio_play(AUD_MENU, 1.0f);
480
481     demo_replay_stop(i == DEMO_DEL);
482     return goto_state(&st_demo);
483 }
484
485 static int demo_del_enter(void)
486 {
487     int id, jd, kd;
488
489     if ((id = gui_vstack(0)))
490     {
491         kd = gui_label(id, _("Delete Replay?"), GUI_MED, GUI_ALL, gui_red, gui_red);
492
493         if ((jd = gui_harray(id)))
494         {
495             gui_start(jd, _("No"),  GUI_SML, DEMO_KEEP, 1);
496             gui_state(jd, _("Yes"), GUI_SML, DEMO_DEL,  0);
497         }
498
499         gui_pulse(kd, 1.2f);
500         gui_layout(id, 0, 0);
501     }
502     audio_music_fade_out(2.0f);
503
504     return id;
505 }
506
507 static int demo_del_buttn(int b, int d)
508 {
509     if (d)
510     {
511         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
512             return demo_del_action(gui_token(gui_click()));
513         if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
514             return demo_del_action(DEMO_KEEP);
515     }
516     return 1;
517 }
518
519 /*---------------------------------------------------------------------------*/
520
521 struct state st_demo = {
522     demo_enter,
523     shared_leave,
524     shared_paint,
525     shared_timer,
526     demo_point,
527     demo_stick,
528     shared_click,
529     NULL,
530     demo_buttn,
531     0
532 };
533
534 struct state st_demo_play = {
535     demo_play_enter,
536     shared_leave,
537     demo_play_paint,
538     demo_play_timer,
539     NULL,
540     NULL,
541     NULL,
542     NULL,
543     demo_play_buttn,
544     0
545 };
546
547 struct state st_demo_end = {
548     demo_end_enter,
549     shared_leave,
550     shared_paint,
551     demo_end_timer,
552     shared_point,
553     shared_stick,
554     shared_click,
555     NULL,
556     demo_end_buttn,
557     1, 0
558 };
559
560 struct state st_demo_del = {
561     demo_del_enter,
562     shared_leave,
563     shared_paint,
564     shared_timer,
565     shared_point,
566     shared_stick,
567     shared_click,
568     NULL,
569     demo_del_buttn,
570     1, 0
571 };