void PauseGame(void); //Pause
void PrintHelp(char* name); //Print help and exit
int GameOverScreen(void);
+void HighScoreInit(void);
+void HighScoreLoad(void);
+void HighScoreSave(void);
+
/*******************
* GLOBAL VARIABLES *
*******************/
//I know global variables are bad, but it's just sooo easy!
+//File to load/save high score
+char *highscorefullpath = NULL;
+
//Windows used by curses
WINDOW * win;
WINDOW * status;
srand( (unsigned)time( NULL ) );
+ HighScoreInit();
+ HighScoreLoad();
+
InitCurses(); //Must be called to start curses
CheckScreenSize(); //Make sure screen is big enough
CreateWindows(30, 28, 1, 1); //Create the main and status windows
if (Points > HiPoints) {
HiPoints = Points;
HiLevelNumber = LevelNumber;
+ HighScoreSave();
}
werase(win);
printf(" --level=LEVEL play specified non-standard LEVEL\n");
}
+
+#define HIGHSCOREFULLPATHBUFSIZ 256
+
+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;
+ }
+}
+
+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';
+ HiPoints = atoi(line);
+ }
+ if (fgets(line, 63, fh) != NULL) {
+ line[63] = '\0';
+ HiLevelNumber = 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, "%d\n", HiPoints);
+ (void)fprintf(fh, "%d\n", HiLevelNumber);
+ (void)fclose(fh);
+ }
+}