X-Git-Url: https://git.verplant.org/?a=blobdiff_plain;f=src%2Fhigh_scores.cpp;h=86b1eb699086ab285dc32a5313852a927c28fb4a;hb=ee6972038331a3c26a2a6a0bdb2baca25475b1d2;hp=d2e71020c8fa0ed7d4c7f2f6793898522a99e9fb;hpb=a00085c8846cd85e7c4fd004b32753a367ac684a;p=supertux.git diff --git a/src/high_scores.cpp b/src/high_scores.cpp index d2e71020c..86b1eb699 100644 --- a/src/high_scores.cpp +++ b/src/high_scores.cpp @@ -1,158 +1,129 @@ -/* - - by Adam Czachorowski - gislan@o2.pl - -*/ +// $Id$ +// +// SuperTux +// Copyright (C) 2004 Adam Czachorowski +// +// This program is free software; you can redistribute it and/or +// modify it under the terms of the GNU General Public License +// as published by the Free Software Foundation; either version 2 +// of the License, or (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program; if not, write to the Free Software +// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA +// 02111-1307, USA. /* Open the highscore file: */ -#include -#include - -#include "globals.h" -#include "high_scores.h" -#include "menu.h" -#include "screen.h" -#include "texture.h" - -int hs_score; -char hs_name[62]; /* highscores global variables*/ - -FILE * opendata(char * mode) -{ - char * filename = NULL; - FILE * fi; - - - filename = (char *) malloc(sizeof(char) * (strlen(st_dir) + - strlen("/st_highscore.dat") + 1)); +#include - strcpy(filename, st_dir); - /* Open the high score file: */ +#include +#include -#ifndef WIN32 - strcat(filename, "/highscore"); +#include "app/globals.h" +#include "high_scores.h" +#include "gui/menu.h" +#include "video/drawing_context.h" +#include "video/screen.h" +#include "video/surface.h" +#include "app/setup.h" +#include "utils/lispreader.h" +#include "resources.h" + +using namespace SuperTux; + +#ifdef WIN32 +const char * highscore_filename = "/st_highscore.dat"; #else - strcat(filename, "/st_highscore.dat"); +const char * highscore_filename = "/highscore"; #endif +int hs_score; +std::string hs_name; /* highscores global variables*/ - /* Try opening the file: */ +/* Load data from high score file: */ - fi = fopen(filename, mode); - free( filename ); +void load_hs(void) +{ + hs_score = 100; + hs_name = "Grandma"; + FILE * fi; + lisp_object_t* root_obj = 0; + fi = fopen(highscore_filename, "r"); if (fi == NULL) { - fprintf(stderr, "Warning: Unable to open the high score file "); - - if (strcmp(mode, "r") == 0) - fprintf(stderr, "for read!!!\n"); - else if (strcmp(mode, "w") == 0) - fprintf(stderr, "for write!!!\n"); - + perror(highscore_filename); + return; } - return(fi); -} - -/* Load data from high score file: */ - -void load_hs(void) -{ - FILE * fi; - char temp[128]; - int strl; - unsigned int i, c; + lisp_stream_t stream; + lisp_stream_init_file (&stream, fi); + root_obj = lisp_read (&stream); - hs_score = 100; - strcpy(hs_name, "Grandma\0"); - c = 0; + if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR) + { + printf("HighScore: Parse Error in file %s", highscore_filename); + } - /* Try to open file: */ - fi = opendata("r"); - if (fi != NULL) + if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-highscore") == 0) { - do - { - fgets(temp, sizeof(temp), fi); - - if (!feof(fi)) - { - temp[strlen(temp) - 1] = '\0'; - - - /* Parse each line: */ - - if (strstr(temp, "highscore=") == temp) - { - hs_score = atoi(temp + 10); - - if (hs_score == 0) - hs_score = 100; - } - if (strstr(temp, "name=") == temp) - { - fprintf(stderr, "name found\n"); - strl = strlen("name="); - for(c = strl, i = 0; c < strlen(temp); ++c, ++i) - hs_name[i] = temp[c]; - hs_name[i]= '\0'; - } - } - } - while (!feof(fi)); - - fclose(fi); + LispReader reader(lisp_cdr(root_obj)); + reader.read_int("score", hs_score); + reader.read_string("name", hs_name); } + + fclose(fi); + lisp_free(root_obj); } void save_hs(int score) { char str[80]; - texture_type bkgd; + Surface* bkgd; SDL_Event event; - FILE * fi; - - texture_load(&bkgd, datadir + "/images/highscore/highscore.png", IGNORE_ALPHA); + DrawingContext context; + bkgd = new Surface(datadir + "/images/highscore/highscore.png", false); hs_score = score; - menu_reset(); Menu::set_current(highscore_menu); - if(!highscore_menu->item[0].input) - highscore_menu->item[0].input = (char*) malloc(strlen(hs_name) + 1); - - strcpy(highscore_menu->item[0].input,hs_name); + highscore_menu->item[0].input = hs_name; /* ask for player's name */ - show_menu = 1; - while(show_menu) + while(Menu::current()) { - texture_draw_bg(&bkgd, NO_UPDATE); + context.draw_surface(bkgd, Vector(0, 0), LAYER_BACKGROUND0); - text_drawf(&blue_text, "Congratulations", 0, 130, A_HMIDDLE, A_TOP, 2, NO_UPDATE); - text_draw(&blue_text, "Your score:", 150, 180, 1, NO_UPDATE); + context.draw_text(blue_text, "Congratulations", + Vector(screen->w/2, 130), CENTER_ALLIGN, LAYER_FOREGROUND1); + context.draw_text(blue_text, "Your score:", Vector(150, 180), + LEFT_ALLIGN, LAYER_FOREGROUND1); sprintf(str, "%d", hs_score); - text_draw(&yellow_nums, str, 350, 170, 1, NO_UPDATE); + context.draw_text(yellow_nums, str, Vector(250, 170), LEFT_ALLIGN, LAYER_FOREGROUND1); + + Menu::current()->draw(context); + Menu::current()->action(); - menu_process_current(); - flipscreen(); + context.do_drawing(); while(SDL_PollEvent(&event)) if(event.type == SDL_KEYDOWN) - menu_event(&event.key.keysym); + Menu::current()->event(event); switch (highscore_menu->check()) { case 0: - if(highscore_menu->item[0].input != NULL) - strcpy(hs_name, highscore_menu->item[0].input); + hs_name = highscore_menu->item[0].input; break; } @@ -160,18 +131,34 @@ void save_hs(int score) } - /* Try to open file: */ + /* Save to file: */ - fi = opendata("w"); - if (fi != NULL) + FILE* fi; + std::string filename; + + /* Save data file: */ + filename = highscore_filename; + + FileSystem::fcreatedir(filename.c_str()); + if(FileSystem::fwriteable(filename.c_str())) { - fprintf(fi, "# Supertux highscore file\n\n"); + fi = fopen(filename.c_str(), "w"); + if (fi == NULL) + { + perror(filename.c_str()); + } + + /* Write header: */ + fprintf(fi,";SuperTux HighScores\n"); + fprintf(fi,"(supertux-highscore\n"); - fprintf(fi, "name=%s\n", hs_name); - fprintf(fi, "highscore=%d\n", hs_score); + /* Save title info: */ + fprintf(fi," (name \"%s\")\n", hs_name.c_str()); - fprintf(fi, "# (File automatically created.)\n"); + /* Save the description: */ + fprintf(fi," (score \"%i\")\n", hs_score); + fprintf( fi,")"); fclose(fi); } }