d50aea006e8f5fa944a5b6d8366882458c10ed79
[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 #include "lispreader.h"
20
21 #ifdef WIN32
22 const char * highscore_filename = "/st_highscore.dat";
23 #else
24 const char * highscore_filename = "/highscore";
25 #endif
26
27 int hs_score;
28 std::string hs_name; /* highscores global variables*/
29
30 /* Load data from high score file: */
31
32 void load_hs(void)
33 {
34   hs_score = 100;
35   hs_name  = "Grandma";
36
37   FILE * fi;
38   lisp_object_t* root_obj = 0;
39   fi = fopen(highscore_filename, "r");
40   if (fi == NULL)
41     {
42       perror(highscore_filename);
43       return;
44     }
45
46   lisp_stream_t stream;
47   lisp_stream_init_file (&stream, fi);
48   root_obj = lisp_read (&stream);
49
50   if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
51     {
52       printf("HighScore: Parse Error in file %s", highscore_filename);
53     }
54
55
56   if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-highscore") == 0)
57     {
58       LispReader reader(lisp_cdr(root_obj));
59       reader.read_int("score",  &hs_score);
60       reader.read_string("name", &hs_name);
61     }
62  
63   fclose(fi);
64
65 printf("name=%s\n", hs_name.c_str());
66 printf("score=%i\n\n", hs_score);
67 }
68
69 void save_hs(int score)
70 {
71   char str[80];
72
73   Surface* bkgd;
74   SDL_Event event;
75
76   bkgd = new Surface(datadir + "/images/highscore/highscore.png", IGNORE_ALPHA);
77
78   hs_score = score;
79
80   Menu::set_current(highscore_menu);
81
82   if(!highscore_menu->item[0].input)
83     highscore_menu->item[0].input = (char*) malloc(strlen(hs_name.c_str()) + 1);
84
85   strcpy(highscore_menu->item[0].input,hs_name.c_str());
86
87   /* ask for player's name */
88   while(Menu::current())
89     {
90       bkgd->draw_bg();
91
92       blue_text->drawf("Congratulations", 0, 130, A_HMIDDLE, A_TOP, 2, NO_UPDATE);
93       blue_text->draw("Your score:", 150, 180, 1, NO_UPDATE);
94       sprintf(str, "%d", hs_score);
95       yellow_nums->draw(str, 350, 170, 1, NO_UPDATE);
96
97       menu_process_current();
98       flipscreen();
99
100       while(SDL_PollEvent(&event))
101         if(event.type == SDL_KEYDOWN)
102           Menu::current()->event(event);
103
104       switch (highscore_menu->check())
105         {
106         case 0:
107           if(highscore_menu->item[0].input != NULL)
108             hs_name = highscore_menu->item[0].input;
109           break;
110         }
111
112       SDL_Delay(25);
113     }
114
115
116   /* Save to file: */
117
118   FILE* fi;
119   std::string filename;
120
121   /* Save data file: */
122   filename = highscore_filename;
123
124   fcreatedir(filename.c_str());
125   if(fwriteable(filename.c_str()))
126     {
127       fi = fopen(filename.c_str(), "w");
128       if (fi == NULL)
129         {
130           perror(filename.c_str());
131         }
132
133       /* Write header: */
134       fprintf(fi,";SuperTux HighScores\n");
135       fprintf(fi,"(supertux-highscore\n");
136
137       /* Save title info: */
138       fprintf(fi,"  (name \"%s\")\n", hs_name.c_str());
139
140       /* Save the description: */
141       fprintf(fi,"  (score \"%i\")\n", hs_score);
142
143       fprintf( fi,")");
144       fclose(fi);
145     }
146
147 /*
148   fi = opendata(highscore_filename, "w");
149   if (fi != NULL)
150     {
151       fprintf(fi, "# Supertux highscore file\n\n");
152
153       fprintf(fi, "name=%s\n", hs_name);
154       fprintf(fi, "highscore=%d\n", hs_score);
155
156       fprintf(fi, "# (File automatically created.)\n");
157
158       fclose(fi);
159     }*/
160 }