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_gui(void)
271 if ((jd = gui_hstack(id)))
274 gui_label(jd, _("Select Replay"), GUI_SML, GUI_ALL, 0,0);
276 gui_navig(jd, first > 0, first + DEMO_STEP < total);
283 gui_layout(id, 0, 0);
285 gui_demo_update_thumbs();
286 gui_demo_update_status(last_viewed);
290 gui_label(id, _("No Replays"), GUI_MED, GUI_ALL, 0, 0);
291 gui_layout(id, 0, 0);
297 static int demo_enter(struct state *st, struct state *prev)
300 demo_dir_free(items);
302 items = demo_dir_scan();
303 total = array_len(items);
305 audio_music_fade_to(0.5f, "bgm/inter.ogg");
310 static void demo_timer(int id, float dt)
312 if (total == 0 && time_state() > 4.0f)
313 goto_state(&st_title);
318 static void demo_point(int id, int x, int y, int dx, int dy)
320 int jd = shared_point_basic(id, x, y);
321 int i = gui_token(jd);
323 if (jd && i >= 0 && !GUI_ISMSK(i))
324 gui_demo_update_status(i);
327 static void demo_stick(int id, int a, int v)
329 int jd = shared_stick_basic(id, a, v);
330 int i = gui_token(jd);
332 if (jd && i >= 0 && !GUI_ISMSK(i))
333 gui_demo_update_status(i);
336 static int demo_buttn(int b, int d)
340 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
341 return demo_action(total ? gui_token(gui_click()) : GUI_BACK);
342 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
343 return demo_action(GUI_BACK);
348 /*---------------------------------------------------------------------------*/
350 static int standalone;
351 static int demo_paused;
353 static int check_compat;
355 static float prelude;
357 void demo_play_goto(int s)
363 static int demo_play_gui(void)
367 if ((id = gui_vstack(0)))
369 gui_label(id, _("Replay"), GUI_LRG, GUI_ALL, gui_blu, gui_grn);
370 gui_layout(id, 0, 0);
377 static int demo_play_enter(struct state *st, struct state *prev)
383 audio_music_fade_in(0.5f);
388 * Post-1.5.1 replays include view data in the first update, this
389 * line is currently left in for compatibility with older replays.
391 game_client_fly(0.0f);
393 if (check_compat && !game_compat_map)
395 goto_state(&st_demo_compat);
404 return demo_play_gui();
407 static void demo_play_paint(int id, float t)
409 game_client_draw(0, t);
414 if (time_state() < prelude)
418 static void demo_play_timer(int id, float dt)
424 /* Pause briefly before starting playback. */
426 if (time_state() < prelude)
429 if (!demo_replay_step(dt))
432 goto_state(&st_demo_end);
438 static int demo_play_keybd(int c, int d)
442 if (config_tst_d(CONFIG_KEY_PAUSE, c))
445 return goto_state(&st_demo_end);
449 show_hud = !show_hud;
454 static int demo_play_buttn(int b, int d)
458 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
460 if (config_tst_d(CONFIG_KEY_PAUSE, SDLK_ESCAPE))
463 return goto_state(&st_demo_end);
469 /*---------------------------------------------------------------------------*/
474 #define DEMO_REPLAY 3
475 #define DEMO_CONTINUE 4
477 static int demo_end_action(int i)
479 audio_play(AUD_MENU, 1.0f);
485 return goto_state(&st_demo_del);
489 return goto_state(&st_demo);
495 progress_replay(curr_demo_replay()->filename);
496 return goto_state(&st_demo_play);
498 return goto_state(&st_demo_play);
503 static int demo_end_gui(void)
507 if ((id = gui_vstack(0)))
510 kd = gui_label(id, _("Replay Paused"), GUI_LRG, GUI_ALL,
513 kd = gui_label(id, _("Replay Ends"), GUI_LRG, GUI_ALL,
516 if ((jd = gui_harray(id)))
522 start_id = gui_start(jd, _("Quit"), GUI_SML, DEMO_QUIT, 1);
526 start_id = gui_start(jd, _("Keep"), GUI_SML, DEMO_KEEP, 1);
527 gui_state(jd, _("Delete"), GUI_SML, DEMO_DEL, 0);
532 gui_start(jd, _("Continue"), GUI_SML, DEMO_CONTINUE, 1);
533 gui_toggle(start_id);
536 gui_state(jd, _("Repeat"), GUI_SML, DEMO_REPLAY, 0);
540 gui_layout(id, 0, 0);
546 static int demo_end_enter(struct state *st, struct state *prev)
548 audio_music_fade_out(demo_paused ? 0.2f : 2.0f);
550 return demo_end_gui();
553 static void demo_end_paint(int id, float t)
555 game_client_draw(0, t);
562 static int demo_end_keybd(int c, int d)
566 if (demo_paused && config_tst_d(CONFIG_KEY_PAUSE, c))
567 return demo_end_action(DEMO_CONTINUE);
572 static int demo_end_buttn(int b, int d)
576 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
577 return demo_end_action(gui_token(gui_click()));
579 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
582 return demo_end_action(DEMO_CONTINUE);
584 return demo_end_action(standalone ? DEMO_QUIT : DEMO_KEEP);
590 /*---------------------------------------------------------------------------*/
592 static int demo_del_action(int i)
594 audio_play(AUD_MENU, 1.0f);
596 demo_replay_stop(i == DEMO_DEL);
597 return goto_state(&st_demo);
600 static int demo_del_gui(void)
604 if ((id = gui_vstack(0)))
606 kd = gui_label(id, _("Delete Replay?"), GUI_MED, GUI_ALL, gui_red, gui_red);
608 if ((jd = gui_harray(id)))
610 gui_start(jd, _("No"), GUI_SML, DEMO_KEEP, 1);
611 gui_state(jd, _("Yes"), GUI_SML, DEMO_DEL, 0);
615 gui_layout(id, 0, 0);
621 static int demo_del_enter(struct state *st, struct state *prev)
623 audio_music_fade_out(2.0f);
625 return demo_del_gui();
628 static int demo_del_buttn(int b, int d)
632 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
633 return demo_del_action(gui_token(gui_click()));
634 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
635 return demo_del_action(DEMO_KEEP);
640 /*---------------------------------------------------------------------------*/
642 static int demo_compat_gui(void)
646 if ((id = gui_vstack(0)))
648 gui_label(id, _("Warning!"), GUI_MED, GUI_ALL, 0, 0);
650 gui_multi(id, _("The current replay was recorded with a\\"
651 "different (or unknown) version of this level.\\"
652 "Be prepared to encounter visual errors.\\"),
653 GUI_SML, GUI_ALL, gui_wht, gui_wht);
655 gui_layout(id, 0, 0);
661 static int demo_compat_enter(struct state *st, struct state *prev)
665 return demo_compat_gui();
668 static void demo_compat_timer(int id, float dt)
674 static int demo_compat_buttn(int b, int d)
678 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
679 return goto_state(&st_demo_play);
680 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
681 return goto_state(&st_demo_end);
686 /*---------------------------------------------------------------------------*/
688 struct state st_demo = {
702 struct state st_demo_play = {
716 struct state st_demo_end = {
730 struct state st_demo_del = {
744 struct state st_demo_compat = {