8 /* Open the highscore file: */
14 #include "high_scores.h"
20 char hs_name[62]; /* highscores global variables*/
22 FILE * opendata(char * mode)
24 char * filename = NULL;
28 filename = (char *) malloc(sizeof(char) * (strlen(st_dir) +
29 strlen("/st_highscore.dat") + 1));
31 strcpy(filename, st_dir);
32 /* Open the high score file: */
35 strcat(filename, "/highscore");
37 strcat(filename, "/st_highscore.dat");
41 /* Try opening the file: */
43 fi = fopen(filename, mode);
48 fprintf(stderr, "Warning: Unable to open the high score file ");
50 if (strcmp(mode, "r") == 0)
51 fprintf(stderr, "for read!!!\n");
52 else if (strcmp(mode, "w") == 0)
53 fprintf(stderr, "for write!!!\n");
60 /* Load data from high score file: */
70 strcpy(hs_name, "Grandma\0");
73 /* Try to open file: */
80 fgets(temp, sizeof(temp), fi);
84 temp[strlen(temp) - 1] = '\0';
87 /* Parse each line: */
89 if (strstr(temp, "highscore=") == temp)
91 hs_score = atoi(temp + 10);
96 if (strstr(temp, "name=") == temp)
98 fprintf(stderr, "name found\n");
99 strl = strlen("name=");
100 for(c = strl, i = 0; c < strlen(temp); ++c, ++i)
101 hs_name[i] = temp[c];
112 void save_hs(int score)
121 texture_load(&bkgd, DATA_PREFIX "/images/highscore/highscore.png", IGNORE_ALPHA);
126 menu_set_current(&highscore_menu);
128 if(!highscore_menu.item[0].input)
129 highscore_menu.item[0].input = (char*) malloc(strlen(hs_name) + 1);
131 strcpy(highscore_menu.item[0].input,hs_name);
133 /* ask for player's name */
137 texture_draw_bg(&bkgd, NO_UPDATE);
139 text_drawf(&blue_text, "Congratulations", 0, 130, A_HMIDDLE, A_TOP, 2, NO_UPDATE);
140 text_draw(&blue_text, "Your score:", 150, 180, 1, NO_UPDATE);
141 sprintf(str, "%d", hs_score);
142 text_draw(&yellow_nums, str, 350, 170, 1, NO_UPDATE);
144 menu_process_current();
147 while(SDL_PollEvent(&event))
148 if(event.type == SDL_KEYDOWN)
149 menu_event(&event.key.keysym);
151 switch (menu_check(&highscore_menu))
154 if(highscore_menu.item[0].input != NULL)
155 strcpy(hs_name, highscore_menu.item[0].input);
163 /* Try to open file: */
168 fprintf(fi, "# Supertux highscore file\n\n");
170 fprintf(fi, "name=%s\n", hs_name);
171 fprintf(fi, "highscore=%d\n", hs_score);
173 fprintf(fi, "# (File automatically created.)\n");