(Added high score code from Adam Czachorowski)
authorBill Kendrick <nbs@sonic.net>
Wed, 10 Dec 2003 08:17:26 +0000 (08:17 +0000)
committerBill Kendrick <nbs@sonic.net>
Wed, 10 Dec 2003 08:17:26 +0000 (08:17 +0000)
SVN-Revision: 12

src/high_scores.c [new file with mode: 0644]
src/high_scores.h [new file with mode: 0644]

diff --git a/src/high_scores.c b/src/high_scores.c
new file mode 100644 (file)
index 0000000..6112b6f
--- /dev/null
@@ -0,0 +1,121 @@
+/*
+
+  by Adam Czachorowski
+  gislan@o2.pl
+
+*/
+
+/* Open the highscore file: */
+
+# include <string.h>
+# include <stdlib.h>
+
+# 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 (file)
index 0000000..1e7421f
--- /dev/null
@@ -0,0 +1,12 @@
+/*
+
+  by Adam Czachorowski
+  gislan@o2.pl
+
+*/
+
+# include <stdio.h>
+
+void save_hs(int score);
+int load_hs();
+FILE * opendata(char * mode);