Those sets haven't been merged.
[neverball] / ball / demo.c
index abfc5ff..2903b7f 100644 (file)
 #include "solid.h"
 #include "config.h"
 #include "binary.h"
+#include "text.h"
 
 /*---------------------------------------------------------------------------*/
 
 #define MAGIC           0x52424EAF
-#define DEMO_VERSION    3
+#define DEMO_VERSION    5
 
 #define DATELEN 20
 
@@ -59,7 +60,7 @@ void demo_dump_info(const struct demo *d)
            "Balls:        %d\n"
            "Total Time:   %d\n",
            d->name, d->filename,
-           d->timer, d->coins, d->mode, d->state, ctime(&d->date),
+           d->timer, d->coins, d->mode, d->status, ctime(&d->date),
            d->player,
            d->shot, d->file, d->back, d->grad, d->song,
            d->time, d->goal, d->score, d->balls, d->times);
@@ -104,12 +105,12 @@ static int demo_header_read(FILE *fp, struct demo *d)
         d->timer = t;
 
         get_index(fp, &d->coins);
-        get_index(fp, &d->state);
+        get_index(fp, &d->status);
         get_index(fp, &d->mode);
 
-        fread(d->player, 1, MAXNAM, fp);
+        get_string(fp, d->player, MAXNAM);
 
-        fread(datestr, 1, DATELEN, fp);
+        get_string(fp, datestr, DATELEN);
         sscanf(datestr,
                "%d-%d-%dT%d:%d:%d",
                &date.tm_year,
@@ -126,8 +127,8 @@ static int demo_header_read(FILE *fp, struct demo *d)
 
         d->date = make_time_from_utc(&date);
 
-        fread(d->shot, 1, PATHMAX, fp);
-        fread(d->file, 1, PATHMAX, fp);
+        get_string(fp, d->shot, PATHMAX);
+        get_string(fp, d->file, PATHMAX);
 
         get_index(fp, &d->time);
         get_index(fp, &d->goal);
@@ -188,11 +189,11 @@ static void demo_header_write(FILE *fp, struct demo *d)
     put_index(fp, &zero);
     put_index(fp, &d->mode);
 
-    fwrite(d->player, 1, MAXNAM, fp);
-    fwrite(datestr, 1, DATELEN, fp);
+    put_string(fp, d->player);
+    put_string(fp, datestr);
 
-    fwrite(d->shot, 1, PATHMAX, fp);
-    fwrite(d->file, 1, PATHMAX, fp);
+    put_string(fp, d->shot);
+    put_string(fp, d->file);
 
     put_index(fp, &d->time);
     put_index(fp, &d->goal);
@@ -201,19 +202,6 @@ static void demo_header_write(FILE *fp, struct demo *d)
     put_index(fp, &d->times);
 }
 
-/* Update the demo header using the final level state. */
-
-void demo_header_stop(FILE *fp, int coins, int timer, int state)
-{
-    long pos = ftell(fp);
-
-    fseek(fp, 8, SEEK_SET);
-    put_index(fp, &timer);
-    put_index(fp, &coins);
-    put_index(fp, &state);
-    fseek(fp, pos, SEEK_SET);
-}
-
 /*---------------------------------------------------------------------------*/
 
 /* Scan another file (used by demo_scan). */
