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.
26 #include "st_shared.h"
32 /*---------------------------------------------------------------------------*/
40 static float replay_time;
41 static float global_time;
43 /*---------------------------------------------------------------------------*/
45 static int demo_action(int i)
47 audio_play(AUD_MENU, 1.0f);
52 return goto_state(&st_title);
56 return goto_state(&st_demo);
61 return goto_state(&st_demo);
65 if (level_replay(demo_get(i)->filename))
67 return goto_state(&st_demo_play);
72 static void demo_replay(int id, int i)
74 int w = config_get_d(CONFIG_WIDTH);
75 int h = config_get_d(CONFIG_HEIGHT);
80 if ((jd = gui_vstack(id)))
83 gui_image(jd, demo_get(i)->level->shot, w / 6, h / 6);
85 nam[MAXNAM - 1] = '\0';
86 strncpy(nam, demo_get(i)->name, MAXNAM);
87 if (nam[MAXNAM - 1] != '\0')
89 nam[MAXNAM - 2] = '.';
90 nam[MAXNAM - 1] = '.';
91 nam[MAXNAM + 0] = '.';
92 nam[MAXNAM + 1] = '\0';
94 gui_state(jd, nam, GUI_SML, i, 0);
107 static int player_id;
109 /* Create a layout for some demo info. If d is NULL, try to reserve enough
112 static int gui_demo_status(int id, const struct demo *d)
115 char levelname[MAXSTR];
116 const char *mode, *state;
122 /* Build a long name */
123 memset(noname, 'M', MAXNAM - 1);
124 noname[MAXNAM - 1] = '\0';
126 /* Get a long mode */
127 mode = mode_to_str(0);
129 for (i = 1; i <= MODE_SINGLE; i++)
131 k = strlen(mode_to_str(i));
135 mode = mode_to_str(i);
139 /* Get a long state */
140 state = state_to_str(0);
142 for (i = 1; i <= GAME_FALL; i++)
144 k = strlen(state_to_str(i));
148 state = state_to_str(i);
152 strcpy(levelname, "M");
156 mode = mode_to_str(d->mode);
157 state = state_to_str(d->state);
158 strcpy(levelname, _(d->level->set->name));
159 strcat(levelname, " - ");
160 strcat(levelname, d->level->repr);
163 if ((jd = gui_hstack(id)))
165 if ((kd = gui_vstack(jd)))
167 if ((ld = gui_harray(kd)))
169 if ((md = gui_vstack(ld)))
171 player_id = gui_label(md, (d ? d->player : noname),
172 GUI_SML, GUI_RGT, 0, 0);
173 coin_id = gui_count(md, (d ? d->coins : 100),
175 state_id = gui_label(md, state, GUI_SML, GUI_RGT,
178 if ((md = gui_vstack(ld)))
180 gui_label(md, _("Player"), GUI_SML, GUI_LFT,
182 gui_label(md, _("Coins"), GUI_SML, GUI_LFT,
184 gui_label(md, _("State"), GUI_SML, GUI_LFT,
187 if ((md = gui_vstack(ld)))
189 name_id = gui_label(md, (d ? d->name : noname),
190 GUI_SML, GUI_RGT, 0, 0);
191 time_id = gui_clock(md, (d ? d->timer : 35000),
193 mode_id = gui_label(md, mode, GUI_SML, GUI_RGT, 0, 0);
196 level_id = gui_label(kd, (levelname), GUI_SML, GUI_RGT, 0, 0);
197 date_id = gui_label(kd, (d ? date_to_str(d->date) : "M"),
198 GUI_SML, GUI_RGT, 0, 0);
200 if ((kd = gui_vstack(jd)))
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, _("Level"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
206 gui_label(kd, _("Date"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
208 if (d && (d->state == GAME_GOAL || d->state == GAME_SPEC))
209 gui_set_color(state_id, gui_grn, gui_grn);
214 static void gui_demo_update_status(int i)
216 const struct demo *d = demo_get(i);
217 char levelname[MAXSTR];
219 strcpy(levelname, _(d->level->set->name));
220 strcat(levelname, " - ");
221 strcat(levelname, d->level->repr);
223 gui_set_label(name_id, d->name);
224 gui_set_label(date_id, date_to_str(d->date));
225 gui_set_label(level_id, levelname);
226 gui_set_label(player_id, d->player);
227 gui_set_label(mode_id, mode_to_str(d->mode));
229 if (d->state == GAME_GOAL || d->state == GAME_SPEC)
230 gui_set_color(state_id, gui_grn, gui_grn);
232 gui_set_color(state_id, gui_red, gui_red);
234 gui_set_label(state_id, state_to_str(d->state));
235 gui_set_count(coin_id, d->coins);
236 gui_set_clock(time_id, d->timer);
239 static int demo_enter(void)
246 if ((total = demo_scan()))
248 if ((jd = gui_hstack(id)))
251 gui_label(jd, _("Select Replay"), GUI_SML, GUI_ALL, 0,0);
253 gui_back_prev_next(jd, first > 0, first + DEMO_STEP < total);
256 if ((jd = gui_varray(id)))
257 for (i = first; i < first + DEMO_STEP ; i += DEMO_LINE)
258 if ((kd = gui_harray(jd)))
260 for (j = i + DEMO_LINE - 1; j >= i; j--)
267 gui_demo_status(id, NULL);
268 gui_layout(id, 0, 0);
269 gui_demo_update_status(0);
273 gui_label(id, _("No Replays"), GUI_MED, GUI_ALL, 0,0);
275 gui_start(id, _("Back"), GUI_SML, GUI_BACK, 0);
276 gui_layout(id, 0, 0);
279 audio_music_fade_to(0.5f, "bgm/inter.ogg");
284 static void demo_point(int id, int x, int y, int dx, int dy)
286 int jd = shared_point_basic(id, x, y);
287 int i = gui_token(jd);
290 gui_demo_update_status(i);
293 static void demo_stick(int id, int a, int v)
295 int jd = shared_stick_basic(id, a, v);
296 int i = gui_token(jd);
299 gui_demo_update_status(i);
302 static int demo_buttn(int b, int d)
306 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
307 return demo_action(gui_token(gui_click()));
308 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
309 return demo_action(GUI_BACK);
314 /*---------------------------------------------------------------------------*/
316 static int simple_play; /* play demo from command line */
318 void demo_play_goto(int simple)
320 simple_play = simple;
323 static int demo_play_enter(void)
327 if ((id = gui_vstack(0)))
329 gui_label(id, _("Replay"), GUI_LRG, GUI_ALL, gui_blu, gui_grn);
330 gui_layout(id, 0, 0);
344 static void demo_play_paint(int id, float st)
349 if (time_state() < 1.f)
353 static void demo_play_timer(int id, float dt)
364 /* Spin or skip depending on how fast the demo wants to run. */
366 while (replay_time < global_time)
367 if (demo_replay_step(&t))
373 goto_state(&st_demo_end);
378 static int demo_play_buttn(int b, int d)
382 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
383 return goto_state(&st_demo_end);
388 /*---------------------------------------------------------------------------*/
393 #define DEMO_REPLAY 3
395 static int demo_end_action(int i)
397 audio_play(AUD_MENU, 1.0f);
402 return goto_state(&st_demo_del);
405 return goto_state(&st_demo);
411 level_replay(curr_demo_replay()->filename);
412 return goto_state(&st_demo_play);
417 static int demo_end_enter(void)
421 if ((id = gui_vstack(0)))
423 kd = gui_label(id, _("Replay Ends"), GUI_LRG, GUI_ALL, gui_gry, gui_red);
425 if ((jd = gui_harray(id)))
427 gui_start(jd, _("Replay Again"), GUI_SML, DEMO_REPLAY, 0);
430 gui_start(jd, _("OK"), GUI_SML, DEMO_QUIT, 1);
433 gui_start(jd, _("Keep"), GUI_SML, DEMO_KEEP, 1);
434 gui_state(jd, _("Delete"), GUI_SML, DEMO_DEL, 0);
440 if ((jd = gui_hstack(id)))
443 gui_demo_status(jd, curr_demo_replay());
448 gui_layout(id, 0, 0);
450 audio_music_fade_out(2.0f);
455 static void demo_end_timer(int id, float dt)
458 float gg[3] = { 0.0f, 9.8f, 0.0f };
459 float gf[3] = { 0.0f, -9.8f, 0.0f };
460 int state = curr_demo_replay()->state;
462 if (time_state() < 2.f)
464 /* Continue demo in background for 2 seconds */
465 if (replay_time < global_time)
467 /* The demo is finished, let the ball go */
468 if (state != GAME_NONE)
469 game_step((state == GAME_GOAL || state == GAME_SPEC) ? gg : gf,
474 /* The demo is not finished, play it */
477 while (replay_time < global_time)
478 if (demo_replay_step(&t))
489 static int demo_end_buttn(int b, int d)
493 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
494 return demo_end_action(gui_token(gui_click()));
495 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
496 return demo_end_action(simple_play ? DEMO_QUIT : DEMO_KEEP);
501 /*---------------------------------------------------------------------------*/
503 static int demo_del_action(int i)
505 audio_play(AUD_MENU, 1.0f);
507 demo_replay_stop(i == DEMO_DEL);
508 return goto_state(&st_demo);
511 static int demo_del_enter(void)
515 if ((id = gui_vstack(0)))
517 kd = gui_label(id, _("Delete Replay?"), GUI_MED, GUI_ALL, gui_red, gui_red);
519 if ((jd = gui_harray(id)))
521 gui_start(jd, _("No"), GUI_SML, DEMO_KEEP, 1);
522 gui_state(jd, _("Yes"), GUI_SML, DEMO_DEL, 0);
526 gui_layout(id, 0, 0);
528 audio_music_fade_out(2.0f);
533 static int demo_del_buttn(int b, int d)
537 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_A, b))
538 return demo_del_action(gui_token(gui_click()));
539 if (config_tst_d(CONFIG_JOYSTICK_BUTTON_EXIT, b))
540 return demo_del_action(DEMO_KEEP);
545 /*---------------------------------------------------------------------------*/
547 struct state st_demo = {
560 struct state st_demo_play = {
573 struct state st_demo_end = {
586 struct state st_demo_del = {