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.
29 #include "game_common.h"
30 #include "game_server.h"
31 #include "game_client.h"
35 #include "st_shared.h"
37 /*---------------------------------------------------------------------------*/
48 static int last_viewed = 0;
50 /*---------------------------------------------------------------------------*/
52 static int demo_action(int i)
54 audio_play(AUD_MENU, 1.0f);
59 return goto_state(&st_title);
63 return goto_state(&st_demo);
68 return goto_state(&st_demo);
76 if (progress_replay(DIR_ITEM_GET(items, i)->path))
80 return goto_state(&st_demo_play);
87 /*---------------------------------------------------------------------------*/
96 static int gui_demo_thumbs(int id)
98 int w = config_get_d(CONFIG_WIDTH);
99 int h = config_get_d(CONFIG_HEIGHT);
106 if ((jd = gui_varray(id)))
107 for (i = first; i < first + DEMO_STEP; i += DEMO_LINE)
108 if ((kd = gui_harray(jd)))
110 for (j = i + DEMO_LINE - 1; j >= i; j--)
112 thumb = &thumbs[j % DEMO_STEP];
118 if ((ld = gui_vstack(kd)))
122 thumb->shot = gui_image(ld, " ", w / 6, h / 6);
123 thumb->name = gui_state(ld, " ", GUI_SML, j, 0);
125 gui_set_trunc(thumb->name, TRUNC_TAIL);
127 gui_active(ld, j, 0);
143 static void gui_demo_update_thumbs(void)
145 struct dir_item *item;
149 for (i = 0; i < ARRAYSIZE(thumbs) && thumbs[i].shot && thumbs[i].name; i++)
151 item = DIR_ITEM_GET(items, thumbs[i].item);
154 gui_set_image(thumbs[i].shot, demo ? demo->shot : "");
155 gui_set_label(thumbs[i].name, demo ? demo->name : base_name(item->path));
163 static int status_id;
164 static int player_id;
166 static int gui_demo_status(int id)
172 /* Find the longest status string. */
174 for (status = "", s = GAME_NONE; s < GAME_MAX; s++)
175 if (strlen(status_to_str(s)) > strlen(status))
176 status = status_to_str(s);
178 /* Build info bar with dummy values. */
180 if ((jd = gui_hstack(id)))
184 if ((kd = gui_hstack(jd)))
186 if ((ld = gui_vstack(kd)))
190 time_id = gui_clock(ld, 35000, GUI_SML, GUI_NE);
191 coin_id = gui_count(ld, 100, GUI_SML, 0);
192 status_id = gui_label(ld, status, GUI_SML, GUI_SE,
198 if ((ld = gui_vstack(kd)))
202 gui_label(ld, _("Time"), GUI_SML, GUI_NW, gui_wht, gui_wht);
203 gui_label(ld, _("Coins"), GUI_SML, 0, gui_wht, gui_wht);
204 gui_label(ld, _("Status"), GUI_SML, GUI_SW, gui_wht, gui_wht);
212 if ((kd = gui_vstack(jd)))
216 name_id = gui_label(kd, " ", GUI_SML, GUI_NE, 0, 0);
217 player_id = gui_label(kd, " ", GUI_SML, 0, 0, 0);
218 date_id = gui_label(kd, date_to_str(time(NULL)),
219 GUI_SML, GUI_SE, 0, 0);
223 gui_set_trunc(name_id, TRUNC_TAIL);
224 gui_set_trunc(player_id, TRUNC_TAIL);
227 if ((kd = gui_vstack(jd)))
231 gui_label(kd, _("Replay"), GUI_SML, GUI_NW, gui_wht, gui_wht);
232 gui_label(kd, _("Player"), GUI_SML, 0, gui_wht, gui_wht);
233 gui_label(kd, _("Date"), GUI_SML, GUI_SW, gui_wht, gui_wht);
244 static void gui_demo_update_status(int i)
246 const struct demo *d;
251 d = DEMO_GET(items, i < total ? i : 0);
256 gui_set_label(name_id, d->name);
257 gui_set_label(date_id, date_to_str(d->date));
258 gui_set_label(player_id, d->player);
260 if (d->status == GAME_GOAL)
261 gui_set_color(status_id, gui_grn, gui_grn);
263 gui_set_color(status_id, gui_red, gui_red);
265 gui_set_label(status_id, status_to_str(d->status));
266 gui_set_count(coin_id, d->coins);
267 gui_set_clock(time_id, d->timer);
270 /*---------------------------------------------------------------------------*/
272 static int demo_gui(void)
280 if ((jd = gui_hstack(id)))
283 gui_label(jd, _("Select Replay"), GUI_SML, GUI_ALL, 0,0);
285 gui_navig(jd, first > 0, first + DEMO_STEP < total);
292 gui_layout(id, 0, 0);
294 gui_demo_update_thumbs();
295 gui_demo_update_status(last_viewed);
299 gui_label(id, _("No Replays"), GUI_MED, GUI_ALL, 0, 0);
300 gui_layout(id, 0, 0);
306 static int demo_enter(struct state *st, struct state *prev)
308 if (!items || (prev == &st_demo_del))
312 demo_dir_free(items);
316 items = demo_dir_scan();
317 total = array_len(items);
320 first = first < total ? first : 0;
321 last = MIN(first + DEMO_STEP - 1, total - 1);
322 last_viewed = MIN(MAX(first, last_viewed), last);
325 demo_dir_load(items, first, last);
327 audio_music_fade_to(0.5f, "bgm/inter.ogg");
332 static void demo_leave(struct state *st, struct state *next, int id)
334 if (next == &st_title)
336 demo_dir_free(items);
343 static void demo_timer(int id, float dt)
345 if (total == 0 && time_state() > 4.0f)
346 goto_state(&st_title);
351 static void demo_point(int id, int x, int y, int dx, int dy)
353 int jd = shared_point_basic(id, x, y);
354 int i = gui_token(jd);
356 if (jd && i >= 0 && !GUI_ISMSK(i))
357 gui_demo_update_status(i);
360 static void demo_stick(int id, int a, float v, int bump)
362 int jd = shared_stick_basic(id, a, v, bump);
363 int i = gui_token(jd);
365 if (jd && i >= 0 && !GUI_ISMSK(i))
366 gui_demo_update_status(i);
369 static int demo_buttn(int b, int d)
373 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
374 return demo_action(total ? gui_token(gui_click()) : GUI_BACK);
375 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
376 return demo_action(GUI_BACK);
381 /*---------------------------------------------------------------------------*/
383 static int standalone;
384 static int demo_paused;
386 static int check_compat;
389 static float prelude;
391 void demo_play_goto(int s)
397 static int demo_play_gui(void)
401 if ((id = gui_vstack(0)))
403 gui_label(id, _("Replay"), GUI_LRG, GUI_ALL, gui_blu, gui_grn);
404 gui_layout(id, 0, 0);
411 static int demo_play_enter(struct state *st, struct state *prev)
417 audio_music_fade_in(0.5f);
422 * Post-1.5.1 replays include view data in the first update, this
423 * line is currently left in for compatibility with older replays.
425 game_client_fly(0.0f);
427 if (check_compat && !game_compat_map)
429 goto_state(&st_demo_compat);
435 speed = SPEED_NORMAL;
436 demo_speed_set(speed);
441 return demo_play_gui();
444 static void demo_play_paint(int id, float t)
446 game_client_draw(0, t);
451 if (time_state() < prelude)
455 static void demo_play_timer(int id, float dt)
461 /* Pause briefly before starting playback. */
463 if (time_state() < prelude)
466 if (!demo_replay_step(dt))
469 goto_state(&st_demo_end);
474 game_client_blend(demo_replay_blend());
478 static void set_speed(int d)
480 if (d > 0) speed = SPEED_UP(speed);
481 if (d < 0) speed = SPEED_DN(speed);
483 demo_speed_set(speed);
484 hud_speed_pulse(speed);
487 static void demo_play_stick(int id, int a, float v, int bump)
492 if (config_tst_d(CONFIG_JOYSTICK_AXIS_Y, a))
494 if (v < 0) set_speed(+1);
495 if (v > 0) set_speed(-1);
499 static int demo_play_click(int b, int d)
503 if (b == SDL_BUTTON_WHEELUP) set_speed(+1);
504 if (b == SDL_BUTTON_WHEELDOWN) set_speed(-1);
510 static int demo_play_keybd(int c, int d)
514 if (config_tst_d(CONFIG_KEY_PAUSE, c))
517 return goto_state(&st_demo_end);
521 show_hud = !show_hud;
526 static int demo_play_buttn(int b, int d)
530 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
532 if (config_tst_d(CONFIG_KEY_PAUSE, SDLK_ESCAPE))
535 return goto_state(&st_demo_end);
541 /*---------------------------------------------------------------------------*/
546 #define DEMO_REPLAY 3
547 #define DEMO_CONTINUE 4
549 static int demo_end_action(int i)
551 audio_play(AUD_MENU, 1.0f);
557 return goto_state(&st_demo_del);
561 return goto_state(&st_demo);
567 progress_replay(curr_demo());
568 return goto_state(&st_demo_play);
570 return goto_state(&st_demo_play);
575 static int demo_end_gui(void)
579 if ((id = gui_vstack(0)))
582 kd = gui_label(id, _("Replay Paused"), GUI_LRG, GUI_ALL,
585 kd = gui_label(id, _("Replay Ends"), GUI_LRG, GUI_ALL,
588 if ((jd = gui_harray(id)))
594 start_id = gui_start(jd, _("Quit"), GUI_SML, DEMO_QUIT, 1);
598 start_id = gui_start(jd, _("Keep"), GUI_SML, DEMO_KEEP, 1);
599 gui_state(jd, _("Delete"), GUI_SML, DEMO_DEL, 0);
604 gui_start(jd, _("Continue"), GUI_SML, DEMO_CONTINUE, 1);
605 gui_toggle(start_id);
608 gui_state(jd, _("Repeat"), GUI_SML, DEMO_REPLAY, 0);
612 gui_layout(id, 0, 0);
618 static int demo_end_enter(struct state *st, struct state *prev)
620 audio_music_fade_out(demo_paused ? 0.2f : 2.0f);
622 return demo_end_gui();
625 static void demo_end_paint(int id, float t)
627 game_client_draw(0, t);
634 static int demo_end_keybd(int c, int d)
638 if (demo_paused && config_tst_d(CONFIG_KEY_PAUSE, c))
639 return demo_end_action(DEMO_CONTINUE);
644 static int demo_end_buttn(int b, int d)
648 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
649 return demo_end_action(gui_token(gui_click()));
651 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
654 return demo_end_action(DEMO_CONTINUE);
656 return demo_end_action(standalone ? DEMO_QUIT : DEMO_KEEP);
662 /*---------------------------------------------------------------------------*/
664 static int demo_del_action(int i)
666 audio_play(AUD_MENU, 1.0f);
667 demo_replay_stop(i == DEMO_DEL);
668 return goto_state(&st_demo);
671 static int demo_del_gui(void)
675 if ((id = gui_vstack(0)))
677 kd = gui_label(id, _("Delete Replay?"), GUI_MED, GUI_ALL, gui_red, gui_red);
679 if ((jd = gui_harray(id)))
681 gui_start(jd, _("No"), GUI_SML, DEMO_KEEP, 1);
682 gui_state(jd, _("Yes"), GUI_SML, DEMO_DEL, 0);
686 gui_layout(id, 0, 0);
692 static int demo_del_enter(struct state *st, struct state *prev)
694 audio_music_fade_out(2.0f);
696 return demo_del_gui();
699 static int demo_del_buttn(int b, int d)
703 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
704 return demo_del_action(gui_token(gui_click()));
705 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
706 return demo_del_action(DEMO_KEEP);
711 /*---------------------------------------------------------------------------*/
713 static int demo_compat_gui(void)
717 if ((id = gui_vstack(0)))
719 gui_label(id, _("Warning!"), GUI_MED, GUI_ALL, 0, 0);
721 gui_multi(id, _("The current replay was recorded with a\\"
722 "different (or unknown) version of this level.\\"
723 "Be prepared to encounter visual errors.\\"),
724 GUI_SML, GUI_ALL, gui_wht, gui_wht);
726 gui_layout(id, 0, 0);
732 static int demo_compat_enter(struct state *st, struct state *prev)
736 return demo_compat_gui();
739 static void demo_compat_timer(int id, float dt)
745 static int demo_compat_buttn(int b, int d)
749 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
750 return goto_state(&st_demo_play);
751 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
752 return goto_state(&st_demo_end);
757 /*---------------------------------------------------------------------------*/
759 struct state st_demo = {
773 struct state st_demo_play = {
787 struct state st_demo_end = {
801 struct state st_demo_del = {
815 struct state st_demo_compat = {