- fixed bomb-dup bug (vector increases on add, and this changes I guess, so remove...
[supertux.git] / src / high_scores.cpp
1 //  $Id$
2 // 
3 //  SuperTux
4 //  Copyright (C) 2004 Adam Czachorowski <gislan@o2.pl>
5 //
6 //  This program is free software; you can redistribute it and/or
7 //  modify it under the terms of the GNU General Public License
8 //  as published by the Free Software Foundation; either version 2
9 //  of the License, or (at your option) any later version.
10 //
11 //  This program is distributed in the hope that it will be useful,
12 //  but WITHOUT ANY WARRANTY; without even the implied warranty of
13 //  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 //  GNU General Public License for more details.
15 // 
16 //  You should have received a copy of the GNU General Public License
17 //  along with this program; if not, write to the Free Software
18 //  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
19 //  02111-1307, USA.
20
21 /* Open the highscore file: */
22
23 #include <string.h>
24 #include <stdlib.h>
25
26 #include "globals.h"
27 #include "high_scores.h"
28 #include "menu.h"
29 #include "screen.h"
30 #include "texture.h"
31 #include "setup.h"
32 #include "lispreader.h"
33
34 #ifdef WIN32
35 const char * highscore_filename = "/st_highscore.dat";
36 #else
37 const char * highscore_filename = "/highscore";
38 #endif
39
40 int hs_score;
41 std::string hs_name; /* highscores global variables*/
42
43 /* Load data from high score file: */
44
45 void load_hs(void)
46 {
47   hs_score = 100;
48   hs_name  = "Grandma";
49
50   FILE * fi;
51   lisp_object_t* root_obj = 0;
52   fi = fopen(highscore_filename, "r");
53   if (fi == NULL)
54     {
55       perror(highscore_filename);
56       return;
57     }
58
59   lisp_stream_t stream;
60   lisp_stream_init_file (&stream, fi);
61   root_obj = lisp_read (&stream);
62
63   if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
64     {
65       printf("HighScore: Parse Error in file %s", highscore_filename);
66     }
67
68
69   if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-highscore") == 0)
70     {
71       LispReader reader(lisp_cdr(root_obj));
72       reader.read_int("score",  &hs_score);
73       reader.read_string("name", &hs_name);
74     }
75  
76   fclose(fi);
77
78 printf("name=%s\n", hs_name.c_str());
79 printf("score=%i\n\n", hs_score);
80 }
81
82 void save_hs(int score)
83 {
84   char str[80];
85
86   Surface* bkgd;
87   SDL_Event event;
88
89   bkgd = new Surface(datadir + "/images/highscore/highscore.png", IGNORE_ALPHA);
90
91   hs_score = score;
92
93   Menu::set_current(highscore_menu);
94
95   if(!highscore_menu->item[0].input)
96     highscore_menu->item[0].input = (char*) malloc(strlen(hs_name.c_str()) + 1);
97
98   strcpy(highscore_menu->item[0].input,hs_name.c_str());
99
100   /* ask for player's name */
101   while(Menu::current())
102     {
103       bkgd->draw_bg();
104
105       blue_text->drawf("Congratulations", 0, 130, A_HMIDDLE, A_TOP, 2, NO_UPDATE);
106       blue_text->draw("Your score:", 150, 180, 1, NO_UPDATE);
107       sprintf(str, "%d", hs_score);
108       yellow_nums->draw(str, 350, 170, 1, NO_UPDATE);
109
110       Menu::current()->draw();
111       Menu::current()->action();
112
113       flipscreen();
114
115       while(SDL_PollEvent(&event))
116         if(event.type == SDL_KEYDOWN)
117           Menu::current()->event(event);
118
119       switch (highscore_menu->check())
120         {
121         case 0:
122           if(highscore_menu->item[0].input != NULL)
123             hs_name = highscore_menu->item[0].input;
124           break;
125         }
126
127       SDL_Delay(25);
128     }
129
130
131   /* Save to file: */
132
133   FILE* fi;
134   std::string filename;
135
136   /* Save data file: */
137   filename = highscore_filename;
138
139   fcreatedir(filename.c_str());
140   if(fwriteable(filename.c_str()))
141     {
142       fi = fopen(filename.c_str(), "w");
143       if (fi == NULL)
144         {
145           perror(filename.c_str());
146         }
147
148       /* Write header: */
149       fprintf(fi,";SuperTux HighScores\n");
150       fprintf(fi,"(supertux-highscore\n");
151
152       /* Save title info: */
153       fprintf(fi,"  (name \"%s\")\n", hs_name.c_str());
154
155       /* Save the description: */
156       fprintf(fi,"  (score \"%i\")\n", hs_score);
157
158       fprintf( fi,")");
159       fclose(fi);
160     }
161
162 /*
163   fi = opendata(highscore_filename, "w");
164   if (fi != NULL)
165     {
166       fprintf(fi, "# Supertux highscore file\n\n");
167
168       fprintf(fi, "name=%s\n", hs_name);
169       fprintf(fi, "highscore=%d\n", hs_score);
170
171       fprintf(fi, "# (File automatically created.)\n");
172
173       fclose(fi);
174     }*/
175 }