From 32db2afef54f9f49a116dac6fd8b75b0f248e349 Mon Sep 17 00:00:00 2001 From: Bill Kendrick Date: Wed, 10 Dec 2003 08:17:26 +0000 Subject: [PATCH] (Added high score code from Adam Czachorowski) SVN-Revision: 12 --- src/high_scores.c | 121 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/high_scores.h | 12 ++++++ 2 files changed, 133 insertions(+) create mode 100644 src/high_scores.c create mode 100644 src/high_scores.h diff --git a/src/high_scores.c b/src/high_scores.c new file mode 100644 index 000000000..6112b6f1c --- /dev/null +++ b/src/high_scores.c @@ -0,0 +1,121 @@ +/* + + by Adam Czachorowski + gislan@o2.pl + +*/ + +/* Open the highscore file: */ + +# include +# include + +# include "high_scores.h" + +FILE * opendata(char * mode) +{ + char * filename, * home; + FILE * fi; + + +#ifdef LINUX + /* Get home directory (from $HOME variable)... if we can't determine it, + use the current directory ("."): */ + + if (getenv("HOME") != NULL) + home = getenv("HOME"); + else + home = "."; + + + /* Create the buffer for the filename: */ + + filename = (char *) malloc(sizeof(char) * (strlen(home) + + strlen("/.supertux") + 1)); + + strcpy(filename, home); + /* Open the high score file: */ + + strcat(filename, "/.supertux"); +#else + filename = "supertux.dat"; +#endif + + + /* Try opening the file: */ + + fi = fopen(filename, mode); + + if (fi == NULL) + { + fprintf(stderr, "Warning: I could not open the high score file "); + + if (strcmp(mode, "r") == 0) + fprintf(stderr, "for read!!!\n"); + else if (strcmp(mode, "w") == 0) + fprintf(stderr, "for write!!!\n"); + + } + + return(fi); + } + +/* Load data from high score file: */ + +int load_hs(void) +{ + FILE * fi; + char temp[128]; + int score = 100; + + /* Try to open file: */ + + fi = opendata("r"); + if (fi != NULL) + { + do + { + fgets(temp, sizeof(temp), fi); + + if (!feof(fi)) + { + temp[strlen(temp) - 1] = '\0'; + + + /* Parse each line: */ + + if (strstr(temp, "highscore=") == temp) + { + score = atoi(temp + 10); + + if (score == 0) + score = 100; + } + } + } + while (!feof(fi)); + + fclose(fi); + } + return score; +} + +void save_hs(int score) +{ + FILE * fi; + + + /* Try to open file: */ + + fi = opendata("w"); + if (fi != NULL) + { + fprintf(fi, "# Supertux data file\n\n"); + + fprintf(fi, "highscore=%d\n", score); + + fprintf(fi, "# (File automatically created.)\n"); + + fclose(fi); + } +} diff --git a/src/high_scores.h b/src/high_scores.h new file mode 100644 index 000000000..1e7421f4a --- /dev/null +++ b/src/high_scores.h @@ -0,0 +1,12 @@ +/* + + by Adam Czachorowski + gislan@o2.pl + +*/ + +# include + +void save_hs(int score); +int load_hs(); +FILE * opendata(char * mode); -- 2.11.0