Minor simplification of code in date_to_str().
[neverball] / ball / st_demo.c
index 1c51c4f..fe49041 100644 (file)
@@ -1,4 +1,4 @@
-/*   
+/*
  * Copyright (C) 2003 Robert Kooima
  *
  * NEVERBALL is  free software; you can redistribute  it and/or modify
 #include "st_demo.h"
 #include "st_title.h"
 
-extern struct state st_demo_play;
-extern struct state st_demo_end;
-extern struct state st_demo_del;
-
 /*---------------------------------------------------------------------------*/
 
 #define DEMO_LINE 4
@@ -66,8 +62,9 @@ static int demo_action(int i)
         break;
 
     default:
-        if (level_replay(get_demo(i)->filename))
-            return goto_demo_play(0);
+        if (level_replay(demo_get(i)->filename))
+            demo_play_goto(0);
+            return goto_state(&st_demo_play);
     }
     return 1;
 }
@@ -78,12 +75,23 @@ static void demo_replay(int id, int i)
     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);
-        gui_state(jd, get_demo(i)->name, GUI_SML, i, 0);
+        nam[MAXNAM - 1] = '\0';
+        strncpy(nam, demo_get(i)->name, MAXNAM);
+        if (nam[MAXNAM - 1] != '\0')
+        {
+            nam[MAXNAM - 2] = '.';
+            nam[MAXNAM - 1] = '.';
+            nam[MAXNAM + 0] = '.';
+            nam[MAXNAM + 1] = '\0';
+        }
+        gui_state(jd, nam, GUI_SML, i, 0);
 
         gui_active(jd, i, 0);
     }
@@ -98,8 +106,10 @@ static int mode_id;
 static int state_id;
 static int player_id;
 
-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 enought space */
+/* 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)
 {
     char noname[MAXNAM];
     const char *mode, *state;
@@ -108,130 +118,131 @@ static int gui_demo_status(int id, const struct demo * d)
 
     if (d == NULL)
     {
-       /* Build a long name */
-       memset(noname, 'M', MAXNAM-1);
-       noname[MAXNAM-1] = '\0';
-
-       /* Get a long mode */
-       mode = mode_to_str(0);
-       j = strlen(mode);
-       for(i=1; i<=MODE_SINGLE; i++)
-       {
-           k = strlen(mode_to_str(i));
-           if (k > j)
-           {
-               j = k;
-               mode = mode_to_str(i);
-           }
-       }
-
-       /* Get a long state */
-       state = state_to_str(0);
-       j = strlen(state);
-       for(i=1; i<=GAME_FALL; i++)
-       {
-           k = strlen(state_to_str(i));
-           if (k > j)
-           {
-               j = k;
-               state = state_to_str(i);
-           }
-       }
+        /* Build a long name */
+        memset(noname, 'M', MAXNAM - 1);
+        noname[MAXNAM - 1] = '\0';
+
+        /* Get a long mode */
+        mode = mode_to_str(0);
+        j = strlen(mode);
+        for (i = 1; i <= MODE_SINGLE; i++)
+        {
+            k = strlen(mode_to_str(i));
+            if (k > j)
+            {
+                j = k;
+                mode = mode_to_str(i);
+            }
+        }
+
+        /* Get a long state */
+        state = state_to_str(0);
+        j = strlen(state);
+        for (i = 1; i <= GAME_FALL; i++)
+        {
+            k = strlen(state_to_str(i));
+            if (k > j)
+            {
+                j = k;
+                state = state_to_str(i);
+            }
+        }
     }
     else
     {
-       mode  = mode_to_str(d->mode);
-       state = state_to_str(d->state);
+        mode = mode_to_str(d->mode);
+        state = state_to_str(d->state);
     }
-    
+
     if ((jd = gui_hstack(id)))
     {
-       if((kd = gui_vstack(jd)))
-       {
-           if ((ld = gui_harray(kd)))
-           {
-               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);
-               }
-               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);
-               }
-               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);
-                   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);
-       }
-       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);
-       }
-       if(d && d->state == GAME_GOAL)
-           gui_set_color(state_id, gui_grn, gui_grn);
+        if ((kd = gui_vstack(jd)))
+        {
+            if ((ld = gui_harray(kd)))
+            {
+                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);
+                }
+                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);
+                }
+                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);
+                    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);
+        }
+        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);
+        }
+        if (d && (d->state == GAME_GOAL || d->state == GAME_SPEC))
+            gui_set_color(state_id, gui_grn, gui_grn);
     }
     return jd;
 }
 
 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));
-    gui_set_label(level_id,   d->file);
+    gui_set_label(level_id,  d->file);
     gui_set_label(player_id, d->player);
     gui_set_label(mode_id,   mode_to_str(d->mode));
-    if (d->state == GAME_GOAL)
-       gui_set_color(state_id, gui_grn, gui_grn);
+
+    if (d->state == GAME_GOAL || d->state == GAME_SPEC)
+        gui_set_color(state_id, gui_grn, gui_grn);
     else
