-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
5 changed files
with
124 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
#ifndef ARGS_H | ||
#define ARGS_H | ||
void parse_args(int argc, char **argv); | ||
unsigned long get_seed(void); | ||
void set_seed(unsigned long seed); | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#include <stdio.h> | ||
#include <curses.h> | ||
#include <unistd.h> | ||
#include "demo.h" | ||
#include "args.h" | ||
|
||
static void demo_write_header(void); | ||
static void demo_read_header(void); | ||
|
||
static FILE* read_from = 0; | ||
static FILE* write_to = 0; | ||
|
||
void usleep(long); /* needed because headers are big sad */ | ||
|
||
int demo_next(void) | ||
{ | ||
int move = 0; | ||
if (read_from) { | ||
fread(&move, sizeof(move), 1, read_from); | ||
usleep(1000 /*ms to us*/ * 300 /*ms*/); | ||
} | ||
if (!move) { | ||
move = getch(); | ||
} | ||
if (write_to) { | ||
fwrite(&move, sizeof(move), 1, write_to); | ||
} | ||
return move; | ||
} | ||
|
||
static void demo_read_header() | ||
{ | ||
int demo_version; | ||
unsigned long s; | ||
fread(&demo_version, sizeof(demo_version), 1, read_from); | ||
fread(&s, sizeof(s), 1, read_from); | ||
/* TODO: fail if demo version is wrong */ | ||
set_seed(s); | ||
} | ||
|
||
|
||
static void demo_write_header(void) | ||
{ | ||
unsigned long s = get_seed(); | ||
int demo_version = DEMO_VERSION; | ||
fwrite(&demo_version, sizeof(demo_version), 1, write_to); | ||
fwrite(&s, sizeof(s), 1, write_to); | ||
} | ||
|
||
void demo_start(int mode, char* fname) | ||
{ | ||
if (!fname) { | ||
fname = DEMO_DEFAULT_FILE; | ||
} | ||
if (read_from) { | ||
fclose(read_from); | ||
read_from = 0; | ||
} | ||
if (write_to) { | ||
fclose(write_to); | ||
write_to = 0; | ||
} | ||
switch (mode) { | ||
default: | ||
break; | ||
case DEMO_REPLAY: | ||
read_from = fopen(fname, "r"); | ||
demo_read_header(); | ||
break; | ||
case DEMO_RECORD: | ||
write_to = fopen(fname, "w"); | ||
demo_write_header(); | ||
break; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
#ifndef DEMO_H | ||
#define DEMO_H | ||
int demo_next(void); | ||
void demo_start(int mode, char* fname); | ||
#define DEMO_NONE 0 | ||
#define DEMO_REPLAY 1 | ||
#define DEMO_RECORD 2 | ||
#define DEMO_VERSION 0 | ||
#define DEMO_DEFAULT_FILE ".demo.hag" | ||
/* TODO: do we need a demo_close? */ | ||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters