From 992f4eb1e6d711c1be097ffb1684df9a23e08d54 Mon Sep 17 00:00:00 2001 From: Starla Insigna Date: Wed, 28 Aug 2013 11:44:12 -0400 Subject: Added highscore list viewing --- gamestate.cpp | 5 ++ hslist.cpp | 209 ++++++++++++++++++++++++++++++++++++++++++++++++-- hslist.h | 25 ++++++ resources/hlo_rtm.bmp | Bin 76938 -> 76856 bytes titlestate.cpp | 3 +- 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 @@ #include #include "util.h" #include "mazeoflife.h" +#include "highscore.h" +#include "titlestate.h" class GameBoard { public: @@ -210,6 +212,9 @@ State* PlayGameState::operator() (SDL_Renderer* renderer) } else { break; } + + case SDLK_ESCAPE: + return new TitleState(); } } } diff --git a/hslist.cpp b/hslist.cpp index 80e0801..3d9f19a 100644 --- a/hslist.cpp +++ b/hslist.cpp @@ -4,6 +4,7 @@ #include #include #include "util.h" +#include "titlestate.h" SDL_Surface* HighscoreList::render() { @@ -20,8 +21,8 @@ SDL_Surface* HighscoreList::render() Highscore h = hslist[i]; int posw, posh; - char pos[3]; // 2 max characters in rank plus the colon at the end - sprintf(pos, "%d:", h.getRank()); + char pos[3]; // 2 max characters in rank plus the colon at the end + sprintf(pos, "%d:", h.getRank()); TTF_SizeText(posFont, pos, &posw, &posh); SDL_Rect posSpace = {0, (i+1)*40, posw, posh}; SDL_BlitSurface(TTF_RenderText_Blended(posFont, pos, fontColor), NULL, tmp, &posSpace); @@ -96,7 +97,7 @@ std::vector HighscoreList::getGlobalHighscores() throw 2; } - 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"; + 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"; if (SDLNet_TCP_Send(tcpsock, headers, strlen(headers)+1) < strlen(headers)) { printf("Connection closed by peer: %s\n", SDLNet_GetError()); @@ -135,7 +136,7 @@ std::vector HighscoreList::getGlobalHighscores() if (sscanf(temps, "%d", &namelen) != 1) { - printf("Recieved data is of an invalid format: %s\n", temps); + printf("Recieved data is of an invalid format (1-%d): %s\n", i, temps); throw 4; } @@ -143,15 +144,15 @@ std::vector HighscoreList::getGlobalHighscores() if (sscanf(temps, namelens, name) != 1) { - printf("Recieved data is of an invalid format: %s\n", temps); + printf("Recieved data is of an invalid format (2-%d): %s\n", i, temps); throw 4; } - sprintf(namelens, "%%*d%%*%dc%%d%%*c", namelen); + sprintf(namelens, "%%*d%%*%dc%%d", namelen); if (sscanf(temps, namelens, &score) != 1) { - printf("Recieved data is of an invalid format: %s\n", temps); + printf("Recieved data is of an invalid format (3-%d): %s\n", i, temps); throw 4; } @@ -201,3 +202,197 @@ SDL_Surface* GlobalHighscoreList::render() return super::render(); } } + +State* ChooseHighscoreListState::operator() (SDL_Renderer* renderer) +{ + SDL_Texture* background = loadImage(renderer, "resources/chl.bmp"); + SDL_Texture* pointer = loadImage(renderer, "resources/pointer.bmp"); + int selection = 0; + SDL_Event e; + + for (;;) + { + SDL_RenderClear(renderer); + SDL_RenderCopy(renderer, background, NULL, NULL); + applyTexture(renderer, pointer, 127, selection==0?306:(selection==1?336:396)); + SDL_RenderPresent(renderer); + + while (SDL_PollEvent(&e)) + { + if (e.type == SDL_QUIT) + { + return NULL; + } else if (e.type == SDL_KEYDOWN) + { + if ((e.key.keysym.sym == SDLK_UP) && (selection != 0)) + { + selection--; + } else if ((e.key.keysym.sym == SDLK_DOWN) && (selection != 2)) + { + selection++; + } else if (e.key.keysym.sym == SDLK_RETURN) + { + switch (selection) + { + case 0: return new DisplayLocalHighscoreListState(); + case 1: return new DisplayGlobalHighscoreListState(); + case 2: return new TitleState(); + } + } + } + } + } +} + +State* DisplayLocalHighscoreListState::operator() (SDL_Renderer* renderer) +{ + SDL_Texture* pointer = loadImage(renderer, "resources/pointer.bmp"); + + LocalHighscoreList* lhl = new LocalHighscoreList(); + SDL_Surface* list_s = lhl->render(); + SDL_Color fontColor = {0, 0, 0, 0}; + SDL_Surface* title = TTF_RenderText_Blended(loadFont(40), "Highscore List", fontColor); + SDL_Rect tSpace = {240-(title->w/2), 0, title->w, title->h}; + SDL_BlitSurface(title, NULL, list_s, &tSpace); + SDL_FreeSurface(title); + + SDL_Surface* options_s = SDL_LoadBMP("resources/hlo_rtm.bmp"); + SDL_Rect oSpace = {0, 440, options_s->w, options_s->h}; + SDL_BlitSurface(options_s, NULL, list_s, &oSpace); + SDL_FreeSurface(options_s); + + SDL_Texture* list = SDL_CreateTextureFromSurface(renderer, list_s); + SDL_FreeSurface(list_s); + + SDL_Event e; + + for (;;) + { + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + SDL_RenderClear(renderer); + SDL_RenderCopy(renderer, list, NULL, NULL); + applyTexture(renderer, pointer, 137, 449); + SDL_RenderPresent(renderer); + + while (SDL_PollEvent(&e)) + { + if (e.type == SDL_QUIT) + { + return NULL; + } else if (e.type == SDL_KEYDOWN) + { + if (e.key.keysym.sym == SDLK_RETURN) + { + return new ChooseHighscoreListState(); + } + } + } + } +} + +State* DisplayGlobalHighscoreListState::operator() (SDL_Renderer* renderer) +{ + SDL_Texture* pointer = loadImage(renderer, "resources/pointer.bmp"); + + // Display loading message + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + SDL_RenderClear(renderer); + + SDL_Surface* list_s = SDL_CreateRGBSurface(0, 480, 480, 32, 0,0,0,0); + Uint32 bgColor = SDL_MapRGB(list_s->format, 255, 255, 255); + SDL_FillRect(list_s, NULL, bgColor); + SDL_SetColorKey(list_s, SDL_TRUE, bgColor); + TTF_Font* dataFont = loadFont(25); + SDL_Color fontColor = {0, 0, 0, 0}; + SDL_Surface* text = TTF_RenderText_Blended(dataFont, "Fetching highscores....", fontColor); + SDL_Rect aSpace = {240-(text->w/2), 240-(text->h/2), text->w, text->h}; + SDL_BlitSurface(text, NULL, list_s, &aSpace); + SDL_FreeSurface(text); + + SDL_Surface* title = TTF_RenderText_Blended(loadFont(40), "Highscore List", fontColor); + SDL_Rect tSpace = {240-(title->w/2), 0, title->w, title->h}; + SDL_BlitSurface(title, NULL, list_s, &tSpace); + SDL_FreeSurface(title); + + SDL_Surface* options_s = SDL_LoadBMP("resources/hlo_rtm.bmp"); + SDL_Rect oSpace = {0, 440, options_s->w, options_s->h}; + SDL_BlitSurface(options_s, NULL, list_s, &oSpace); + SDL_FreeSurface(options_s); + + list = SDL_CreateTextureFromSurface(renderer, list_s); + SDL_FreeSurface(list_s); + + m = SDL_CreateMutex(); + + // Start downloading scores + SDL_CreateThread(&LoadHighscoreList, "LoadHighscoreList", this); + + // Parse keyboard events + SDL_Event e; + + for (;;) + { + if (SDL_LockMutex(m) == 0) + { + if (lhl != NULL) + { + SDL_Surface* list_s = lhl->render(); + + SDL_Color fontColor = {0, 0, 0, 0}; + SDL_Surface* title = TTF_RenderText_Blended(loadFont(40), "Highscore List", fontColor); + SDL_Rect tSpace = {240-(title->w/2), 0, title->w, title->h}; + SDL_BlitSurface(title, NULL, list_s, &tSpace); + SDL_FreeSurface(title); + + SDL_Surface* options_s = SDL_LoadBMP("resources/hlo_rtm.bmp"); + SDL_Rect oSpace = {0, 440, options_s->w, options_s->h}; + SDL_BlitSurface(options_s, NULL, list_s, &oSpace); + SDL_FreeSurface(options_s); + + list = SDL_CreateTextureFromSurface(renderer, list_s); + SDL_FreeSurface(list_s); + + lhl = NULL; + } + + SDL_UnlockMutex(m); + } + + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + SDL_RenderClear(renderer); + SDL_RenderCopy(renderer, list, NULL, NULL); + applyTexture(renderer, pointer, 137, 449); + SDL_RenderPresent(renderer); + + while (SDL_PollEvent(&e)) + { + if (e.type == SDL_QUIT) + { + SDL_DestroyMutex(m); + + return NULL; + } else if (e.type == SDL_KEYDOWN) + { + if (e.key.keysym.sym == SDLK_RETURN) + { + SDL_DestroyMutex(m); + + return new ChooseHighscoreListState(); + } + } + } + } +} + +int DisplayGlobalHighscoreListState::LoadHighscoreList(void* pParam) +{ + DisplayGlobalHighscoreListState* parent = ((DisplayGlobalHighscoreListState*)pParam); + if (SDL_LockMutex(parent->m) == 0) + { + parent->lhl = new GlobalHighscoreList(); + + SDL_UnlockMutex(parent->m); + } else { + printf("Couldn't lock mutex: %s\n", SDL_GetError()); + } +} diff --git a/hslist.h b/hslist.h index 80e1c31..f043336 100644 --- a/hslist.h +++ b/hslist.h @@ -1,6 +1,7 @@ #include #include #include "highscore.h" +#include "state.h" #ifndef HSLIST_H #define HSLIST_H @@ -34,4 +35,28 @@ class GlobalHighscoreList : public HighscoreList { bool fail; }; +class ChooseHighscoreListState : public State { + public: + State* operator() (SDL_Renderer* renderer); +}; + +class DisplayLocalHighscoreListState : public State { + public: + State* operator() (SDL_Renderer* renderer); +}; + +class DisplayGlobalHighscoreListState : public State { + public: + State* operator() (SDL_Renderer* renderer); + + protected: + SDL_Surface* list_s; + SDL_Texture* list; + GlobalHighscoreList* lhl; + SDL_mutex* m; + + private: + static int LoadHighscoreList(void* pParam); +}; + #endif diff --git a/resources/hlo_rtm.bmp b/resources/hlo_rtm.bmp index c05c208..5971a53 100644 Binary files a/resources/hlo_rtm.bmp and b/resources/hlo_rtm.bmp differ diff --git a/titlestate.cpp b/titlestate.cpp index 87a1faf..177a4c8 100644 --- a/titlestate.cpp +++ b/titlestate.cpp @@ -1,6 +1,7 @@ #include "titlestate.h" #include "util.h" #include "gamestate.h" +#include "hslist.h" State* TitleState::operator() (SDL_Renderer* renderer) { @@ -35,7 +36,7 @@ State* TitleState::operator() (SDL_Renderer* renderer) { case 0: return new GameState(); //case 1: return new HowToPlayState(); - //case 2: return new ChooseHighscoreListState(); + case 2: return new ChooseHighscoreListState(); case 3: return NULL; } } -- cgit 1.4.1