- converted texture_type into a Surface class
[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   Surface* bkgd;
86   SDL_Event event;
87   FILE * fi;
88
89   bkgd = new Surface(datadir + "/images/highscore/highscore.png", IGNORE_ALPHA);
90
91   hs_score = score;
92
93   menu_reset();
94   Menu::set_current(highscore_menu);
95
96   if(!highscore_menu->item[0].input)
97     highscore_menu->item[0].input = (char*) malloc(strlen(hs_name) + 1);
98
99   strcpy(highscore_menu->item[0].input,hs_name);
100
101   /* ask for player's name */
102   show_menu = 1;
103   while(show_menu)
104     {
105       bkgd->draw_bg();
106
107       text_drawf(&blue_text, "Congratulations", 0, 130, A_HMIDDLE, A_TOP, 2, NO_UPDATE);
108       text_draw(&blue_text, "Your score:", 150, 180, 1, NO_UPDATE);
109       sprintf(str, "%d", hs_score);
110       text_draw(&yellow_nums, str, 350, 170, 1, NO_UPDATE);
111
112       menu_process_current();
113       flipscreen();
114
115       while(SDL_PollEvent(&event))
116         if(event.type == SDL_KEYDOWN)
117           current_menu->event(event);
118
119       switch (highscore_menu->check())
120         {
121         case 0:
122           if(highscore_menu->item[0].input != NULL)
123             strcpy(hs_name, highscore_menu->item[0].input);
124           break;
125         }
126
127       SDL_Delay(25);
128     }
129
130
131   /* Try to open file: */
132
133   fi = opendata(highscore_filename, "w");
134   if (fi != NULL)
135     {
136       fprintf(fi, "# Supertux highscore file\n\n");
137
138       fprintf(fi, "name=%s\n", hs_name);
139       fprintf(fi, "highscore=%d\n", hs_score);
140
141       fprintf(fi, "# (File automatically created.)\n");
142
143       fclose(fi);
144     }
145 }