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