#include <string.h>
#include <time.h>
-#ifndef _WIN32
-#include <unistd.h>
-#endif
-
#include "demo.h"
#include "game.h"
#include "audio.h"
/*---------------------------------------------------------------------------*/
-#define MAGIC 0x4E425251 /* Replay file magic number (should not change) */
-#define OLD_MAGIC 0x4E425250 /* Replay file magic number for neverball 1.4.0 */
-#define REPLAY_VERSION 1 /* Replay file format version (can change) */
-
-#define DEMO_FPS_CAP 200 /* FPS replay limit, keeps size down on monster systems */
+#define MAGIC 0x52424EAF
+#define DEMO_VERSION 1
static FILE *demo_fp;
-static struct demo demos[MAXDEMO]; /* Array of scanned demos */
-
-static int count; /* number of scanned demos */
+static struct demo demos[MAXDEMO]; /* Array of scanned demos */
+static int count; /* Number of scanned demos */
/*---------------------------------------------------------------------------*/
void demo_dump_info(const struct demo *d)
-/* This function dump the info of a demo structure
- * It's only a function for debugging, no need of I18N */
{
printf("Name: %s\n"
"File: %s\n"
d->time, d->goal, d->score, d->balls, d->times);
}
-FILE *demo_header_read(const char *filename, struct demo *d)
-/* Open a demo file, fill the demo information structure
- * If success, return the file pointer positioned after the header
- * If fail, return null
- */
+static int demo_header_read(FILE *fp, struct demo *d)
{
- FILE *fp;
- char *basename;
- char buf[MAXSTR];
+ int magic;
+ int version;
+ int t;
- if ((fp = fopen(filename, FMODE_RB)))
- {
- int magic;
- int version;
- int t;
+ get_index(fp, &magic);
+ get_index(fp, &version);
- get_index(fp, &magic);
- get_index(fp, &version);
+ get_index(fp, &t);
- /* if time is 0, it means the replay was not finished */
- get_index(fp, &t);
+ if (magic == MAGIC && version == DEMO_VERSION && t)
+ {
+ d->timer = t;
- if (magic == MAGIC && version == REPLAY_VERSION && t)
- {
- d->timer = t;
- strncpy(d->filename, filename, PATHMAX);
+ get_index(fp, &d->coins);
+ get_index(fp, &d->state);
+ get_index(fp, &d->mode);
+ get_index(fp, (int *) &d->date);
- /* Remove the directory delimiter */
- basename = strrchr(filename, '/');
+ fread(d->player, 1, MAXNAM, fp);
- if (basename != NULL)
- strncpy(buf, basename + 1, MAXSTR);
- else
- strncpy(buf, filename, MAXSTR);
+ fread(d->shot, 1, PATHMAX, fp);
+ fread(d->file, 1, PATHMAX, fp);
+ fread(d->back, 1, PATHMAX, fp);
+ fread(d->grad, 1, PATHMAX, fp);
+ fread(d->song, 1, PATHMAX, fp);
- /* Remove the extension */
- t = strlen(buf) - strlen(REPLAY_EXT);
- if ((t > 1) && (strcmp(buf + t, REPLAY_EXT) == 0))
- buf[t] = '\0';
- strncpy(d->name, buf, PATHMAX);
- d->name[PATHMAX - 1] = '\0';
+ get_index(fp, &d->time);
+ get_index(fp, &d->goal);
+ get_index(fp, &d->score);
+ get_index(fp, &d->balls);
+ get_index(fp, &d->times);
- get_index (fp, &d->coins);
- get_index (fp, &d->state);
- get_index (fp, &d->mode);
- get_index (fp, (int *)&d->date);
- get_string(fp, d->player, MAXNAM);
- get_string(fp, d->shot, PATHMAX);
- get_string(fp, d->file, PATHMAX);
- get_string(fp, d->back, PATHMAX);
- get_string(fp, d->grad, PATHMAX);
- get_string(fp, d->song, PATHMAX);
- get_index (fp, &d->time);
- get_index (fp, &d->goal);
- get_index (fp, &d->score);
- get_index (fp, &d->balls);
- get_index (fp, &d->times);
- get_string(fp, d->nb_version, 20);
-
- return fp;
- }
- fclose(fp);
+ fread(d->nb_version, 1, 20, fp);
+
+ return 1;
}
- return NULL;
+ return 0;
}
-static FILE *demo_header_write(struct demo *d)
-/* Create a new demo file, write the demo information structure
- * If success, return the file pointer positioned after the header
- * If fail, return null
- * */
+static void demo_header_write(FILE *fp, struct demo *d)
{
int magic = MAGIC;
- int version = REPLAY_VERSION;
+ int version = DEMO_VERSION;
int zero = 0;
- FILE *fp;
- if (d->filename && (fp = fopen(d->filename, FMODE_WB)))
- {
- put_index (fp, &magic);
- put_index (fp, &version);
- put_index (fp, &zero);
- put_index (fp, &zero);
- put_index (fp, &zero);
- put_index (fp, &d->mode);
- put_index (fp, (int *)&d->date);
- put_string(fp, d->player);
- put_string(fp, d->shot);
- put_string(fp, d->file);
- put_string(fp, d->back);
- put_string(fp, d->grad);
- put_string(fp, d->song);
- put_index (fp, &d->time);
- put_index (fp, &d->goal);
- put_index (fp, &d->score);
- put_index (fp, &d->balls);
- put_index (fp, &d->times);
- put_string(fp, VERSION);
-
- return fp;
- }
- return NULL;
+ put_index(fp, &magic);
+ put_index(fp, &version);
+ put_index(fp, &zero);
+ put_index(fp, &zero);
+ put_index(fp, &zero);
+ put_index(fp, &d->mode);
+ put_index(fp, (int *) &d->date);
+
+ fwrite(d->player, 1, MAXNAM, fp);
+
+ fwrite(d->shot, 1, PATHMAX, fp);
+ fwrite(d->file, 1, PATHMAX, fp);
+ fwrite(d->back, 1, PATHMAX, fp);
+ fwrite(d->grad, 1, PATHMAX, fp);
+ fwrite(d->song, 1, PATHMAX, fp);
+
+ put_index(fp, &d->time);
+ put_index(fp, &d->goal);
+ put_index(fp, &d->score);
+ put_index(fp, &d->balls);
+ put_index(fp, &d->times);
+
+ fwrite(d->nb_version, 1, 20, fp);
}
-void demo_header_stop(FILE *fp, int coins, int timer, int state)
/* 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);
/*---------------------------------------------------------------------------*/
+/* Scan another file (used by demo_scan). */
+
static void demo_scan_file(const char *filename)
-/* Scan another file (used by demo_scan */
{
FILE *fp;
- if ((fp = demo_header_read(config_user(filename), &demos[count])))
+ struct demo *d = &demos[count];
+
+ if ((fp = fopen(config_user(filename), FMODE_RB)))
{
- count++;
+ if (demo_header_read(fp, d))
+ {
+ char buf[PATHMAX];
+ int l;
+
+ strncpy(d->filename, config_user(filename), MAXSTR);
+
+ strncpy(buf, filename, PATHMAX);
+ l = strlen(buf) - strlen(REPLAY_EXT);
+
+ if ((l > 1) && (strcmp(buf + l, REPLAY_EXT) == 0))
+ buf[l] = '\0';
+
+ strncpy(d->name, buf, PATHMAX);
+ d->name[PATHMAX - 1] = '\0';
+
+ count++;
+ }
fclose(fp);
}
}
return (n > 0) ? demos[(rand() >> 4) % n].filename : NULL;
}
-const struct demo *get_demo(int i)
+const struct demo *demo_get(int i)
{
return (0 <= i && i < count) ? &demos[i] : NULL;
}
{
static char str[MAXSTR];
struct tm *tm = localtime(&i);
- strftime (str, MAXSTR, "%c", tm);
+ strftime(str, MAXSTR, "%c", tm);
return str;
}
{
struct demo demo;
- /* file structure */
- strncpy(demo.name, name, MAXNAM);
- strncpy(demo.filename, config_user(name), PATHMAX);
+ memset(&demo, 0, sizeof (demo));
+
+ strncpy(demo.filename, config_user(name), MAXSTR);
strcat(demo.filename, REPLAY_EXT);
- demo.time = demo.coins = demo.state = 0;
+
demo.mode = lg->mode;
demo.date = time(NULL);
+
config_get_s(CONFIG_PLAYER, demo.player, MAXNAM);
+
strncpy(demo.shot, level->shot, PATHMAX);
strncpy(demo.file, level->file, PATHMAX);
strncpy(demo.back, level->back, PATHMAX);
strncpy(demo.grad, level->grad, PATHMAX);
strncpy(demo.song, level->song, PATHMAX);
+
demo.time = lg->time;
demo.goal = lg->goal;
demo.score = lg->score;
demo.balls = lg->balls;
demo.times = lg->times;
- demo_fp = demo_header_write(&demo);
- if (demo_fp == NULL)
- return 0;
- else
+ strncpy(demo.nb_version, VERSION, 20);
+
+ if (demo.filename && (demo_fp = fopen(demo.filename, FMODE_WB)))
{
+ demo_header_write(demo_fp, &demo);
audio_music_fade_to(2.0f, level->song);
return game_init(level, lg->time, lg->goal);
}
+ return 0;
}
void demo_play_step(float dt)
{
- static float fps_track = 0.0f;
- static float fps_cap = 1.0f / (float) DEMO_FPS_CAP;
-
if (demo_fp)
{
- fps_track += dt;
- if (fps_track > fps_cap)
- {
- put_float(demo_fp, &fps_track);
- put_game_state(demo_fp);
- fps_track = 0.0f;
- }
+ put_float(demo_fp, &dt);
+ put_game_state(demo_fp);
}
}
-void demo_play_stop(const struct level_game *lg)
/* Update the demo header using the final level state. */
+
+void demo_play_stop(const struct level_game *lg)
{
if (demo_fp)
{
/*---------------------------------------------------------------------------*/
static int demo_load_level(const struct demo *demo, struct level *level)
-/* Load the level of the demo and fill the level structure */
{
if (level_load(demo->file, level))
{
level->goal = demo->goal;
return 1;
}
- else
- return 0;
+ return 0;
}
static struct demo demo_replay; /* The current demo */
return &demo_replay;
}
+/* Internally load a replay and fill the lg structure (if not NULL) */
int demo_replay_init(const char *name, struct level_game *lg)
-/* Internally load a replay an fill the lg structure (if not NULL) */
{
- if ((demo_fp = demo_header_read(name, &demo_replay)))
+ demo_fp = fopen(name, FMODE_RB);
+
+ if (demo_fp && demo_header_read(demo_fp, &demo_replay))
{
+ strncpy(demo_replay.filename, name, MAXSTR);
+
if (!demo_load_level(&demo_replay, &demo_level_replay))
return 0;
return game_init(&demo_level_replay, demo_replay.time,
demo_replay.goal);
}
- else /* A title screen demo */
+ else /* A title screen demo */
return game_init(&demo_level_replay, demo_replay.time, 0);
}
-
return 0;
}
fclose(demo_fp);
demo_fp = NULL;
- if (d) unlink(demo_replay.filename);
+ if (d) remove(demo_replay.filename);
}
}