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.
#include "ufo.h"
#define DEFAULT_FPS 50
+#define HIGHSCORE_FILE ".nInvaders_highscore"
int fps = DEFAULT_FPS;
#define GAME_HIGHSCORE 6
+static void highScoreInit(void);
+static void highScoreLoad(void);
+
+static char *highscorefullpath = NULL;
/**
skill_level = 1;
evaluateCommandLine(argc, argv); // evaluate command line parameters
+ highScoreInit();
+ highScoreLoad();
graphicEngineInit(); // initialize graphic engine
// set up timer/ game handling
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);
+ }
+}