[Super-tux-commit] supertux/src high_scores.cpp,1.11,1.12 high_scores.h,1.5,1.6
Brought to you by:
wkendrick
From: Ricardo C. <rm...@us...> - 2004-04-19 14:09:04
|
Update of /cvsroot/super-tux/supertux/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv18865/src Modified Files: high_scores.cpp high_scores.h Log Message: Made high_scores to use the lispreader (i wasn't able to do proper testing, could someone give a look at the code). Anyway, do you think high_scores.* should be merged with configfile.* ? If nobody says anything, I'll do that. Index: high_scores.cpp =================================================================== RCS file: /cvsroot/super-tux/supertux/src/high_scores.cpp,v retrieving revision 1.11 retrieving revision 1.12 diff -u -d -r1.11 -r1.12 --- high_scores.cpp 13 Apr 2004 12:25:22 -0000 1.11 +++ high_scores.cpp 19 Apr 2004 14:08:54 -0000 1.12 @@ -16,6 +16,7 @@ #include "screen.h" #include "texture.h" #include "setup.h" +#include "lispreader.h" #ifdef WIN32 const char * highscore_filename = "/st_highscore.dat"; @@ -24,58 +25,45 @@ #endif int hs_score; -char hs_name[62]; /* highscores global variables*/ +std::string hs_name; /* highscores global variables*/ /* Load data from high score file: */ void load_hs(void) { - FILE * fi; - char temp[128]; - int strl; - unsigned int i, c; - hs_score = 100; - strcpy(hs_name, "Grandma\0"); - c = 0; - - /* Try to open file: */ + hs_name = "Grandma"; - fi = opendata(highscore_filename, "r"); - if (fi != NULL) + FILE * fi; + lisp_object_t* root_obj = 0; + fi = fopen(highscore_filename, "r"); + if (fi == NULL) { - do - { - fgets(temp, sizeof(temp), fi); - - if (!feof(fi)) - { - temp[strlen(temp) - 1] = '\0'; + perror(highscore_filename); + return; + } + lisp_stream_t stream; + lisp_stream_init_file (&stream, fi); + root_obj = lisp_read (&stream); - /* Parse each line: */ + if (root_obj->type == LISP_TYPE_EOF || root_obj->type == LISP_TYPE_PARSE_ERROR) + { + printf("HighScore: Parse Error in file %s", highscore_filename); + } - 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); + if (strcmp(lisp_symbol(lisp_car(root_obj)), "supertux-highscore") == 0) + { + LispReader reader(lisp_cdr(root_obj)); + reader.read_int("score", &hs_score); + reader.read_string("name", &hs_name); } + + fclose(fi); + +printf("name=%s\n", hs_name.c_str()); +printf("score=%i\n\n", hs_score); } void save_hs(int score) @@ -84,7 +72,6 @@ Surface* bkgd; SDL_Event event; - FILE * fi; bkgd = new Surface(datadir + "/images/highscore/highscore.png", IGNORE_ALPHA); @@ -94,9 +81,9 @@ Menu::set_current(highscore_menu); if(!highscore_menu->item[0].input) - highscore_menu->item[0].input = (char*) malloc(strlen(hs_name) + 1); + highscore_menu->item[0].input = (char*) malloc(strlen(hs_name.c_str()) + 1); - strcpy(highscore_menu->item[0].input,hs_name); + strcpy(highscore_menu->item[0].input,hs_name.c_str()); /* ask for player's name */ show_menu = 1; @@ -120,7 +107,7 @@ { 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; } @@ -128,8 +115,38 @@ } - /* Try to open file: */ + /* Save to file: */ + + FILE* fi; + std::string filename; + + /* Save data file: */ + filename = highscore_filename; + fcreatedir(filename.c_str()); + if(fwriteable(filename.c_str())) + { + 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"); + + /* Save title info: */ + fprintf(fi," (name \"%s\")\n", hs_name.c_str()); + + /* Save the description: */ + fprintf(fi," (score \"%i\")\n", hs_score); + + fprintf( fi,")"); + fclose(fi); + } + +/* fi = opendata(highscore_filename, "w"); if (fi != NULL) { @@ -141,5 +158,5 @@ fprintf(fi, "# (File automatically created.)\n"); fclose(fi); - } + }*/ } Index: high_scores.h =================================================================== RCS file: /cvsroot/super-tux/supertux/src/high_scores.h,v retrieving revision 1.5 retrieving revision 1.6 diff -u -d -r1.5 -r1.6 --- high_scores.h 24 Mar 2004 14:35:11 -0000 1.5 +++ high_scores.h 19 Apr 2004 14:08:54 -0000 1.6 @@ -11,7 +11,7 @@ #include <stdio.h> extern int hs_score; -extern char hs_name[62]; /* highscores global variables*/ +extern std::string hs_name; /* highscores global variables*/ void save_hs(int score); void load_hs(); |