moved savelevel() code to level.h/c where it belongs to. :)
[supertux.git] / src / high_scores.c
index 6112b6f..4327b83 100644 (file)
@@ -1,73 +1,74 @@
 /*
-
   by Adam Czachorowski
   gislan@o2.pl
-
 */
 
 /* Open the highscore file: */
 
-# include <string.h>
-# include <stdlib.h>
+#include <string.h>
+#include <stdlib.h>
 
-# include "high_scores.h"
+#include "globals.h"
+#include "high_scores.h"
+#include "menu.h"
+#include "screen.h"
+#include "texture.h"
 
 FILE * opendata(char * mode)
 {
-  char * filename, * home;
+  char * filename;
   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));
+  filename = (char *) malloc(sizeof(char) * (strlen(st_dir) +
+                             strlen("/st_highscore.dat") + 1));
 
-  strcpy(filename, home);
+  strcpy(filename, st_dir);
   /* Open the high score file: */
 
-      strcat(filename, "/.supertux");
+#ifdef LINUX
+
+  strcat(filename, "/highscore");
 #else
-      filename = "supertux.dat";
-#endif
+#ifdef WIN32
 
+  strcat(filename, "/st_highscore.dat");
+#endif
+#endif
 
-      /* Try opening the file: */
 
-      fi = fopen(filename, mode);
+  /* Try opening the file: */
 
-      if (fi == NULL)
-       {
-         fprintf(stderr, "Warning: I could not open the high score file ");
+  fi = fopen(filename, mode);
+  free( filename );
 
-         if (strcmp(mode, "r") == 0)
-           fprintf(stderr, "for read!!!\n");
-         else if (strcmp(mode, "w") == 0)
-           fprintf(stderr, "for write!!!\n");
+  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);
     }
 
+  return(fi);
+}
+
 /* Load data from high score file: */
 
-int load_hs(void)
+void load_hs(void)
 {
   FILE * fi;
   char temp[128];
-  int score = 100;
-
+  hs_score = 100;
+  int c, strl;
+  strcpy(hs_name, "Grandma\0");
+  c = 0;
+  
   /* Try to open file: */
 
   fi = opendata("r");
@@ -86,33 +87,61 @@ int load_hs(void)
 
               if (strstr(temp, "highscore=") == temp)
                 {
-                  score = atoi(temp + 10);
+                  hs_score = atoi(temp + 10);
 
-                  if (score == 0)
-                    score = 100;
-               }
+                  if (hs_score == 0)
+                    hs_score = 100;
+                }
+              if (strstr(temp, "name=") == temp)
+                {
+                  fprintf(stderr, "name found\n");
+                  strl = strlen("name=");
+                 hs_name[strl-1]='\0';
+                  for(c = strl; c < strlen(temp); c++)
+                    hs_name[c-strl] = temp[c];
+                }
             }
         }
       while (!feof(fi));
 
       fclose(fi);
     }
-  return score;
 }
 
 void save_hs(int score)
 {
-  FILE * fi;
+  texture_type bkgd;
+  texture_load(&bkgd, DATA_PREFIX "/images/highscore/highscore.png", IGNORE_ALPHA);
+
+  hs_score = score;
 
+  /* ask for player's name */
+  menumenu = MENU_HIGHSCORE;
+  show_menu = 1;
+  SDL_Event event;
+  while(show_menu)
+    {
+      texture_draw_bg(&bkgd, NO_UPDATE);
+      drawmenu();
+      flipscreen();
+
+      while(SDL_PollEvent(&event))
+        if(event.type == SDL_KEYDOWN)
+          menu_event(&event.key.keysym);
+    }
+
+
+  FILE * fi;
 
   /* Try to open file: */
 
   fi = opendata("w");
   if (fi != NULL)
     {
-      fprintf(fi, "# Supertux data file\n\n");
+      fprintf(fi, "# Supertux highscore file\n\n");
 
-      fprintf(fi, "highscore=%d\n", score);
+      fprintf(fi, "name=%s\n", hs_name);
+      fprintf(fi, "highscore=%d\n", hs_score);
 
       fprintf(fi, "# (File automatically created.)\n");