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: */
36 strcat(filename, "/highscore");
40 strcat(filename, "/st_highscore.dat");
45 /* Try opening the file: */
47 fi = fopen(filename, mode);
52 fprintf(stderr, "Warning: I could not open the high score file ");
54 if (strcmp(mode, "r") == 0)
55 fprintf(stderr, "for read!!!\n");
56 else if (strcmp(mode, "w") == 0)
57 fprintf(stderr, "for write!!!\n");
64 /* Load data from high score file: */
73 strcpy(hs_name, "Grandma\0");
76 /* Try to open file: */
83 fgets(temp, sizeof(temp), fi);
87 temp[strlen(temp) - 1] = '\0';
90 /* Parse each line: */
92 if (strstr(temp, "highscore=") == temp)
94 hs_score = atoi(temp + 10);
99 if (strstr(temp, "name=") == temp)
101 fprintf(stderr, "name found\n");
102 strl = strlen("name=");
103 for(c = strl, i = 0; c < strlen(temp); ++c, ++i)
104 hs_name[i] = temp[c];
115 void save_hs(int score)
124 texture_load(&bkgd, DATA_PREFIX "/images/highscore/highscore.png", IGNORE_ALPHA);
129 menu_set_current(&highscore_menu);
131 if(!highscore_menu.item[0].input)
132 highscore_menu.item[0].input = (char*) malloc(strlen(hs_name) + 1);
134 strcpy(highscore_menu.item[0].input,hs_name);
136 /* ask for player's name */
140 texture_draw_bg(&bkgd, NO_UPDATE);
142 text_drawf(&blue_text, "Congratulations", 0, 130, A_HMIDDLE, A_TOP, 2, NO_UPDATE);
143 text_draw(&blue_text, "Your score:", 150, 180, 1, NO_UPDATE);
144 sprintf(str, "%d", hs_score);
145 text_draw(&yellow_nums, str, 350, 170, 1, NO_UPDATE);
147 menu_process_current();
150 while(SDL_PollEvent(&event))
151 if(event.type == SDL_KEYDOWN)
152 menu_event(&event.key.keysym);
154 switch (menu_check(&highscore_menu))
157 if(highscore_menu.item[0].input != NULL)
158 strcpy(hs_name, highscore_menu.item[0].input);
166 /* Try to open file: */
171 fprintf(fi, "# Supertux highscore file\n\n");
173 fprintf(fi, "name=%s\n", hs_name);
174 fprintf(fi, "highscore=%d\n", hs_score);
176 fprintf(fi, "# (File automatically created.)\n");