Store replay date/time as an ISO 8601 formatted string. This involves
[neverball] / ball / st_demo.c
index ed243e7..fe49041 100644 (file)
@@ -29,9 +29,6 @@
 #include "st_demo.h"
 #include "st_title.h"
 
-extern struct state st_demo_end;
-extern struct state st_demo_del;
-
 /*---------------------------------------------------------------------------*/
 
 #define DEMO_LINE 4
@@ -65,7 +62,7 @@ static int demo_action(int i)
         break;
 
     default:
-        if (level_replay(get_demo(i)->filename))
+        if (level_replay(demo_get(i)->filename))
             demo_play_goto(0);
             return goto_state(&st_demo_play);
     }
@@ -77,15 +74,16 @@ static void demo_replay(int id, int i)
     int w = config_get_d(CONFIG_WIDTH);
     int h = config_get_d(CONFIG_HEIGHT);
     int jd;
+
     char nam[MAXNAM + 3];
 
     if ((jd = gui_vstack(id)))
     {
         gui_space(jd);
+        gui_image(jd, demo_get(i)->shot, w / 6, h / 6);
 
-        gui_image(jd, get_demo(i)->shot, w / 6, h / 6);
         nam[MAXNAM - 1] = '\0';
-        strncpy(nam, get_demo(i)->name, MAXNAM);
+        strncpy(nam, demo_get(i)->name, MAXNAM);
         if (nam[MAXNAM - 1] != '\0')
         {
             nam[MAXNAM - 2] = '.';
@@ -108,9 +106,10 @@ static int mode_id;
 static int state_id;
 static int player_id;
 
+/* Create a layout for some demo info.  If d is NULL, try to reserve enough
+ * space. */
+
 static int gui_demo_status(int id, const struct demo *d)
-/* Create a layout for some demo info, if d is NULL, try to reserve enough
- * space */
 {
     char noname[MAXNAM];
     const char *mode, *state;
@@ -163,48 +162,43 @@ static int gui_demo_status(int id, const struct demo *d)
             {
                 if ((md = gui_vstack(ld)))
                 {
-                    player_id =
-                        gui_label(md, (d ? d->player : noname), GUI_SML,
-                                  GUI_RGT, 0, 0);
-                    coin_id =
-                        gui_count(md, (d ? d->coins : 100), GUI_SML, GUI_RGT);
-                    state_id =
-                        gui_label(md, state, GUI_SML, GUI_RGT, gui_red,
-                                  gui_red);
+                    player_id = gui_label(md, (d ? d->player : noname),
+                                          GUI_SML, GUI_RGT, 0, 0);
+                    coin_id = gui_count(md, (d ? d->coins : 100),
+                                        GUI_SML, GUI_RGT);
+                    state_id = gui_label(md, state, GUI_SML, GUI_RGT,
+                                         gui_red, gui_red);
                 }
                 if ((md = gui_vstack(ld)))
                 {
-                    gui_label(md, _("Player"), GUI_SML, GUI_LFT, gui_wht,
-                              gui_wht);
-                    gui_label(md, _("Coins"), GUI_SML, GUI_LFT, gui_wht,
-                              gui_wht);
-                    gui_label(md, _("State"), GUI_SML, GUI_LFT, gui_wht,
-                              gui_wht);
+                    gui_label(md, _("Player"), GUI_SML, GUI_LFT,
+                              gui_wht, gui_wht);
+                    gui_label(md, _("Coins"), GUI_SML, GUI_LFT,
+                              gui_wht, gui_wht);
+                    gui_label(md, _("State"), GUI_SML, GUI_LFT,
+                              gui_wht, gui_wht);
                 }
                 if ((md = gui_vstack(ld)))
                 {
-                    name_id =
-                        gui_label(md, (d ? d->name : noname), GUI_SML, GUI_RGT,
-                                  0, 0);
-                    time_id =
-                        gui_clock(md, (d ? d->timer : 35000), GUI_SML, GUI_RGT);
+                    name_id = gui_label(md, (d ? d->name : noname),
+                                        GUI_SML, GUI_RGT, 0, 0);
+                    time_id = gui_clock(md, (d ? d->timer : 35000),
+                                        GUI_SML, GUI_RGT);
                     mode_id = gui_label(md, mode, GUI_SML, GUI_RGT, 0, 0);
                 }
             }
-            level_id =
-                gui_label(kd, (d ? d->file : "M"), GUI_SML, GUI_RGT, gui_wht,
-                          gui_wht);
-            date_id =
-                gui_label(kd, (d ? date_to_str(d->date) : "M"), GUI_SML,
-                          GUI_RGT, 0, 0);
+            level_id = gui_label(kd, (d ? d->file : "M"), GUI_SML, GUI_RGT,
+                                 gui_wht, gui_wht);
+            date_id = gui_label(kd, (d ? date_to_str(d->date) : "M"),
+                                GUI_SML, GUI_RGT, 0, 0);
         }
         if ((kd = gui_vstack(jd)))
         {
             gui_label(kd, _("Replay"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
-            gui_label(kd, _("Time"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
-            gui_label(kd, _("Mode"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
-            gui_label(kd, _("Level"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
-            gui_label(kd, _("Date"), GUI_SML, GUI_LFT, gui_wht, gui_wht);
+            gui_label(kd, _("Time"),   GUI_SML, GUI_LFT, gui_wht, gui_wht);
+            gui_label(kd, _("Mode"),   GUI_SML, GUI_LFT, gui_wht, gui_wht);
+            gui_label(kd, _("Level"),  GUI_SML, GUI_LFT, gui_wht, gui_wht);
+            gui_label(kd, _("Date"),   GUI_SML, GUI_LFT, gui_wht, gui_wht);
         }
         if (d && (d->state == GAME_GOAL || d->state == GAME_SPEC))
             gui_set_color(state_id, gui_grn, gui_grn);
@@ -214,7 +208,7 @@ static int gui_demo_status(int id, const struct demo *d)
 
 static void gui_demo_update_status(int i)
 {
-    const struct demo *d = get_demo(i);
+    const struct demo *d = demo_get(i);
 
     gui_set_label(name_id,   d->name);
     gui_set_label(date_id,   date_to_str(d->date));
@@ -235,27 +229,20 @@ static void gui_demo_update_status(int i)
 static int demo_enter(void)
 {
     int i, j;
-    int id, jd, kd, ld;
-
-    total = demo_scan();
+    int id, jd, kd;
 
     id = gui_vstack(0);
-    if (total == 0)
-    {
-        gui_label(id, _("No Replays"), GUI_MED, GUI_ALL, 0,0);
-        gui_filler(id);
-        gui_start(id, _("Back"), GUI_SML, GUI_BACK, 0);
-        gui_layout(id, 0, 0);
-    }
-    else
+
+    if ((total = demo_scan()))
     {
         if ((jd = gui_hstack(id)))
         {
 
-            ld = gui_label(jd, _("Select Replay"), GUI_SML, GUI_ALL, 0,0);
+            gui_label(jd, _("Select Replay"), GUI_SML, GUI_ALL, 0,0);
             gui_filler(jd);
             gui_back_prev_next(jd, first > 0, first + DEMO_STEP < total);
         }
+
         if ((jd = gui_varray(id)))
             for (i = first; i < first + DEMO_STEP ; i += DEMO_LINE)
                 if ((kd = gui_harray(jd)))
@@ -271,6 +258,13 @@ static int demo_enter(void)
         gui_layout(id, 0, 0);
         gui_demo_update_status(0);
     }
+    else
+    {
+        gui_label(id, _("No Replays"), GUI_MED, GUI_ALL, 0,0);
+        gui_filler(id);
+        gui_start(id, _("Back"), GUI_SML, GUI_BACK, 0);
+        gui_layout(id, 0, 0);
+    }
 
     audio_music_fade_to(0.5f, "bgm/inter.ogg");
 
@@ -281,6 +275,7 @@ static void demo_point(int id, int x, int y, int dx, int dy)
 {
     int jd = shared_point_basic(id, x, y);
     int i  = gui_token(jd);
+
     if (jd && i >= 0)
         gui_demo_update_status(i);
 }
@@ -289,6 +284,7 @@ static void demo_stick(int id, int a, int v)
 {
     int jd = shared_stick_basic(id, a, v);
     int i  = gui_token(jd);
+
     if (jd && i >= 0)
         gui_demo_update_status(i);
 }
@@ -307,7 +303,7 @@ static int demo_buttn(int b, int d)
 
 /*---------------------------------------------------------------------------*/
 
-static int simple_play;
+static int simple_play; /* play demo from command line */
 
 void demo_play_goto(int simple)
 {
@@ -448,12 +444,33 @@ static int demo_end_enter(void)
 
 static void demo_end_timer(int id, float dt)
 {
+    float t;
     float gg[3] = { 0.0f,  9.8f, 0.0f };
     float gf[3] = { 0.0f, -9.8f, 0.0f };
     int state = curr_demo_replay()->state;
 
-    if (time_state() < 2.f && state != GAME_NONE)
-        game_step((state == GAME_GOAL || state == GAME_SPEC) ? gg : gf, dt, NULL);
+    if (time_state() < 2.f)
+    {
+        /* Continue demo in background for 2 seconds */
+        if (replay_time < global_time)
+        {
+            /* The demo is finished, let the ball go */
+            if (state != GAME_NONE)
+                game_step((state == GAME_GOAL || state == GAME_SPEC) ? gg : gf,
+                          dt, NULL);
+        }
+        else
+        {
+            /* The demo is not finished, play it */
+            global_time += dt;
+
+            while (replay_time < global_time)
+                if (demo_replay_step(&t))
+                    replay_time += t;
+                else
+                    break;
+        }
+    }
 
     gui_timer(id, dt);
     audio_timer(dt);