-       gui_set_color(state_id, gui_red, gui_red);
-    gui_set_label(state_id,  state_to_str(d->state));
-    gui_set_count(coin_id,   d->coins);
-    gui_set_clock(time_id,   d->timer);
+        gui_set_color(state_id, gui_red, gui_red);
+
+    gui_set_label(state_id, state_to_str(d->state));
+    gui_set_count(coin_id, d->coins);
+    gui_set_clock(time_id, d->timer);
 }
 
 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_multi(id, _("You can save replays of your games.\\"
-                        "Currently, there are no replays saved."),
-                  GUI_SML, GUI_ALL, gui_wht, gui_wht);
-        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)))
+        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);
+            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)))
@@ -242,11 +253,18 @@ static int demo_enter(void)
                         else
                             gui_space(kd);
                 }
-       gui_filler(id);
-       gui_demo_status(id, NULL);
-       gui_layout(id, 0, 0);
+        gui_filler(id);
+        gui_demo_status(id, NULL);
+        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");
 
@@ -257,16 +275,18 @@ 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);
+
+    if (jd && i >= 0)
+        gui_demo_update_status(i);
 }
 
 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);
+
+    if (jd && i >= 0)
+        gui_demo_update_status(i);
 }
 
 static int demo_buttn(int b, int d)
@@ -283,11 +303,11 @@ static int demo_buttn(int b, int d)
 
 /*---------------------------------------------------------------------------*/
 
-static int simple_play;
-int goto_demo_play(int simple)
+static int simple_play; /* play demo from command line */
+
+void demo_play_goto(int simple)
 {
     simple_play = simple;
-    return goto_state(&st_demo_play);
 }
 
 static int demo_play_enter(void)
@@ -338,7 +358,7 @@ static void demo_play_timer(int id, float dt)
         {
             replay_time += t;
         }
-        else 
+        else
         {
             goto_state(&st_demo_end);
             break;
@@ -368,17 +388,17 @@ static int demo_end_action(int i)
 
     switch (i)
     {
-    case DEMO_DEL: 
-       return goto_state(&st_demo_del);
+    case DEMO_DEL:
+        return goto_state(&st_demo_del);
     case DEMO_KEEP:
         demo_replay_stop(0);
         return goto_state(&st_demo);
     case DEMO_QUIT:
-       demo_replay_stop(0);
-       return 0;
+        demo_replay_stop(0);
+        return 0;
     case DEMO_REPLAY:
-       demo_replay_stop(0);
-       level_replay(curr_demo_replay()->filename);
+        demo_replay_stop(0);
+        level_replay(curr_demo_replay()->filename);
         return goto_state(&st_demo_play);
     }
     return 1;
@@ -394,24 +414,25 @@ static int demo_end_enter(void)
 
         if ((jd = gui_harray(id)))
         {
-           gui_start(jd, _("Replay Again"), GUI_SML, DEMO_REPLAY, 0);
-           if (simple_play)
+            gui_start(jd, _("Replay Again"), GUI_SML, DEMO_REPLAY, 0);
+
+            if (simple_play)
                 gui_start(jd, _("OK"),       GUI_SML, DEMO_QUIT,   1);
-           else
-           {
-                gui_state(jd, _("Delete"),   GUI_SML, DEMO_DEL,    0);
+            else
+            {
                 gui_start(jd, _("Keep"),     GUI_SML, DEMO_KEEP,   1);
-           }
+                gui_state(jd, _("Delete"),   GUI_SML, DEMO_DEL,    0);
+            }
         }
 
-       gui_filler(id);
+        gui_filler(id);
 
         if ((jd = gui_hstack(id)))
-       {
-           gui_filler(jd);
-           gui_demo_status(jd, curr_demo_replay());
-           gui_filler(jd);
-       }
+        {
+            gui_filler(jd);
+            gui_demo_status(jd, curr_demo_replay());
+            gui_filler(jd);
+        }
 
         gui_pulse(kd, 1.2f);
         gui_layout(id, 0, 0);
@@ -423,11 +444,34 @@ static int demo_end_enter(void)
 
 static void demo_end_timer(int id, float dt)
 {
-    float g[3] = { 0.0f, -9.8f, 0.0f };
+    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)
-       game_step(g, dt, 0);
-               
+    {
+        /* 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);
 }
@@ -464,8 +508,8 @@ static int demo_del_enter(void)
 
         if ((jd = gui_harray(id)))
         {
-            gui_state(jd, _("Yes"), GUI_SML, DEMO_DEL,  0);
             gui_start(jd, _("No"),  GUI_SML, DEMO_KEEP, 1);
+            gui_state(jd, _("Yes"), GUI_SML, DEMO_DEL,  0);
         }
 
         gui_pulse(kd, 1.2f);