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"
32 #include "lispreader.h"
35 const char * highscore_filename = "/st_highscore.dat";
37 const char * highscore_filename = "/highscore";
41 std::string hs_name; /* highscores global variables*/
43 /* Load data from high score file: */
51 lisp_object_t* root_obj = 0;
52 fi = fopen(highscore_filename, "r");
55 perror(highscore_filename);
60 lisp_stream_init_file (&stream, fi);
61 root_obj = lisp_read (&stream);
63 if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
65 printf("HighScore: Parse Error in file %s", highscore_filename);
69 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-highscore") == 0)
71 LispReader reader(lisp_cdr(root_obj));
72 reader.read_int("score", &hs_score);
73 reader.read_string("name", &hs_name);
78 printf("name=%s\n", hs_name.c_str());
79 printf("score=%i\n\n", hs_score);
82 void save_hs(int score)
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())
105 blue_text->drawf("Congratulations", 0, 130, A_HMIDDLE, A_TOP, 2, NO_UPDATE);
106 blue_text->draw("Your score:", 150, 180, 1, NO_UPDATE);
107 sprintf(str, "%d", hs_score);
108 yellow_nums->draw(str, 350, 170, 1, NO_UPDATE);
110 Menu::current()->draw();
111 Menu::current()->action();
115 while(SDL_PollEvent(&event))
116 if(event.type == SDL_KEYDOWN)
117 Menu::current()->event(event);
119 switch (highscore_menu->check())
122 if(highscore_menu->item[0].input != NULL)
123 hs_name = highscore_menu->item[0].input;
134 std::string filename;
136 /* Save data file: */
137 filename = highscore_filename;
139 fcreatedir(filename.c_str());
140 if(fwriteable(filename.c_str()))
142 fi = fopen(filename.c_str(), "w");
145 perror(filename.c_str());
149 fprintf(fi,";SuperTux HighScores\n");
150 fprintf(fi,"(supertux-highscore\n");
152 /* Save title info: */
153 fprintf(fi," (name \"%s\")\n", hs_name.c_str());
155 /* Save the description: */
156 fprintf(fi," (score \"%i\")\n", hs_score);
163 fi = opendata(highscore_filename, "w");
166 fprintf(fi, "# Supertux highscore file\n\n");
168 fprintf(fi, "name=%s\n", hs_name);
169 fprintf(fi, "highscore=%d\n", hs_score);
171 fprintf(fi, "# (File automatically created.)\n");