fixed save/load-game.
[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 int hs_score;
20 char hs_name[62]; /* highscores global variables*/
21
22 FILE * opendata(char * mode)
23 {
24   char * filename = NULL;
25   FILE * fi;
26
27
28   filename = (char *) malloc(sizeof(char) * (strlen(st_dir) +
29                              strlen("/st_highscore.dat") + 1));
30
31   strcpy(filename, st_dir);
32   /* Open the high score file: */
33
34 #ifdef LINUX
35
36   strcat(filename, "/highscore");
37 #else
38 #ifdef WIN32
39
40   strcat(filename, "/st_highscore.dat");
41 #endif
42 #endif
43
44
45   /* Try opening the file: */
46
47   fi = fopen(filename, mode);
48   free( filename );
49
50   if (fi == NULL)
51     {
52       fprintf(stderr, "Warning: I could not open the high score file ");
53
54       if (strcmp(mode, "r") == 0)
55         fprintf(stderr, "for read!!!\n");
56       else if (strcmp(mode, "w") == 0)
57         fprintf(stderr, "for write!!!\n");
58
59     }
60
61   return(fi);
62 }
63
64 /* Load data from high score file: */
65
66 void load_hs(void)
67 {
68   FILE * fi;
69   char temp[128];
70   int c, i, strl;
71   
72   hs_score = 100;
73   strcpy(hs_name, "Grandma\0");
74   c = 0;
75
76   /* Try to open file: */
77
78   fi = opendata("r");
79   if (fi != NULL)
80     {
81       do
82         {
83           fgets(temp, sizeof(temp), fi);
84
85           if (!feof(fi))
86             {
87               temp[strlen(temp) - 1] = '\0';
88
89
90               /* Parse each line: */
91
92               if (strstr(temp, "highscore=") == temp)
93                 {
94                   hs_score = atoi(temp + 10);
95
96                   if (hs_score == 0)
97                     hs_score = 100;
98                 }
99               if (strstr(temp, "name=") == temp)
100                 {
101                   fprintf(stderr, "name found\n");
102                   strl = strlen("name=");
103                   for(c = strl, i = 0; c < strlen(temp); ++c, ++i)
104                     hs_name[i] = temp[c];
105                   hs_name[i]= '\0';
106                 }
107             }
108         }
109       while (!feof(fi));
110
111       fclose(fi);
112     }
113 }
114
115 void save_hs(int score)
116 {
117   char str[80];
118
119   texture_type bkgd;
120   SDL_Event event;
121   FILE * fi;
122
123   
124   texture_load(&bkgd, DATA_PREFIX "/images/highscore/highscore.png", IGNORE_ALPHA);
125
126   hs_score = score;
127
128   menu_reset();
129   menu_set_current(&highscore_menu);
130
131   if(!highscore_menu.item[0].input)
132   highscore_menu.item[0].input = (char*) malloc(strlen(hs_name) + 1);
133   
134   strcpy(highscore_menu.item[0].input,hs_name);
135
136   /* ask for player's name */
137   show_menu = 1;
138   while(show_menu)
139     {
140       texture_draw_bg(&bkgd, NO_UPDATE);
141
142       text_drawf(&blue_text, "Congratulations", 0, 130, A_HMIDDLE, A_TOP, 2, NO_UPDATE);
143       text_draw(&blue_text, "Your score:", 150, 180, 1, NO_UPDATE);
144       sprintf(str, "%d", hs_score);
145       text_draw(&yellow_nums, str, 350, 170, 1, NO_UPDATE);
146
147       menu_process_current();
148       flipscreen();
149
150       while(SDL_PollEvent(&event))
151         if(event.type == SDL_KEYDOWN)
152           menu_event(&event.key.keysym);
153
154       switch (menu_check(&highscore_menu))
155         {
156         case 0:
157           if(highscore_menu.item[0].input != NULL)
158             strcpy(hs_name, highscore_menu.item[0].input);
159           break;
160         }
161
162       SDL_Delay(25);
163     }
164
165
166   /* Try to open file: */
167
168   fi = opendata("w");
169   if (fi != NULL)
170     {
171       fprintf(fi, "# Supertux highscore file\n\n");
172
173       fprintf(fi, "name=%s\n", hs_name);
174       fprintf(fi, "highscore=%d\n", hs_score);
175
176       fprintf(fi, "# (File automatically created.)\n");
177
178       fclose(fi);
179     }
180 }