summary refs log tree commit diff stats
path: root/gamestate.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'gamestate.cpp')
-rw-r--r--gamestate.cpp45
1 files changed, 44 insertions, 1 deletions
diff --git a/gamestate.cpp b/gamestate.cpp index 323cac5..812f26c 100644 --- a/gamestate.cpp +++ b/gamestate.cpp
@@ -4,6 +4,8 @@
4#include "mazeoflife.h" 4#include "mazeoflife.h"
5#include "highscore.h" 5#include "highscore.h"
6#include "titlestate.h" 6#include "titlestate.h"
7#include <fstream>
8#include "hslist.h"
7 9
8class GameBoard { 10class GameBoard {
9 public: 11 public:
@@ -214,7 +216,48 @@ State* PlayGameState::operator() (SDL_Renderer* renderer)
214 } 216 }
215 217
216 case SDLK_ESCAPE: 218 case SDLK_ESCAPE:
217 return new TitleState(); 219 std::ifstream exists(getDataFile());
220 if (exists)
221 {
222 FILE* hslist = fopen(getDataFile(), "r");
223 int scores;
224 Highscore* h;
225
226 fscanf(hslist, "%d%*c", &scores);
227
228 if (scores < 10)
229 {
230 fclose(hslist);
231
232 return new EnterHighscoreState(level);
233 } else {
234 for (int i=0; i<scores; i++)
235 {
236 int namelen;
237 char namelens[4];
238 char* name = (char*) calloc(25, sizeof(char));
239 int score;
240
241 fscanf(hslist, "%d", &namelen);
242 sprintf(namelens, "%%%dc", namelen);
243 fscanf(hslist, namelens, name);
244 fscanf(hslist, "%d%*c", &score);
245
246 h = new Highscore(name, score);
247 }
248
249 fclose(hslist);
250
251 if (h->getLevel() < level)
252 {
253 return new EnterHighscoreState(level);
254 } else {
255 return new DisplayAndReturnLocalHighscoreListState();
256 }
257 }
258 } else {
259 return new EnterHighscoreState(level);
260 }
218 } 261 }
219 } 262 }
220 } 263 }