@@ -227,8 +215,9 @@ static void demo_scan_file(const char *filename)
     {
         if (demo_header_read(fp, d))
         {
-            strncpy(d->filename, config_user(filename),       MAXSTR);
-            strncpy(d->name,     bname(filename, REPLAY_EXT), PATHMAX);
+            strncpy(d->filename, config_user(filename), MAXSTR);
+            strncpy(d->name, bname(text_from_locale(d->filename), REPLAY_EXT),
+                    PATHMAX);
             d->name[PATHMAX - 1] = '\0';
 
             count++;
@@ -297,17 +286,17 @@ const struct demo *demo_get(int i)
 const char *date_to_str(time_t i)
 {
     static char str[MAXSTR];
+    const char *fmt;
 
     /* TRANSLATORS:  here is the format of the date shown at the
-       replay selection screen.  The default will work in most cases, so
-       you should only change it if something's horribly wrong, like,
-       for instance, the GUI layout is broken.  See strftime(3) for
+       replay selection screen (and possibly elsewhere).  The default
+       format is necessarily locale-independent.  See strftime(3) for
        details on the format.
      */
 
-    strftime(str, MAXSTR, /* xgettext:no-c-format */ _("%c"), localtime(&i));
-
-    return str;
+    fmt = /* xgettext:no-c-format */ L_("%Y-%m-%d %H:%M:%S");
+    strftime(str, MAXSTR, fmt, localtime(&i));
+    return text_from_locale(str);
 }
 
 /*---------------------------------------------------------------------------*/
@@ -381,33 +370,43 @@ int demo_play_init(const char *name,
     return 0;
 }
 
-void demo_play_step(float dt)
+void demo_play_step()
+{
+    if (demo_fp)
+        input_put(demo_fp);
+}
+
+void demo_play_stat(const struct level_game *lg)
 {
     if (demo_fp)
     {
-        put_float(demo_fp, &dt);
-        put_game_state(demo_fp);
+        long pos = ftell(demo_fp);
+
+        fseek(demo_fp, 8, SEEK_SET);
+
+        put_index(demo_fp, &lg->timer);
+        put_index(demo_fp, &lg->coins);
+        put_index(demo_fp, &lg->status);
+
+        fseek(demo_fp, pos, SEEK_SET);
     }
 }
 
-/* Update the demo header using the final level state. */
-
-void demo_play_stop(const struct level_game *lg)
+void demo_play_stop(void)
 {
     if (demo_fp)
     {
-        demo_header_stop(demo_fp, lg->coins, lg->timer, lg->state);
         fclose(demo_fp);
         demo_fp = NULL;
     }
 }
 
-int demo_play_saved(void)
+int demo_saved(void)
 {
     return demo_exists(USER_REPLAY_FILE);
 }
 
-void demo_play_save(const char *name)
+void demo_rename(const char *name)
 {
     char src[MAXSTR];
     char dst[MAXSTR];
@@ -422,6 +421,10 @@ void demo_play_save(const char *name)
         strcpy(dst, config_user(name));
         strcat(dst, REPLAY_EXT);
 
+#ifdef _WIN32
+        if (demo_exists(name))
+            remove(dst);
+#endif
         rename(src, dst);
     }
 }
@@ -447,27 +450,29 @@ const struct demo *curr_demo_replay(void)
     return &demo_replay;
 }
 
-/* Internally load a replay and fill the lg structure (if not NULL) */
+static int demo_status = GAME_NONE;
 
 int demo_replay_init(const char *name, struct level_game *lg)
 {
-    demo_fp = fopen(name, FMODE_RB);
+    demo_status = GAME_NONE;
+    demo_fp     = fopen(name, FMODE_RB);
 
     if (demo_fp && demo_header_read(demo_fp, &demo_replay))
     {
-        strncpy(demo_replay.filename, name,                    MAXSTR);
-        strncpy(demo_replay.name,     bname(name, REPLAY_EXT), PATHMAX);
+        strncpy(demo_replay.filename, name, MAXSTR);
+        strncpy(demo_replay.name, bname(text_from_locale(demo_replay.filename),
+                REPLAY_EXT), PATHMAX);
 
         if (!demo_load_level(&demo_replay, &demo_level_replay))
             return 0;
 
         if (lg)
         {
-            lg->mode = demo_replay.mode;
+            lg->mode  = demo_replay.mode;
             lg->score = demo_replay.score;
             lg->times = demo_replay.times;
-            lg->time = demo_replay.time;
-            lg->goal = demo_replay.goal;
+            lg->time  = demo_replay.time;
+            lg->goal  = demo_replay.goal;
 
             /* A normal replay demo */
             audio_music_fade_to(0.5f, demo_level_replay.song);
@@ -480,25 +485,28 @@ int demo_replay_init(const char *name, struct level_game *lg)
     return 0;
 }
 
-int demo_replay_step(float *dt)
+int demo_replay_step(float dt)
 {
-    const float g[3] = { 0.0f, -9.8f, 0.0f };
-    int sv;
+    const float gdn[3] = { 0.0f, -9.8f, 0.0f };
+    const float gup[3] = { 0.0f, +9.8f, 0.0f };
 
     if (demo_fp)
     {
-        get_float(demo_fp, dt);
-
-        if (feof(demo_fp) == 0)
+        if (input_get(demo_fp))
         {
-            /* Play out current game state for particles, clock, etc. */
-
-            game_step(g, *dt, &sv);
-
-            /* Load real current game state from file. */
-
-            if (get_game_state(demo_fp))
-                return 1;
+            /* Play out current game state. */
+
+            switch (demo_status)
+            {
+            case GAME_NONE:
+                demo_status = game_step(gdn, dt, 1); break;
+            case GAME_GOAL:
+                (void)        game_step(gup, dt, 0); break;
+            default:
+                (void)        game_step(gdn, dt, 0); break;
+            }
+
+            return 1;
         }
     }
     return 0;