more kinds of menu_event are handled directly in the menu-code now.
[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 #include "setup.h"
19
20 #ifdef WIN32
21 const char * highscore_filename = "/st_highscore.dat";
22 #else
23 const char * highscore_filename = "/highscore";
24 #endif
25
26 int hs_score;
27 char hs_name[62]; /* highscores global variables*/
28
29 /* Load data from high score file: */
30
31 void load_hs(void)
32 {
33   FILE * fi;
34   char temp[128];
35   int strl;
36   unsigned int i, c;
37
38   hs_score = 100;
39   strcpy(hs_name, "Grandma\0");
40   c = 0;
41
42   /* Try to open file: */
43
44   fi = opendata(highscore_filename, "r");
45   if (fi != NULL)
46     {
47       do
48         {
49           fgets(temp, sizeof(temp), fi);
50
51           if (!feof(fi))
52             {
53               temp[strlen(temp) - 1] = '\0';
54
55
56               /* Parse each line: */
57
58               if (strstr(temp, "highscore=") == temp)
59                 {
60                   hs_score = atoi(temp + 10);
61
62                   if (hs_score == 0)
63                     hs_score = 100;
64                 }
65               if (strstr(temp, "name=") == temp)
66                 {
67                   fprintf(stderr, "name found\n");
68                   strl = strlen("name=");
69                   for(c = strl, i = 0; c < strlen(temp); ++c, ++i)
70                     hs_name[i] = temp[c];
71                   hs_name[i]= '\0';
72                 }
73             }
74         }
75       while (!feof(fi));
76
77       fclose(fi);
78     }
79 }
80
81 void save_hs(int score)
82 {
83   char str[80];
84
85   texture_type bkgd;
86   SDL_Event event;
87   FILE * fi;
88
89
90   texture_load(&bkgd, datadir + "/images/highscore/highscore.png", IGNORE_ALPHA);
91
92   hs_score = score;
93
94   menu_reset();
95   Menu::set_current(highscore_menu);
96
97   if(!highscore_menu->item[0].input)
98     highscore_menu->item[0].input = (char*) malloc(strlen(hs_name) + 1);
99
100   strcpy(highscore_menu->item[0].input,hs_name);
101
102   /* ask for player's name */
103   show_menu = 1;
104   while(show_menu)
105     {
106       texture_draw_bg(&bkgd, NO_UPDATE);
107
108       text_drawf(&blue_text, "Congratulations", 0, 130, A_HMIDDLE, A_TOP, 2, NO_UPDATE);
109       text_draw(&blue_text, "Your score:", 150, 180, 1, NO_UPDATE);
110       sprintf(str, "%d", hs_score);
111       text_draw(&yellow_nums, str, 350, 170, 1, NO_UPDATE);
112
113       menu_process_current();
114       flipscreen();
115
116       while(SDL_PollEvent(&event))
117         if(event.type == SDL_KEYDOWN)
118           menu_event(event);
119
120       switch (highscore_menu->check())
121         {
122         case 0:
123           if(highscore_menu->item[0].input != NULL)
124             strcpy(hs_name, highscore_menu->item[0].input);
125           break;
126         }
127
128       SDL_Delay(25);
129     }
130
131
132   /* Try to open file: */
133
134   fi = opendata(highscore_filename, "w");
135   if (fi != NULL)
136     {
137       fprintf(fi, "# Supertux highscore file\n\n");
138
139       fprintf(fi, "name=%s\n", hs_name);
140       fprintf(fi, "highscore=%d\n", hs_score);
141
142       fprintf(fi, "# (File automatically created.)\n");
143
144       fclose(fi);
145     }
146 }