very little fix. ;)
[supertux.git] / src / high_scores.c
1 /*
2  
3   by Adam Czachorowski
4   gislan@o2.pl
5  
6 */
7
8 /* Open the highscore file: */
9
10 #include <string.h>
11 #include <stdlib.h>
12
13 #include "globals.h"
14 #include "high_scores.h"
15 #include "menu.h"
16 #include "screen.h"
17 #include "texture.h"
18
19 FILE * opendata(char * mode)
20 {
21   char * filename;
22   FILE * fi;
23
24
25   filename = (char *) malloc(sizeof(char) * (strlen(st_dir) +
26                              strlen("/st_highscore.dat") + 1));
27
28   strcpy(filename, st_dir);
29   /* Open the high score file: */
30
31 #ifdef LINUX
32
33   strcat(filename, "/highscore");
34 #else
35 #ifdef WIN32
36
37   strcat(filename, "/st_highscore.dat");
38 #endif
39 #endif
40
41
42   /* Try opening the file: */
43
44   fi = fopen(filename, mode);
45   free( filename );
46
47   if (fi == NULL)
48     {
49       fprintf(stderr, "Warning: I could not open the high score file ");
50
51       if (strcmp(mode, "r") == 0)
52         fprintf(stderr, "for read!!!\n");
53       else if (strcmp(mode, "w") == 0)
54         fprintf(stderr, "for write!!!\n");
55
56     }
57
58   return(fi);
59 }
60
61 /* Load data from high score file: */
62
63 void load_hs(void)
64 {
65   FILE * fi;
66   char temp[128];
67   hs_score = 100;
68   int c, strl;
69   strcpy(hs_name, "Grandma\0");
70   c = 0;
71   
72   /* Try to open file: */
73
74   fi = opendata("r");
75   if (fi != NULL)
76     {
77       do
78         {
79           fgets(temp, sizeof(temp), fi);
80
81           if (!feof(fi))
82             {
83               temp[strlen(temp) - 1] = '\0';
84
85
86               /* Parse each line: */
87
88               if (strstr(temp, "highscore=") == temp)
89                 {
90                   hs_score = atoi(temp + 10);
91
92                   if (hs_score == 0)
93                     hs_score = 100;
94                 }
95               if (strstr(temp, "name=") == temp)
96                 {
97                   fprintf(stderr, "name found\n");
98                   strl = strlen("name=");
99                   hs_name[strl-1]='\0';
100                   for(c = strl; c < strlen(temp); c++)
101                     hs_name[c-strl] = temp[c];
102                 }
103             }
104         }
105       while (!feof(fi));
106
107       fclose(fi);
108     }
109 }
110
111 void save_hs(int score)
112 {
113   texture_type bkgd;
114   texture_load(&bkgd, DATA_PREFIX "/images/highscore/highscore.png", IGNORE_ALPHA);
115
116   hs_score = score;
117
118   /* ask for player's name */
119   menumenu = MENU_HIGHSCORE;
120   show_menu = 1;
121   SDL_Event event;
122   while(show_menu)
123     {
124       texture_draw_bg(&bkgd, NO_UPDATE);
125       drawmenu();
126       flipscreen();
127
128       while(SDL_PollEvent(&event))
129         if(event.type == SDL_KEYDOWN)
130           menu_event(&event.key.keysym);
131     }
132
133
134   FILE * fi;
135
136   /* Try to open file: */
137
138   fi = opendata("w");
139   if (fi != NULL)
140     {
141       fprintf(fi, "# Supertux highscore file\n\n");
142
143       fprintf(fi, "name=%s\n", hs_name);
144       fprintf(fi, "highscore=%d\n", hs_score);
145
146       fprintf(fi, "# (File automatically created.)\n");
147
148       fclose(fi);
149     }
150 }