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: */
27 #include "high_scores.h"
29 #include "screen/drawing_context.h"
30 #include "screen/screen.h"
31 #include "screen/texture.h"
33 #include "lispreader.h"
36 const char * highscore_filename = "/st_highscore.dat";
38 const char * highscore_filename = "/highscore";
42 std::string hs_name; /* highscores global variables*/
44 /* Load data from high score file: */
52 lisp_object_t* root_obj = 0;
53 fi = fopen(highscore_filename, "r");
56 perror(highscore_filename);
61 lisp_stream_init_file (&stream, fi);
62 root_obj = lisp_read (&stream);
64 if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
66 printf("HighScore: Parse Error in file %s", highscore_filename);
70 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-highscore") == 0)
72 LispReader reader(lisp_cdr(root_obj));
73 reader.read_int("score", hs_score);
74 reader.read_string("name", hs_name);
81 void save_hs(int score)
88 DrawingContext context;
89 bkgd = new Surface(datadir + "/images/highscore/highscore.png", IGNORE_ALPHA);
93 Menu::set_current(highscore_menu);
95 if(!highscore_menu->item[0].input)
96 highscore_menu->item[0].input = (char*) malloc(strlen(hs_name.c_str()) + 1);
98 strcpy(highscore_menu->item[0].input,hs_name.c_str());
100 /* ask for player's name */
101 while(Menu::current())
103 context.draw_surface(bkgd, Vector(0, 0), LAYER_BACKGROUND0);
105 context.draw_text_center(blue_text, "Congratulations",
106 Vector(0, 130), LAYER_FOREGROUND1);
107 context.draw_text(blue_text, "Your score:", Vector(150, 180),
109 sprintf(str, "%d", hs_score);
110 context.draw_text(yellow_nums, str, Vector(250, 170), 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 if(highscore_menu->item[0].input != NULL)
125 hs_name = highscore_menu->item[0].input;
136 std::string filename;
138 /* Save data file: */
139 filename = highscore_filename;
141 fcreatedir(filename.c_str());
142 if(fwriteable(filename.c_str()))
144 fi = fopen(filename.c_str(), "w");
147 perror(filename.c_str());
151 fprintf(fi,";SuperTux HighScores\n");
152 fprintf(fi,"(supertux-highscore\n");
154 /* Save title info: */
155 fprintf(fi," (name \"%s\")\n", hs_name.c_str());
157 /* Save the description: */
158 fprintf(fi," (score \"%i\")\n", hs_score);