summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorStarla Insigna <starla4444@gmail.com>2013-08-28 11:44:12 -0400
committerStarla Insigna <starla4444@gmail.com>2013-08-28 11:44:12 -0400
commit992f4eb1e6d711c1be097ffb1684df9a23e08d54 (patch)
treeec8ade2dca35fb42f179e21a54cf9a64608011f1
parent4c0b84a69ff30c4aa91490ea1c16300f1017f845 (diff)
downloadmazeoflife-992f4eb1e6d711c1be097ffb1684df9a23e08d54.tar.gz
mazeoflife-992f4eb1e6d711c1be097ffb1684df9a23e08d54.tar.bz2
mazeoflife-992f4eb1e6d711c1be097ffb1684df9a23e08d54.zip
Added highscore list viewing
-rw-r--r--gamestate.cpp5
-rw-r--r--hslist.cpp209
-rw-r--r--hslist.h25
-rw-r--r--resources/hlo_rtm.bmpbin76938 -> 76856 bytes
-rw-r--r--titlestate.cpp3
5 files changed, 234 insertions, 8 deletions
diff --git a/gamestate.cpp b/gamestate.cpp index c6d12fd..323cac5 100644 --- a/gamestate.cpp +++ b/gamestate.cpp
@@ -2,6 +2,8 @@
2#include <SDL_ttf.h> 2#include <SDL_ttf.h>
3#include "util.h" 3#include "util.h"
4#include "mazeoflife.h" 4#include "mazeoflife.h"
5#include "highscore.h"
6#include "titlestate.h"
5 7
6class GameBoard { 8class GameBoard {
7 public: 9 public:
@@ -210,6 +212,9 @@ State* PlayGameState::operator() (SDL_Renderer* renderer)
210 } else { 212 } else {
211 break; 213 break;
212 } 214 }
215
216 case SDLK_ESCAPE:
217 return new TitleState();
213 } 218 }
214 } 219 }
215 } 220 }
diff --git a/hslist.cpp b/hslist.cpp index 80e0801..3d9f19a 100644 --- a/hslist.cpp +++ b/hslist.cpp
@@ -4,6 +4,7 @@
4#include <sstream> 4#include <sstream>
5#include <fstream> 5#include <fstream>
6#include "util.h" 6#include "util.h"
7#include "titlestate.h"
7 8
8SDL_Surface* HighscoreList::render() 9SDL_Surface* HighscoreList::render()
9{ 10{
@@ -20,8 +21,8 @@ SDL_Surface* HighscoreList::render()
20 Highscore h = hslist[i]; 21 Highscore h = hslist[i];
21 22
22 int posw, posh; 23 int posw, posh;
23 char pos[3]; // 2 max characters in rank plus the colon at the end 24 char pos[3]; // 2 max characters in rank plus the colon at the end
24 sprintf(pos, "%d:", h.getRank()); 25 sprintf(pos, "%d:", h.getRank());
25 TTF_SizeText(posFont, pos, &posw, &posh); 26 TTF_SizeText(posFont, pos, &posw, &posh);
26 SDL_Rect posSpace = {0, (i+1)*40, posw, posh}; 27 SDL_Rect posSpace = {0, (i+1)*40, posw, posh};
27 SDL_BlitSurface(TTF_RenderText_Blended(posFont, pos, fontColor), NULL, tmp, &posSpace); 28 SDL_BlitSurface(TTF_RenderText_Blended(posFont, pos, fontColor), NULL, tmp, &posSpace);
@@ -96,7 +97,7 @@ std::vector<Highscore> HighscoreList::getGlobalHighscores()
96 throw 2; 97 throw 2;
97 } 98 }
98 99
99 char* headers = "GET /mol/hslist.php HTTP/1.1\nHost: other.fourisland.com\nUser-Agent: Maze Of Life v2.0\nAccept: text/plain\nKeep-Alive: 300\nConnection: keep-alive\n\n"; 100 const char* headers = "GET /mol/hslist.php HTTP/1.1\nHost: other.fourisland.com\nUser-Agent: Maze Of Life v3.0\nAccept: text/plain\nKeep-Alive: 300\nConnection: keep-alive\n\n";
100 if (SDLNet_TCP_Send(tcpsock, headers, strlen(headers)+1) < strlen(headers)) 101 if (SDLNet_TCP_Send(tcpsock, headers, strlen(headers)+1) < strlen(headers))
101 { 102 {
102 printf("Connection closed by peer: %s\n", SDLNet_GetError()); 103 printf("Connection closed by peer: %s\n", SDLNet_GetError());
@@ -135,7 +136,7 @@ std::vector<Highscore> HighscoreList::getGlobalHighscores()
135 136
136 if (sscanf(temps, "%d", &namelen) != 1) 137 if (sscanf(temps, "%d", &namelen) != 1)
137 { 138 {
138 printf("Recieved data is of an invalid format: %s\n", temps); 139 printf("Recieved data is of an invalid format (1-%d): %s\n", i, temps);
139 throw 4; 140 throw 4;
140 } 141 }
141 142
@@ -143,15 +144,15 @@ std::vector<Highscore> HighscoreList::getGlobalHighscores()
143 144
144 if (sscanf(temps, namelens, name) != 1) 145 if (sscanf(temps, namelens, name) != 1)
145 { 146 {
146 printf("Recieved data is of an invalid format: %s\n", temps); 147 printf("Recieved data is of an invalid format (2-%d): %s\n", i, temps);
147 throw 4; 148 throw 4;
148 } 149 }
149 150
150 sprintf(namelens, "%%*d%%*%dc%%d%%*c", namelen); 151 sprintf(namelens, "%%*d%%*%dc%%d", namelen);
151 152
152 if (sscanf(temps, namelens, &score) != 1) 153 if (sscanf(temps, namelens, &score) != 1)
153 { 154 {
154 printf("Recieved data is of an invalid format: %s\n", temps); 155 printf("Recieved data is of an invalid format (3-%d): %s\n", i, temps);
155 throw 4; 156 throw 4;
156 } 157 }
157 158
@@ -201,3 +202,197 @@ SDL_Surface* GlobalHighscoreList::render()
201 return super::render(); 202 return super::render();
202 } 203 }
203} 204}
205
206State* ChooseHighscoreListState::operator() (SDL_Renderer* renderer)
207{
208 SDL_Texture* background = loadImage(renderer, "resources/chl.bmp");
209 SDL_Texture* pointer = loadImage(renderer, "resources/pointer.bmp");
210 int selection = 0;
211 SDL_Event e;
212
213 for (;;)
214 {
215 SDL_RenderClear(renderer);
216 SDL_RenderCopy(renderer, background, NULL, NULL);
217 applyTexture(renderer, pointer, 127, selection==0?306:(selection==1?336:396));
218 SDL_RenderPresent(renderer);
219
220 while (SDL_PollEvent(&e))
221 {
222 if (e.type == SDL_QUIT)
223 {
224 return NULL;
225 } else if (e.type == SDL_KEYDOWN)
226 {
227 if ((e.key.keysym.sym == SDLK_UP) && (selection != 0))
228 {
229 selection--;
230 } else if ((e.key.keysym.sym == SDLK_DOWN) && (selection != 2))
231 {
232 selection++;
233 } else if (e.key.keysym.sym == SDLK_RETURN)
234 {
235 switch (selection)
236 {
237 case 0: return new DisplayLocalHighscoreListState();
238 case 1: return new DisplayGlobalHighscoreListState();
239 case 2: return new TitleState();
240 }
241 }
242 }
243 }
244 }
245}
246
247State* DisplayLocalHighscoreListState::operator() (SDL_Renderer* renderer)
248{
249 SDL_Texture* pointer = loadImage(renderer, "resources/pointer.bmp");
250
251 LocalHighscoreList* lhl = new LocalHighscoreList();
252 SDL_Surface* list_s = lhl->render();
253 SDL_Color fontColor = {0, 0, 0, 0};
254 SDL_Surface* title = TTF_RenderText_Blended(loadFont(40), "Highscore List", fontColor);
255 SDL_Rect tSpace = {240-(title->w/2), 0, title->w, title->h};
256 SDL_BlitSurface(title, NULL, list_s, &tSpace);
257 SDL_FreeSurface(title);
258
259 SDL_Surface* options_s = SDL_LoadBMP("resources/hlo_rtm.bmp");
260 SDL_Rect oSpace = {0, 440, options_s->w, options_s->h};
261 SDL_BlitSurface(options_s, NULL, list_s, &oSpace);
262 SDL_FreeSurface(options_s);
263
264 SDL_Texture* list = SDL_CreateTextureFromSurface(renderer, list_s);
265 SDL_FreeSurface(list_s);
266
267 SDL_Event e;
268
269 for (;;)
270 {
271 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
272 SDL_RenderClear(renderer);
273 SDL_RenderCopy(renderer, list, NULL, NULL);
274 applyTexture(renderer, pointer, 137, 449);
275 SDL_RenderPresent(renderer);
276
277 while (SDL_PollEvent(&e))
278 {
279 if (e.type == SDL_QUIT)
280 {
281 return NULL;
282 } else if (e.type == SDL_KEYDOWN)
283 {
284 if (e.key.keysym.sym == SDLK_RETURN)
285 {
286 return new ChooseHighscoreListState();
287 }
288 }
289 }
290 }
291}
292
293State* DisplayGlobalHighscoreListState::operator() (SDL_Renderer* renderer)
294{
295 SDL_Texture* pointer = loadImage(renderer, "resources/pointer.bmp");
296
297 // Display loading message
298 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
299 SDL_RenderClear(renderer);
300
301 SDL_Surface* list_s = SDL_CreateRGBSurface(0, 480, 480, 32, 0,0,0,0);
302 Uint32 bgColor = SDL_MapRGB(list_s->format, 255, 255, 255);
303 SDL_FillRect(list_s, NULL, bgColor);
304 SDL_SetColorKey(list_s, SDL_TRUE, bgColor);
305 TTF_Font* dataFont = loadFont(25);
306 SDL_Color fontColor = {0, 0, 0, 0};
307 SDL_Surface* text = TTF_RenderText_Blended(dataFont, "Fetching highscores....", fontColor);
308 SDL_Rect aSpace = {240-(text->w/2), 240-(text->h/2), text->w, text->h};
309 SDL_BlitSurface(text, NULL, list_s, &aSpace);
310 SDL_FreeSurface(text);
311
312 SDL_Surface* title = TTF_RenderText_Blended(loadFont(40), "Highscore List", fontColor);
313 SDL_Rect tSpace = {240-(title->w/2), 0, title->w, title->h};
314 SDL_BlitSurface(title, NULL, list_s, &tSpace);
315 SDL_FreeSurface(title);
316
317 SDL_Surface* options_s = SDL_LoadBMP("resources/hlo_rtm.bmp");
318 SDL_Rect oSpace = {0, 440, options_s->w, options_s->h};
319 SDL_BlitSurface(options_s, NULL, list_s, &oSpace);
320 SDL_FreeSurface(options_s);
321
322 list = SDL_CreateTextureFromSurface(renderer, list_s);
323 SDL_FreeSurface(list_s);
324
325 m = SDL_CreateMutex();
326
327 // Start downloading scores
328 SDL_CreateThread(&LoadHighscoreList, "LoadHighscoreList", this);
329
330 // Parse keyboard events
331 SDL_Event e;
332
333 for (;;)
334 {
335 if (SDL_LockMutex(m) == 0)
336 {
337 if (lhl != NULL)
338 {
339 SDL_Surface* list_s = lhl->render();
340
341 SDL_Color fontColor = {0, 0, 0, 0};
342 SDL_Surface* title = TTF_RenderText_Blended(loadFont(40), "Highscore List", fontColor);
343 SDL_Rect tSpace = {240-(title->w/2), 0, title->w, title->h};
344 SDL_BlitSurface(title, NULL, list_s, &tSpace);
345 SDL_FreeSurface(title);
346
347 SDL_Surface* options_s = SDL_LoadBMP("resources/hlo_rtm.bmp");
348 SDL_Rect oSpace = {0, 440, options_s->w, options_s->h};
349 SDL_BlitSurface(options_s, NULL, list_s, &oSpace);
350 SDL_FreeSurface(options_s);
351
352 list = SDL_CreateTextureFromSurface(renderer, list_s);
353 SDL_FreeSurface(list_s);
354
355 lhl = NULL;
356 }
357
358 SDL_UnlockMutex(m);
359 }
360
361 SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255);
362 SDL_RenderClear(renderer);
363 SDL_RenderCopy(renderer, list, NULL, NULL);
364 applyTexture(renderer, pointer, 137, 449);
365 SDL_RenderPresent(renderer);
366
367 while (SDL_PollEvent(&e))
368 {
369 if (e.type == SDL_QUIT)
370 {
371 SDL_DestroyMutex(m);
372
373 return NULL;
374 } else if (e.type == SDL_KEYDOWN)
375 {
376 if (e.key.keysym.sym == SDLK_RETURN)
377 {
378 SDL_DestroyMutex(m);
379
380 return new ChooseHighscoreListState();
381 }
382 }
383 }
384 }
385}
386
387int DisplayGlobalHighscoreListState::LoadHighscoreList(void* pParam)
388{
389 DisplayGlobalHighscoreListState* parent = ((DisplayGlobalHighscoreListState*)pParam);
390 if (SDL_LockMutex(parent->m) == 0)
391 {
392 parent->lhl = new GlobalHighscoreList();
393
394 SDL_UnlockMutex(parent->m);
395 } else {
396 printf("Couldn't lock mutex: %s\n", SDL_GetError());
397 }
398}
diff --git a/hslist.h b/hslist.h index 80e1c31..f043336 100644 --- a/hslist.h +++ b/hslist.h
@@ -1,6 +1,7 @@
1#include <SDL.h> 1#include <SDL.h>
2#include <vector> 2#include <vector>
3#include "highscore.h" 3#include "highscore.h"
4#include "state.h"
4 5
5#ifndef HSLIST_H 6#ifndef HSLIST_H
6#define HSLIST_H 7#define HSLIST_H
@@ -34,4 +35,28 @@ class GlobalHighscoreList : public HighscoreList {
34 bool fail; 35 bool fail;
35}; 36};
36 37
38class ChooseHighscoreListState : public State {
39 public:
40 State* operator() (SDL_Renderer* renderer);
41};
42
43class DisplayLocalHighscoreListState : public State {
44 public:
45 State* operator() (SDL_Renderer* renderer);
46};
47
48class DisplayGlobalHighscoreListState : public State {
49 public:
50 State* operator() (SDL_Renderer* renderer);
51
52 protected:
53 SDL_Surface* list_s;
54 SDL_Texture* list;
55 GlobalHighscoreList* lhl;
56 SDL_mutex* m;
57
58 private:
59 static int LoadHighscoreList(void* pParam);
60};
61
37#endif 62#endif
diff --git a/resources/hlo_rtm.bmp b/resources/hlo_rtm.bmp index c05c208..5971a53 100644 --- a/resources/hlo_rtm.bmp +++ b/resources/hlo_rtm.bmp
Binary files differ
diff --git a/titlestate.cpp b/titlestate.cpp index 87a1faf..177a4c8 100644 --- a/titlestate.cpp +++ b/titlestate.cpp
@@ -1,6 +1,7 @@
1#include "titlestate.h" 1#include "titlestate.h"
2#include "util.h" 2#include "util.h"
3#include "gamestate.h" 3#include "gamestate.h"
4#include "hslist.h"
4 5
5State* TitleState::operator() (SDL_Renderer* renderer) 6State* TitleState::operator() (SDL_Renderer* renderer)
6{ 7{
@@ -35,7 +36,7 @@ State* TitleState::operator() (SDL_Renderer* renderer)
35 { 36 {
36 case 0: return new GameState(); 37 case 0: return new GameState();
37 //case 1: return new HowToPlayState(); 38 //case 1: return new HowToPlayState();
38 //case 2: return new ChooseHighscoreListState(); 39 case 2: return new ChooseHighscoreListState();
39 case 3: return NULL; 40 case 3: return NULL;
40 } 41 }
41 } 42 }