Changed up and down to previous and next. And added up and down icons.
[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   lisp_free(root_obj);
78 }
79
80 void save_hs(int score)
81 {
82   char str[80];
83
84   Surface* bkgd;
85   SDL_Event event;
86
87   bkgd = new Surface(datadir + "/images/highscore/highscore.png", IGNORE_ALPHA);
88
89   hs_score = score;
90
91   Menu::set_current(highscore_menu);
92
93   if(!highscore_menu->item[0].input)
94     highscore_menu->item[0].input = (char*) malloc(strlen(hs_name.c_str()) + 1);
95
96   strcpy(highscore_menu->item[0].input,hs_name.c_str());
97
98   /* ask for player's name */
99   while(Menu::current())
100     {
101       bkgd->draw_bg();
102
103       blue_text->drawf("Congratulations", 0, 130, A_HMIDDLE, A_TOP, 2, NO_UPDATE);
104       blue_text->draw("Your score:", 150, 180, 1, NO_UPDATE);
105       sprintf(str, "%d", hs_score);
106       yellow_nums->draw(str, 350, 170, 1, NO_UPDATE);
107
108       Menu::current()->draw();
109       Menu::current()->action();
110
111       flipscreen();
112
113       while(SDL_PollEvent(&event))
114         if(event.type == SDL_KEYDOWN)
115           Menu::current()->event(event);
116
117       switch (highscore_menu->check())
118         {
119         case 0:
120           if(highscore_menu->item[0].input != NULL)
121             hs_name = highscore_menu->item[0].input;
122           break;
123         }
124
125       SDL_Delay(25);
126     }
127
128
129   /* Save to file: */
130
131   FILE* fi;
132   std::string filename;
133
134   /* Save data file: */
135   filename = highscore_filename;
136
137   fcreatedir(filename.c_str());
138   if(fwriteable(filename.c_str()))
139     {
140       fi = fopen(filename.c_str(), "w");
141       if (fi == NULL)
142         {
143           perror(filename.c_str());
144         }
145
146       /* Write header: */
147       fprintf(fi,";SuperTux HighScores\n");
148       fprintf(fi,"(supertux-highscore\n");
149
150       /* Save title info: */
151       fprintf(fi,"  (name \"%s\")\n", hs_name.c_str());
152
153       /* Save the description: */
154       fprintf(fi,"  (score \"%i\")\n", hs_score);
155
156       fprintf( fi,")");
157       fclose(fi);
158     }
159
160 /*
161   fi = opendata(highscore_filename, "w");
162   if (fi != NULL)
163     {
164       fprintf(fi, "# Supertux highscore file\n\n");
165
166       fprintf(fi, "name=%s\n", hs_name);
167       fprintf(fi, "highscore=%d\n", hs_score);
168
169       fprintf(fi, "# (File automatically created.)\n");
170
171       fclose(fi);
172     }*/
173 }