- removed menu_process_current()
[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::current()->action();
98       Menu::current()->draw();
99
100       flipscreen();
101
102       while(SDL_PollEvent(&event))
103         if(event.type == SDL_KEYDOWN)
104           Menu::current()->event(event);
105
106       switch (highscore_menu->check())
107         {
108         case 0:
109           if(highscore_menu->item[0].input != NULL)
110             hs_name = highscore_menu->item[0].input;
111           break;
112         }
113
114       SDL_Delay(25);
115     }
116
117
118   /* Save to file: */
119
120   FILE* fi;
121   std::string filename;
122
123   /* Save data file: */
124   filename = highscore_filename;
125
126   fcreatedir(filename.c_str());
127   if(fwriteable(filename.c_str()))
128     {
129       fi = fopen(filename.c_str(), "w");
130       if (fi == NULL)
131         {
132           perror(filename.c_str());
133         }
134
135       /* Write header: */
136       fprintf(fi,";SuperTux HighScores\n");
137       fprintf(fi,"(supertux-highscore\n");
138
139       /* Save title info: */
140       fprintf(fi,"  (name \"%s\")\n", hs_name.c_str());
141
142       /* Save the description: */
143       fprintf(fi,"  (score \"%i\")\n", hs_score);
144
145       fprintf( fi,")");
146       fclose(fi);
147     }
148
149 /*
150   fi = opendata(highscore_filename, "w");
151   if (fi != NULL)
152     {
153       fprintf(fi, "# Supertux highscore file\n\n");
154
155       fprintf(fi, "name=%s\n", hs_name);
156       fprintf(fi, "highscore=%d\n", hs_score);
157
158       fprintf(fi, "# (File automatically created.)\n");
159
160       fclose(fi);
161     }*/
162 }