4 // Copyright (C) 2004 Adam Czachorowski <gislan@o2.pl>
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.
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.
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
21 /* Open the highscore file: */
28 #include "app/globals.h"
29 #include "high_scores.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"
38 using namespace SuperTux;
41 const char * highscore_filename = "/st_highscore.dat";
43 const char * highscore_filename = "/highscore";
47 std::string hs_name; /* highscores global variables*/
49 /* Load data from high score file: */
57 lisp_object_t* root_obj = 0;
58 fi = fopen(highscore_filename, "r");
61 perror(highscore_filename);
66 lisp_stream_init_file (&stream, fi);
67 root_obj = lisp_read (&stream);
69 if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
71 printf("HighScore: Parse Error in file %s", highscore_filename);
75 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-highscore") == 0)
77 LispReader reader(lisp_cdr(root_obj));
78 reader.read_int("score", hs_score);
79 reader.read_string("name", hs_name);
86 void save_hs(int score)
93 DrawingContext context;
94 bkgd = new Surface(datadir + "/images/highscore/highscore.png", false);
98 Menu::set_current(highscore_menu);
100 highscore_menu->item[0].input = hs_name;
102 /* ask for player's name */
103 while(Menu::current())
105 context.draw_surface(bkgd, Vector(0, 0), LAYER_BACKGROUND0);
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);
114 Menu::current()->draw(context);
115 Menu::current()->action();
117 context.do_drawing();
119 while(SDL_PollEvent(&event))
120 if(event.type == SDL_KEYDOWN)
121 Menu::current()->event(event);
123 switch (highscore_menu->check())
126 hs_name = highscore_menu->item[0].input;
137 std::string filename;
139 /* Save data file: */
140 filename = highscore_filename;
142 FileSystem::fcreatedir(filename.c_str());
143 if(FileSystem::fwriteable(filename.c_str()))
145 fi = fopen(filename.c_str(), "w");
148 perror(filename.c_str());
152 fprintf(fi,";SuperTux HighScores\n");
153 fprintf(fi,"(supertux-highscore\n");
155 /* Save title info: */
156 fprintf(fi," (name \"%s\")\n", hs_name.c_str());
158 /* Save the description: */
159 fprintf(fi," (score \"%i\")\n", hs_score);