- replaced #ifdef LINUX with #ifndef WIN32, should be easier to handle for other...
[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 c, i, strl;
67
68   hs_score = 100;
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                   for(c = strl, i = 0; c < strlen(temp); ++c, ++i)
100                     hs_name[i] = temp[c];
101                   hs_name[i]= '\0';
102                 }
103             }
104         }
105       while (!feof(fi));
106
107       fclose(fi);
108     }
109 }
110
111 void save_hs(int score)
112 {
113   char str[80];
114
115   texture_type bkgd;
116   SDL_Event event;
117   FILE * fi;
118
119
120   texture_load(&bkgd, DATA_PREFIX "/images/highscore/highscore.png", IGNORE_ALPHA);
121
122   hs_score = score;
123
124   menu_reset();
125   menu_set_current(&highscore_menu);
126
127   if(!highscore_menu.item[0].input)
128     highscore_menu.item[0].input = (char*) malloc(strlen(hs_name) + 1);
129
130   strcpy(highscore_menu.item[0].input,hs_name);
131
132   /* ask for player's name */
133   show_menu = 1;
134   while(show_menu)
135     {
136       texture_draw_bg(&bkgd, NO_UPDATE);
137
138       text_drawf(&blue_text, "Congratulations", 0, 130, A_HMIDDLE, A_TOP, 2, NO_UPDATE);
139       text_draw(&blue_text, "Your score:", 150, 180, 1, NO_UPDATE);
140       sprintf(str, "%d", hs_score);
141       text_draw(&yellow_nums, str, 350, 170, 1, NO_UPDATE);
142
143       menu_process_current();
144       flipscreen();
145
146       while(SDL_PollEvent(&event))
147         if(event.type == SDL_KEYDOWN)
148           menu_event(&event.key.keysym);
149
150       switch (menu_check(&highscore_menu))
151         {
152         case 0:
153           if(highscore_menu.item[0].input != NULL)
154             strcpy(hs_name, highscore_menu.item[0].input);
155           break;
156         }
157
158       SDL_Delay(25);
159     }
160
161
162   /* Try to open file: */
163
164   fi = opendata("w");
165   if (fi != NULL)
166     {
167       fprintf(fi, "# Supertux highscore file\n\n");
168
169       fprintf(fi, "name=%s\n", hs_name);
170       fprintf(fi, "highscore=%d\n", hs_score);
171
172       fprintf(fi, "# (File automatically created.)\n");
173
174       fclose(fi);
175     }
176 }