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);
80 void save_hs(int score)
87 bkgd = new Surface(datadir + "/images/highscore/highscore.png", IGNORE_ALPHA);
91 Menu::set_current(highscore_menu);
93 if(!highscore_menu->item[0].input)
94 highscore_menu->item[0].input = (char*) malloc(strlen(hs_name.c_str()) + 1);
96 strcpy(highscore_menu->item[0].input,hs_name.c_str());
98 /* ask for player's name */
99 while(Menu::current())
103 blue_text->drawf("Congratulations", 0, 130, A_HMIDDLE, A_TOP, 2, NO_UPDATE);
104 blue_text->draw("Your score:", 150, 180, 1, NO_UPDATE);
105 sprintf(str, "%d", hs_score);
106 yellow_nums->draw(str, 350, 170, 1, NO_UPDATE);
108 Menu::current()->draw();
109 Menu::current()->action();
113 while(SDL_PollEvent(&event))
114 if(event.type == SDL_KEYDOWN)
115 Menu::current()->event(event);
117 switch (highscore_menu->check())
120 if(highscore_menu->item[0].input != NULL)
121 hs_name = highscore_menu->item[0].input;
132 std::string filename;
134 /* Save data file: */
135 filename = highscore_filename;
137 fcreatedir(filename.c_str());
138 if(fwriteable(filename.c_str()))
140 fi = fopen(filename.c_str(), "w");
143 perror(filename.c_str());
147 fprintf(fi,";SuperTux HighScores\n");
148 fprintf(fi,"(supertux-highscore\n");
150 /* Save title info: */
151 fprintf(fi," (name \"%s\")\n", hs_name.c_str());
153 /* Save the description: */
154 fprintf(fi," (score \"%i\")\n", hs_score);
161 fi = opendata(highscore_filename, "w");
164 fprintf(fi, "# Supertux highscore file\n\n");
166 fprintf(fi, "name=%s\n", hs_name);
167 fprintf(fi, "highscore=%d\n", hs_score);
169 fprintf(fi, "# (File automatically created.)\n");