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