From: Matthew Mondor Date: Tue, 25 Apr 2023 17:26:10 +0000 (+0000) Subject: Save/load high score to/from file. X-Git-Url: http://git.pulsar-zone.net/?a=commitdiff_plain;h=4d4ac16b4fe7b2f62ba245599fd3021b8a6e5344;p=ninvaders.git Save/load high score to/from file. --- diff --git a/MATT-README.txt b/MATT-README.txt index 7ef88be..81fdd24 100644 --- a/MATT-README.txt +++ b/MATT-README.txt @@ -18,18 +18,13 @@ BUG FIXES IMPROVEMENTS - Allow to build against BSD or other X/Open standard curses(3) - - Use getopt(3) and atoi(3) instead of ad-hoc argument management - - Make UFO missiles two animated rows to be closer to those in the original game. - - Add a curses(3) atexit(3) cleanup handler. - - Use stdlib EXIT_SUCCESS/EXIT_FAILURE. - - Add high score keeping and display the scores on the title screen. - +- Load/save the high score from/to file. - Bandwidth efficiency improvent and addition of the -f option to lower the frame rate from the default (very high) 50, if necessary. diff --git a/nInvaders.c b/nInvaders.c index b2f4e0c..2534c84 100644 --- a/nInvaders.c +++ b/nInvaders.c @@ -34,6 +34,7 @@ #include "ufo.h" #define DEFAULT_FPS 50 +#define HIGHSCORE_FILE ".nInvaders_highscore" int fps = DEFAULT_FPS; @@ -52,6 +53,10 @@ int skill_level; #define GAME_HIGHSCORE 6 +static void highScoreInit(void); +static void highScoreLoad(void); + +static char *highscorefullpath = NULL; /** @@ -361,6 +366,8 @@ int main(int argc, char **argv) skill_level = 1; evaluateCommandLine(argc, argv); // evaluate command line parameters + highScoreInit(); + highScoreLoad(); graphicEngineInit(); // initialize graphic engine // set up timer/ game handling @@ -391,3 +398,64 @@ void doScoring(int alienType) drawscore(); // display score } + +#define HIGHSCOREFULLPATHBUFSIZ 256 + +static void highScoreInit(void) +{ + char *home; + + if (highscorefullpath != NULL) + return; + + if ((highscorefullpath = malloc(HIGHSCOREFULLPATHBUFSIZ)) == NULL) + goto err; + if ((home = getenv("HOME")) == NULL) + goto err; + (void)snprintf(highscorefullpath, HIGHSCOREFULLPATHBUFSIZ - 1, + "%s/%s", home, HIGHSCORE_FILE); + highscorefullpath[HIGHSCOREFULLPATHBUFSIZ - 1] = '\0'; + + return; +err: + if (highscorefullpath != NULL) { + free(highscorefullpath); + highscorefullpath = NULL; + } +} + +static void highScoreLoad(void) +{ + FILE *fh; + char line[64]; + + if (highscorefullpath == NULL) + return; + + if ((fh = fopen(highscorefullpath, "r")) != NULL) { + if (fgets(line, 63, fh) != NULL) { + line[63] = '\0'; + hiscore = atol(line); + } + if (fgets(line, 63, fh) != NULL) { + line[63] = '\0'; + hilevel = atoi(line); + } + (void)fclose(fh); + } +} + +/* XXX Should ideally save in a new unique file and rename for safety */ +void highScoreSave(void) +{ + FILE *fh; + + if (highscorefullpath == NULL) + return; + + if ((fh = fopen(highscorefullpath, "w")) != NULL) { + (void)fprintf(fh, "%ld\n", hiscore); + (void)fprintf(fh, "%d\n", hilevel); + (void)fclose(fh); + } +} diff --git a/nInvaders.h b/nInvaders.h index 7400d98..30077b1 100644 --- a/nInvaders.h +++ b/nInvaders.h @@ -63,4 +63,6 @@ extern void showVersion(void); extern void showGplShort(void); extern void showGpl(void); +void highScoreSave(void); + #endif diff --git a/view.c b/view.c index d3add9e..84531c7 100644 --- a/view.c +++ b/view.c @@ -494,6 +494,7 @@ void titleScreenDisplay(void) if (score > hiscore) { hiscore = score; hilevel = level; + highScoreSave(); } } if (hiscore != 0) {