summary refs log tree commit diff stats
path: root/hslist.cpp
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 /hslist.cpp
parent4c0b84a69ff30c4aa91490ea1c16300f1017f845 (diff)
downloadmazeoflife-992f4eb1e6d711c1be097ffb1684df9a23e08d54.tar.gz
mazeoflife-992f4eb1e6d711c1be097ffb1684df9a23e08d54.tar.bz2
mazeoflife-992f4eb1e6d711c1be097ffb1684df9a23e08d54.zip
Added highscore list viewing
Diffstat (limited to 'hslist.cpp')
-rw-r--r--hslist.cpp209
1 files changed, 202 insertions, 7 deletions
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}