2 * Copyright (C) 2003 Robert Kooima
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.
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.
25 #include "st_shared.h"
30 #include "game_common.h"
31 #include "game_server.h"
32 #include "game_client.h"
37 /*---------------------------------------------------------------------------*/
47 static int last_viewed = 0;
49 /*---------------------------------------------------------------------------*/
51 static int demo_action(int i)
53 audio_play(AUD_MENU, 1.0f);
58 return goto_state(&st_title);
62 return goto_state(&st_demo);
67 return goto_state(&st_demo);
75 if (progress_replay(DEMO_GET(items, i)->filename))
79 return goto_state(&st_demo_play);
86 /*---------------------------------------------------------------------------*/
95 static int gui_demo_thumbs(int id)
97 int w = config_get_d(CONFIG_WIDTH);
98 int h = config_get_d(CONFIG_HEIGHT);
105 if ((jd = gui_varray(id)))
106 for (i = first; i < first + DEMO_STEP; i += DEMO_LINE)
107 if ((kd = gui_harray(jd)))
109 for (j = i + DEMO_LINE - 1; j >= i; j--)
111 thumb = &thumbs[j % DEMO_STEP];
117 if ((ld = gui_vstack(kd)))
121 thumb->shot = gui_image(ld, " ", w / 6, h / 6);
122 thumb->name = gui_state(ld, " ", GUI_SML, j, 0);
124 gui_set_trunc(thumb->name, TRUNC_TAIL);
126 gui_active(ld, j, 0);
142 static void gui_demo_update_thumbs(void)
146 for (i = 0; i < ARRAYSIZE(thumbs) && thumbs[i].shot && thumbs[i].name; i++)
148 gui_set_image(thumbs[i].shot, DEMO_GET(items, thumbs[i].item)->shot);
149 gui_set_label(thumbs[i].name, DEMO_GET(items, thumbs[i].item)->name);
157 static int status_id;
158 static int player_id;
160 static int gui_demo_status(int id)
166 /* Find the longest status string. */
168 for (status = "", s = GAME_NONE; s < GAME_MAX; s++)
169 if (strlen(status_to_str(s)) > strlen(status))
170 status = status_to_str(s);
172 /* Build info bar with dummy values. */
174 if ((jd = gui_hstack(id)))
178 if ((kd = gui_hstack(jd)))
180 if ((ld = gui_vstack(kd)))
184 time_id = gui_clock(ld, 35000, GUI_SML, GUI_NE);
185 coin_id = gui_count(ld, 100, GUI_SML, 0);
186 status_id = gui_label(ld, status, GUI_SML, GUI_SE,
192 if ((ld = gui_vstack(kd)))
196 gui_label(ld, _("Time"), GUI_SML, GUI_NW, gui_wht, gui_wht);
197 gui_label(ld, _("Coins"), GUI_SML, 0, gui_wht, gui_wht);
198 gui_label(ld, _("Status"), GUI_SML, GUI_SW, gui_wht, gui_wht);
206 if ((kd = gui_vstack(jd)))
210 name_id = gui_label(kd, " ", GUI_SML, GUI_NE, 0, 0);
211 player_id = gui_label(kd, " ", GUI_SML, 0, 0, 0);
212 date_id = gui_label(kd, date_to_str(time(NULL)),
213 GUI_SML, GUI_SE, 0, 0);
217 gui_set_trunc(name_id, TRUNC_TAIL);
218 gui_set_trunc(player_id, TRUNC_TAIL);
221 if ((kd = gui_vstack(jd)))
225 gui_label(kd, _("Replay"), GUI_SML, GUI_NW, gui_wht, gui_wht);
226 gui_label(kd, _("Player"), GUI_SML, 0, gui_wht, gui_wht);
227 gui_label(kd, _("Date"), GUI_SML, GUI_SW, gui_wht, gui_wht);
238 static void gui_demo_update_status(int i)
240 const struct demo *d;
243 d = DEMO_GET(items, i < total ? i : 0);
247 gui_set_label(name_id, d->name);
248 gui_set_label(date_id, date_to_str(d->date));
249 gui_set_label(player_id, d->player);
251 if (d->status == GAME_GOAL)
252 gui_set_color(status_id, gui_grn, gui_grn);
254 gui_set_color(status_id, gui_red, gui_red);
256 gui_set_label(status_id, status_to_str(d->status));
257 gui_set_count(coin_id, d->coins);
258 gui_set_clock(time_id, d->timer);
261 /*---------------------------------------------------------------------------*/
263 static int demo_enter(void)
268 demo_dir_free(items);
270 items = demo_dir_scan();
271 total = array_len(items);
277 if ((jd = gui_hstack(id)))
280 gui_label(jd, _("Select Replay"), GUI_SML, GUI_ALL, 0,0);
282 gui_navig(jd, first > 0, first + DEMO_STEP < total);
289 gui_layout(id, 0, 0);
291 gui_demo_update_thumbs();
292 gui_demo_update_status(last_viewed);
296 gui_label(id, _("No Replays"), GUI_MED, GUI_ALL, 0, 0);
297 gui_layout(id, 0, 0);
300 audio_music_fade_to(0.5f, "bgm/inter.ogg");
305 static void demo_timer(int id, float dt)
307 if (total == 0 && time_state() > 4.0f)
308 goto_state(&st_title);
313 static void demo_point(int id, int x, int y, int dx, int dy)
315 int jd = shared_point_basic(id, x, y);
316 int i = gui_token(jd);
318 if (jd && i >= 0 && !GUI_ISMSK(i))
319 gui_demo_update_status(i);
322 static void demo_stick(int id, int a, int v)
324 int jd = shared_stick_basic(id, a, v);
325 int i = gui_token(jd);
327 if (jd && i >= 0 && !GUI_ISMSK(i))
328 gui_demo_update_status(i);
331 static int demo_buttn(int b, int d)
335 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
336 return demo_action(total ? gui_token(gui_click()) : GUI_BACK);
337 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
338 return demo_action(GUI_BACK);
343 /*---------------------------------------------------------------------------*/
345 static int standalone;
346 static int demo_paused;
348 static int check_compat;
350 void demo_play_goto(int s)
356 static int demo_play_enter(void)
363 audio_music_fade_in(0.5f);
368 * Post-1.5.1 replays include view data in the first update, these
369 * two lines are currently left in for compatibility with older
373 game_set_fly(0.f, game_client_file());
374 game_client_step(NULL);
376 if (check_compat && !game_compat_map)
378 goto_state(&st_demo_compat);
382 if ((id = gui_vstack(0)))
384 gui_label(id, _("Replay"), GUI_LRG, GUI_ALL, gui_blu, gui_grn);
385 gui_layout(id, 0, 0);
395 static void demo_play_paint(int id, float t)
402 if (time_state() < 1.f)
406 static void demo_play_timer(int id, float dt)
413 * Introduce a one-second pause at the start of replay playback. (One
414 * second is the time during which the "Replay" label is being displayed.)
415 * HACK ALERT! "id == 0" means we got here from the pause screen, so no
416 * label has been created and there's no need to wait.
419 if (id != 0 && time_state() < 1.0f)
422 /* Spin or skip depending on how fast the demo wants to run. */
424 if (!demo_replay_step(dt))
427 goto_state(&st_demo_end);
433 static int demo_play_keybd(int c, int d)
437 if (config_tst_d(CONFIG_KEY_PAUSE, c))
440 return goto_state(&st_demo_end);
444 show_hud = !show_hud;
449 static int demo_play_buttn(int b, int d)
453 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
455 if (config_tst_d(CONFIG_KEY_PAUSE, SDLK_ESCAPE))
458 return goto_state(&st_demo_end);
464 /*---------------------------------------------------------------------------*/
469 #define DEMO_REPLAY 3
470 #define DEMO_CONTINUE 4
472 static int demo_end_action(int i)
474 audio_play(AUD_MENU, 1.0f);
480 return goto_state(&st_demo_del);
484 return goto_state(&st_demo);
490 progress_replay(curr_demo_replay()->filename);
491 return goto_state(&st_demo_play);
493 return goto_state(&st_demo_play);
498 static int demo_end_enter(void)
502 if ((id = gui_vstack(0)))
505 kd = gui_label(id, _("Replay Paused"), GUI_LRG, GUI_ALL,
508 kd = gui_label(id, _("Replay Ends"), GUI_LRG, GUI_ALL,
511 if ((jd = gui_harray(id)))
517 start_id = gui_start(jd, _("Quit"), GUI_SML, DEMO_QUIT, 1);
521 start_id = gui_start(jd, _("Keep"), GUI_SML, DEMO_KEEP, 1);
522 gui_state(jd, _("Delete"), GUI_SML, DEMO_DEL, 0);
527 gui_start(jd, _("Continue"), GUI_SML, DEMO_CONTINUE, 1);
528 gui_toggle(start_id);
531 gui_state(jd, _("Repeat"), GUI_SML, DEMO_REPLAY, 0);
535 gui_layout(id, 0, 0);
538 audio_music_fade_out(demo_paused ? 0.2f : 2.0f);
543 static void demo_end_paint(int id, float t)
552 static int demo_end_keybd(int c, int d)
556 if (demo_paused && config_tst_d(CONFIG_KEY_PAUSE, c))
557 return demo_end_action(DEMO_CONTINUE);
562 static int demo_end_buttn(int b, int d)
566 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
567 return demo_end_action(gui_token(gui_click()));
569 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
572 return demo_end_action(DEMO_CONTINUE);
574 return demo_end_action(standalone ? DEMO_QUIT : DEMO_KEEP);
580 /*---------------------------------------------------------------------------*/
582 static int demo_del_action(int i)
584 audio_play(AUD_MENU, 1.0f);
586 demo_replay_stop(i == DEMO_DEL);
587 return goto_state(&st_demo);
590 static int demo_del_enter(void)
594 if ((id = gui_vstack(0)))
596 kd = gui_label(id, _("Delete Replay?"), GUI_MED, GUI_ALL, gui_red, gui_red);
598 if ((jd = gui_harray(id)))
600 gui_start(jd, _("No"), GUI_SML, DEMO_KEEP, 1);
601 gui_state(jd, _("Yes"), GUI_SML, DEMO_DEL, 0);
605 gui_layout(id, 0, 0);
607 audio_music_fade_out(2.0f);
612 static int demo_del_buttn(int b, int d)
616 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
617 return demo_del_action(gui_token(gui_click()));
618 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
619 return demo_del_action(DEMO_KEEP);
624 /*---------------------------------------------------------------------------*/
626 static int demo_compat_enter(void)
632 if ((id = gui_vstack(0)))
634 gui_label(id, _("Warning!"), GUI_MED, GUI_ALL, 0, 0);
636 gui_multi(id, _("The current replay was recorded with a\\"
637 "different (or unknown) version of this level.\\"
638 "Be prepared to encounter visual errors.\\"),
639 GUI_SML, GUI_ALL, gui_wht, gui_wht);
641 gui_layout(id, 0, 0);
647 static void demo_compat_timer(int id, float dt)
653 static int demo_compat_buttn(int b, int d)
657 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
658 return goto_state(&st_demo_play);
659 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
660 return goto_state(&st_demo_end);
665 /*---------------------------------------------------------------------------*/
667 struct state st_demo = {
681 struct state st_demo_play = {
695 struct state st_demo_end = {
709 struct state st_demo_del = {
723 struct state st_demo_compat = {