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: */
26 #include "app/globals.h"
27 #include "high_scores.h"
29 #include "video/drawing_context.h"
30 #include "video/screen.h"
31 #include "video/surface.h"
32 #include "app/setup.h"
33 #include "utils/lispreader.h"
34 #include "resources.h"
36 using namespace SuperTux;
39 const char * highscore_filename = "/st_highscore.dat";
41 const char * highscore_filename = "/highscore";
45 std::string hs_name; /* highscores global variables*/
47 /* Load data from high score file: */
55 lisp_object_t* root_obj = 0;
56 fi = fopen(highscore_filename, "r");
59 perror(highscore_filename);
64 lisp_stream_init_file (&stream, fi);
65 root_obj = lisp_read (&stream);
67 if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
69 printf("HighScore: Parse Error in file %s", highscore_filename);
73 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-highscore") == 0)
75 LispReader reader(lisp_cdr(root_obj));
76 reader.read_int("score", hs_score);
77 reader.read_string("name", hs_name);
84 void save_hs(int score)
91 DrawingContext context;
92 bkgd = new Surface(datadir + "/images/highscore/highscore.png", false);
96 Menu::set_current(highscore_menu);
98 highscore_menu->item[0].input = hs_name;
100 /* ask for player's name */
101 while(Menu::current())
103 context.draw_surface(bkgd, Vector(0, 0), LAYER_BACKGROUND0);
105 context.draw_text(blue_text, "Congratulations",
106 Vector(screen->w/2, 130), CENTER_ALLIGN, LAYER_FOREGROUND1);
107 context.draw_text(blue_text, "Your score:", Vector(150, 180),
108 LEFT_ALLIGN, LAYER_FOREGROUND1);
109 sprintf(str, "%d", hs_score);
110 context.draw_text(yellow_nums, str, Vector(250, 170), LEFT_ALLIGN, LAYER_FOREGROUND1);
112 Menu::current()->draw(context);
113 Menu::current()->action();
115 context.do_drawing();
117 while(SDL_PollEvent(&event))
118 if(event.type == SDL_KEYDOWN)
119 Menu::current()->event(event);
121 switch (highscore_menu->check())
124 hs_name = highscore_menu->item[0].input;
135 std::string filename;
137 /* Save data file: */
138 filename = highscore_filename;
140 FileSystem::fcreatedir(filename.c_str());
141 if(FileSystem::fwriteable(filename.c_str()))
143 fi = fopen(filename.c_str(), "w");
146 perror(filename.c_str());
150 fprintf(fi,";SuperTux HighScores\n");
151 fprintf(fi,"(supertux-highscore\n");
153 /* Save title info: */
154 fprintf(fi," (name \"%s\")\n", hs_name.c_str());
156 /* Save the description: */
157 fprintf(fi," (score \"%i\")\n", hs_score);