8 /* Open the highscore file: */
14 #include "high_scores.h"
19 #include "lispreader.h"
22 const char * highscore_filename = "/st_highscore.dat";
24 const char * highscore_filename = "/highscore";
28 std::string hs_name; /* highscores global variables*/
30 /* Load data from high score file: */
38 lisp_object_t* root_obj = 0;
39 fi = fopen(highscore_filename, "r");
42 perror(highscore_filename);
47 lisp_stream_init_file (&stream, fi);
48 root_obj = lisp_read (&stream);
50 if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR)
52 printf("HighScore: Parse Error in file %s", highscore_filename);
56 if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-highscore") == 0)
58 LispReader reader(lisp_cdr(root_obj));
59 reader.read_int("score", &hs_score);
60 reader.read_string("name", &hs_name);
65 printf("name=%s\n", hs_name.c_str());
66 printf("score=%i\n\n", hs_score);
69 void save_hs(int score)
76 bkgd = new Surface(datadir + "/images/highscore/highscore.png", IGNORE_ALPHA);
81 Menu::set_current(highscore_menu);
83 if(!highscore_menu->item[0].input)
84 highscore_menu->item[0].input = (char*) malloc(strlen(hs_name.c_str()) + 1);
86 strcpy(highscore_menu->item[0].input,hs_name.c_str());
88 /* ask for player's name */
94 blue_text->drawf("Congratulations", 0, 130, A_HMIDDLE, A_TOP, 2, NO_UPDATE);
95 blue_text->draw("Your score:", 150, 180, 1, NO_UPDATE);
96 sprintf(str, "%d", hs_score);
97 yellow_nums->draw(str, 350, 170, 1, NO_UPDATE);
99 menu_process_current();
102 while(SDL_PollEvent(&event))
103 if(event.type == SDL_KEYDOWN)
104 current_menu->event(event);
106 switch (highscore_menu->check())
109 if(highscore_menu->item[0].input != NULL)
110 hs_name = highscore_menu->item[0].input;
121 std::string filename;
123 /* Save data file: */
124 filename = highscore_filename;
126 fcreatedir(filename.c_str());
127 if(fwriteable(filename.c_str()))
129 fi = fopen(filename.c_str(), "w");
132 perror(filename.c_str());
136 fprintf(fi,";SuperTux HighScores\n");
137 fprintf(fi,"(supertux-highscore\n");
139 /* Save title info: */
140 fprintf(fi," (name \"%s\")\n", hs_name.c_str());
142 /* Save the description: */
143 fprintf(fi," (score \"%i\")\n", hs_score);
150 fi = opendata(highscore_filename, "w");
153 fprintf(fi, "# Supertux highscore file\n\n");
155 fprintf(fi, "name=%s\n", hs_name);
156 fprintf(fi, "highscore=%d\n", hs_score);
158 fprintf(fi, "# (File automatically created.)\n");