diff options
| author | Star Rauchenberger <fefferburbia@gmail.com> | 2023-11-02 09:17:25 -0400 |
|---|---|---|
| committer | Star Rauchenberger <fefferburbia@gmail.com> | 2023-11-02 09:17:25 -0400 |
| commit | 45d6e635c880a7fae8711fba366519dd314d9faf (patch) | |
| tree | a7c5e254938161727c8ba191ad68f801f1f0f2bb /hslist.cpp | |
| parent | 4e8f554286593ec8aca6c61fa0fb9c4934bd640c (diff) | |
| download | mazeoflife-45d6e635c880a7fae8711fba366519dd314d9faf.tar.gz mazeoflife-45d6e635c880a7fae8711fba366519dd314d9faf.tar.bz2 mazeoflife-45d6e635c880a7fae8711fba366519dd314d9faf.zip | |
Formatted source code
Diffstat (limited to 'hslist.cpp')
| -rw-r--r-- | hslist.cpp | 1814 |
1 files changed, 881 insertions, 933 deletions
| diff --git a/hslist.cpp b/hslist.cpp index f71d8be..5eb9b6c 100644 --- a/hslist.cpp +++ b/hslist.cpp | |||
| @@ -1,1002 +1,950 @@ | |||
| 1 | #include "hslist.h" | 1 | #include "hslist.h" |
| 2 | #include <SDL_ttf.h> | 2 | |
| 3 | #include <SDL_net.h> | 3 | #include <SDL_net.h> |
| 4 | #include <sstream> | 4 | #include <SDL_ttf.h> |
| 5 | #include <fstream> | 5 | |
| 6 | #include <algorithm> | 6 | #include <algorithm> |
| 7 | #include "util.h" | 7 | #include <fstream> |
| 8 | #include "titlestate.h" | 8 | #include <sstream> |
| 9 | |||
| 9 | #include "gamestate.h" | 10 | #include "gamestate.h" |
| 11 | #include "titlestate.h" | ||
| 12 | #include "util.h" | ||
| 10 | 13 | ||
| 11 | // We want to be able to sort Highscore objects in descending score order | 14 | // We want to be able to sort Highscore objects in descending score order |
| 12 | struct hslist_comp { | 15 | struct hslist_comp { |
| 13 | bool operator() (Highscore* lhs, Highscore* rhs) const | 16 | bool operator()(Highscore* lhs, Highscore* rhs) const { |
| 14 | { | 17 | return (lhs->getLevel() > rhs->getLevel()); |
| 15 | return (lhs->getLevel() > rhs->getLevel()); | 18 | } |
| 16 | } | ||
| 17 | } hslist_comp_i; | 19 | } hslist_comp_i; |
| 18 | 20 | ||
| 19 | // resetRanks : sets the rank of all Highscore objects in a hslist_t to their (one-based) index in the list | 21 | // resetRanks : sets the rank of all Highscore objects in a hslist_t to their |
| 20 | void resetRanks(hslist_t in) | 22 | // (one-based) index in the list |
| 21 | { | 23 | void resetRanks(hslist_t in) { |
| 22 | int i=1; | 24 | int i = 1; |
| 23 | for (hslist_t::iterator it = in.begin(); it != in.end(); ++it, ++i) | 25 | for (hslist_t::iterator it = in.begin(); it != in.end(); ++it, ++i) { |
| 24 | { | 26 | ((Highscore*)*it)->setRank(i); |
| 25 | ((Highscore*)*it)->setRank(i); | 27 | } |
| 26 | } | ||
| 27 | } | 28 | } |
| 28 | 29 | ||
| 29 | SDL_Surface* HighscoreList::render() | 30 | SDL_Surface* HighscoreList::render() { |
| 30 | { | 31 | SDL_Surface* tmp = SDL_CreateRGBSurface(0, 480, 480, 32, 0, 0, 0, 0); |
| 31 | SDL_Surface* tmp = SDL_CreateRGBSurface(0, 480, 480, 32, 0,0,0,0); | 32 | Uint32 bgColor = SDL_MapRGB(tmp->format, 255, 255, 255); |
| 32 | Uint32 bgColor = SDL_MapRGB(tmp->format, 255, 255, 255); | 33 | SDL_FillRect(tmp, NULL, bgColor); |
| 33 | SDL_FillRect(tmp, NULL, bgColor); | 34 | SDL_SetColorKey(tmp, SDL_TRUE, bgColor); |
| 34 | SDL_SetColorKey(tmp, SDL_TRUE, bgColor); | 35 | TTF_Font* posFont = loadFont(40); |
| 35 | TTF_Font* posFont = loadFont(40); | 36 | TTF_Font* dataFont = loadFont(25); |
| 36 | TTF_Font* dataFont = loadFont(25); | 37 | SDL_Color fontColor = {0, 0, 0, 0}; |
| 37 | SDL_Color fontColor = {0, 0, 0, 0}; | 38 | |
| 38 | 39 | int i = 0; | |
| 39 | int i=0; | 40 | for (hslist_t::iterator it = hslist.begin(); it != hslist.end(); ++it, ++i) { |
| 40 | for (hslist_t::iterator it = hslist.begin(); it != hslist.end(); ++it, ++i) | 41 | Highscore* h = *it; |
| 41 | { | 42 | |
| 42 | Highscore* h = *it; | 43 | int posw, posh; |
| 43 | 44 | char pos[4]; // 2 max characters in rank plus the colon at the end, plus | |
| 44 | int posw, posh; | 45 | // terminator |
| 45 | char pos[4]; // 2 max characters in rank plus the colon at the end, plus terminator | 46 | sprintf(pos, "%d:", h->getRank()); |
| 46 | sprintf(pos, "%d:", h->getRank()); | 47 | TTF_SizeText(posFont, pos, &posw, &posh); |
| 47 | TTF_SizeText(posFont, pos, &posw, &posh); | 48 | SDL_Rect posSpace = {0, (i + 1) * 40, posw, posh}; |
| 48 | SDL_Rect posSpace = {0, (i+1)*40, posw, posh}; | 49 | SDL_BlitSurface(TTF_RenderText_Blended(posFont, pos, fontColor), NULL, tmp, |
| 49 | SDL_BlitSurface(TTF_RenderText_Blended(posFont, pos, fontColor), NULL, tmp, &posSpace); | 50 | &posSpace); |
| 50 | 51 | ||
| 51 | int namew, nameh; | 52 | int namew, nameh; |
| 52 | char name[27]; // 25 max characters in username plus the space at the beginning, plus terminator | 53 | char name[27]; // 25 max characters in username plus the space at the |
| 53 | sprintf(name, " %s", h->getName()); | 54 | // beginning, plus terminator |
| 54 | TTF_SizeText(dataFont, name, &namew, &nameh); | 55 | sprintf(name, " %s", h->getName()); |
| 55 | SDL_Rect nameSpace = {posw, (i+1)*40+((posh/2)-(nameh/2)), namew, nameh}; | 56 | TTF_SizeText(dataFont, name, &namew, &nameh); |
| 56 | SDL_BlitSurface(TTF_RenderText_Blended(dataFont, name, fontColor), NULL, tmp, &nameSpace); | 57 | SDL_Rect nameSpace = {posw, (i + 1) * 40 + ((posh / 2) - (nameh / 2)), |
| 57 | 58 | namew, nameh}; | |
| 58 | int lvlw, lvlh; | 59 | SDL_BlitSurface(TTF_RenderText_Blended(dataFont, name, fontColor), NULL, |
| 59 | char lvl[11]; // 10 max characters in level (based off the fact that 2^32-1 is 10 characters long, and is the highest int), plus terminator | 60 | tmp, &nameSpace); |
| 60 | sprintf(lvl, "%d", h->getLevel()); | 61 | |
| 61 | TTF_SizeText(dataFont, lvl, &lvlw, &lvlh); | 62 | int lvlw, lvlh; |
| 62 | SDL_Rect lvlSpace = {480-lvlw, (i+1)*40+((posh/2)-(nameh/2)), lvlw, lvlh}; | 63 | char lvl[11]; // 10 max characters in level (based off the fact that 2^32-1 |
| 63 | SDL_BlitSurface(TTF_RenderText_Blended(dataFont, lvl, fontColor), NULL, tmp, &lvlSpace); | 64 | // is 10 characters long, and is the highest int), plus |
| 64 | } | 65 | // terminator |
| 65 | 66 | sprintf(lvl, "%d", h->getLevel()); | |
| 66 | return tmp; | 67 | TTF_SizeText(dataFont, lvl, &lvlw, &lvlh); |
| 68 | SDL_Rect lvlSpace = {480 - lvlw, (i + 1) * 40 + ((posh / 2) - (nameh / 2)), | ||
| 69 | lvlw, lvlh}; | ||
| 70 | SDL_BlitSurface(TTF_RenderText_Blended(dataFont, lvl, fontColor), NULL, tmp, | ||
| 71 | &lvlSpace); | ||
| 72 | } | ||
| 73 | |||
| 74 | return tmp; | ||
| 67 | } | 75 | } |
| 68 | 76 | ||
| 69 | hslist_t HighscoreList::getLocalHighscores() | 77 | hslist_t HighscoreList::getLocalHighscores() { |
| 70 | { | 78 | hslist_t temp; |
| 71 | hslist_t temp; | ||
| 72 | 79 | ||
| 73 | std::ifstream exists(getDataFile()); | 80 | std::ifstream exists(getDataFile()); |
| 74 | if (exists) | 81 | if (exists) { |
| 75 | { | 82 | FILE* hslist = fopen(getDataFile(), "r"); |
| 76 | FILE* hslist = fopen(getDataFile(), "r"); | 83 | int scores; |
| 77 | int scores; | 84 | fscanf(hslist, "%d%*c", &scores); |
| 78 | fscanf(hslist, "%d%*c", &scores); | ||
| 79 | 85 | ||
| 80 | for (int i=0; i<scores; i++) | 86 | for (int i = 0; i < scores; i++) { |
| 81 | { | 87 | int namelen; |
| 82 | int namelen; | 88 | char namelens[4]; |
| 83 | char namelens[4]; | 89 | char* name = (char*)calloc(25, sizeof(char)); |
| 84 | char* name = (char*) calloc(25, sizeof(char)); | 90 | int score; |
| 85 | int score; | ||
| 86 | 91 | ||
| 87 | fscanf(hslist, "%d", &namelen); | 92 | fscanf(hslist, "%d", &namelen); |
| 88 | sprintf(namelens, "%%%dc", namelen); | 93 | sprintf(namelens, "%%%dc", namelen); |
| 89 | fscanf(hslist, namelens, name); | 94 | fscanf(hslist, namelens, name); |
| 90 | fscanf(hslist, "%d%*c", &score); | 95 | fscanf(hslist, "%d%*c", &score); |
| 91 | 96 | ||
| 92 | Highscore* h = new Highscore(name, score); | 97 | Highscore* h = new Highscore(name, score); |
| 93 | h->setRank(i+1); | 98 | h->setRank(i + 1); |
| 94 | 99 | ||
| 95 | temp.push_back(h); | 100 | temp.push_back(h); |
| 96 | } | 101 | } |
| 97 | 102 | ||
| 98 | fclose(hslist); | 103 | fclose(hslist); |
| 99 | } | 104 | } |
| 100 | 105 | ||
| 101 | return temp; | 106 | return temp; |
| 102 | } | 107 | } |
| 103 | 108 | ||
| 104 | hslist_t HighscoreList::getGlobalHighscores() | 109 | hslist_t HighscoreList::getGlobalHighscores() { |
| 105 | { | 110 | IPaddress ipaddress; |
| 106 | IPaddress ipaddress; | 111 | |
| 107 | 112 | if (SDLNet_ResolveHost(&ipaddress, "other.fourisland.com", 80) == -1) { | |
| 108 | if (SDLNet_ResolveHost(&ipaddress, "other.fourisland.com", 80) == -1) | 113 | printf("Could not resolve host \"other.fourisland.com\": %s\n", |
| 109 | { | 114 | SDLNet_GetError()); |
| 110 | printf("Could not resolve host \"other.fourisland.com\": %s\n", SDLNet_GetError()); | 115 | throw 1; |
| 111 | throw 1; | 116 | } |
| 112 | } | 117 | |
| 113 | 118 | TCPsocket tcpsock = SDLNet_TCP_Open(&ipaddress); | |
| 114 | TCPsocket tcpsock = SDLNet_TCP_Open(&ipaddress); | 119 | if (!tcpsock) { |
| 115 | if (!tcpsock) | 120 | printf("Could not connect to host \"other.fourisland.com\": %s\n", |
| 116 | { | 121 | SDLNet_GetError()); |
| 117 | printf("Could not connect to host \"other.fourisland.com\": %s\n", SDLNet_GetError()); | 122 | throw 2; |
| 118 | throw 2; | 123 | } |
| 119 | } | 124 | |
| 120 | 125 | const char* headers = | |
| 121 | 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"; | 126 | "GET /mol/hslist.php HTTP/1.1\nHost: other.fourisland.com\nUser-Agent: " |
| 122 | if (SDLNet_TCP_Send(tcpsock, headers, strlen(headers)+1) < strlen(headers)) | 127 | "Maze Of Life v3.0\nAccept: text/plain\nKeep-Alive: 300\nConnection: " |
| 123 | { | 128 | "keep-alive\n\n"; |
| 124 | printf("Connection closed by peer: %s\n", SDLNet_GetError()); | 129 | if (SDLNet_TCP_Send(tcpsock, headers, strlen(headers) + 1) < |
| 125 | throw 3; | 130 | strlen(headers)) { |
| 126 | } | 131 | printf("Connection closed by peer: %s\n", SDLNet_GetError()); |
| 127 | 132 | throw 3; | |
| 128 | std::stringstream download(std::stringstream::in | std::stringstream::out); | 133 | } |
| 129 | char hslist[1024]; | 134 | |
| 130 | SDLNet_TCP_Recv(tcpsock, hslist, 1024); | 135 | std::stringstream download(std::stringstream::in | std::stringstream::out); |
| 131 | download << hslist; | 136 | char hslist[1024]; |
| 132 | SDLNet_TCP_Close(tcpsock); | 137 | SDLNet_TCP_Recv(tcpsock, hslist, 1024); |
| 133 | 138 | download << hslist; | |
| 134 | char temps[256]; | 139 | SDLNet_TCP_Close(tcpsock); |
| 135 | download.getline(temps,256); | 140 | |
| 136 | while (strlen(temps) != 1) | 141 | char temps[256]; |
| 137 | { | 142 | download.getline(temps, 256); |
| 138 | download.getline(temps,256); | 143 | while (strlen(temps) != 1) { |
| 139 | } | 144 | download.getline(temps, 256); |
| 140 | 145 | } | |
| 141 | hslist_t temp; | 146 | |
| 142 | int scores; | 147 | hslist_t temp; |
| 143 | download.getline(temps, 256); | 148 | int scores; |
| 144 | if (sscanf(temps, "%d%*c", &scores) != 1) | 149 | download.getline(temps, 256); |
| 145 | { | 150 | if (sscanf(temps, "%d%*c", &scores) != 1) { |
| 146 | printf("Recieved data is of an invalid format: %s\n", temps); | 151 | printf("Recieved data is of an invalid format: %s\n", temps); |
| 147 | throw 4; | 152 | throw 4; |
| 148 | } | 153 | } |
| 149 | 154 | ||
| 150 | for (int i=0; i<scores; i++) | 155 | for (int i = 0; i < scores; i++) { |
| 151 | { | 156 | int namelen; |
| 152 | int namelen; | 157 | char namelens[13]; |
| 153 | char namelens[13]; | 158 | char* name = (char*)calloc(25, sizeof(char)); |
| 154 | char* name = (char*) calloc(25, sizeof(char)); | 159 | int score; |
| 155 | int score; | 160 | download.getline(temps, 256); |
| 156 | download.getline(temps, 256); | 161 | |
| 157 | 162 | if (sscanf(temps, "%d", &namelen) != 1) { | |
| 158 | if (sscanf(temps, "%d", &namelen) != 1) | 163 | printf("Recieved data is of an invalid format (1-%d): %s\n", i, temps); |
| 159 | { | 164 | throw 4; |
| 160 | printf("Recieved data is of an invalid format (1-%d): %s\n", i, temps); | 165 | } |
| 161 | throw 4; | 166 | |
| 162 | } | 167 | sprintf(namelens, "%%*d%%%dc", namelen); |
| 163 | 168 | ||
| 164 | sprintf(namelens, "%%*d%%%dc", namelen); | 169 | if (sscanf(temps, namelens, name) != 1) { |
| 165 | 170 | printf("Recieved data is of an invalid format (2-%d): %s\n", i, temps); | |
| 166 | if (sscanf(temps, namelens, name) != 1) | 171 | throw 4; |
| 167 | { | 172 | } |
| 168 | printf("Recieved data is of an invalid format (2-%d): %s\n", i, temps); | 173 | |
| 169 | throw 4; | 174 | sprintf(namelens, "%%*d%%*%dc%%d", namelen); |
| 170 | } | 175 | |
| 171 | 176 | if (sscanf(temps, namelens, &score) != 1) { | |
| 172 | sprintf(namelens, "%%*d%%*%dc%%d", namelen); | 177 | printf("Recieved data is of an invalid format (3-%d): %s\n", i, temps); |
| 173 | 178 | throw 4; | |
| 174 | if (sscanf(temps, namelens, &score) != 1) | 179 | } |
| 175 | { | 180 | |
| 176 | printf("Recieved data is of an invalid format (3-%d): %s\n", i, temps); | 181 | Highscore* h = new Highscore(name, score); |
| 177 | throw 4; | 182 | h->setRank(i + 1); |
| 178 | } | 183 | |
| 179 | 184 | temp.push_back(h); | |
| 180 | Highscore* h = new Highscore(name, score); | 185 | } |
| 181 | h->setRank(i+1); | 186 | |
| 182 | 187 | return temp; | |
| 183 | temp.push_back(h); | ||
| 184 | } | ||
| 185 | |||
| 186 | return temp; | ||
| 187 | } | 188 | } |
| 188 | 189 | ||
| 189 | LocalHighscoreList::LocalHighscoreList() | 190 | LocalHighscoreList::LocalHighscoreList() { |
| 190 | { | 191 | this->hslist = getLocalHighscores(); |
| 191 | this->hslist = getLocalHighscores(); | ||
| 192 | } | 192 | } |
| 193 | 193 | ||
| 194 | int LocalHighscoreList::addHighscore(Highscore* h) | 194 | int LocalHighscoreList::addHighscore(Highscore* h) { |
| 195 | { | 195 | hslist.push_back(h); |
| 196 | hslist.push_back(h); | 196 | std::sort(hslist.begin(), hslist.end(), hslist_comp_i); |
| 197 | std::sort(hslist.begin(), hslist.end(), hslist_comp_i); | 197 | resetRanks(hslist); |
| 198 | resetRanks(hslist); | ||
| 199 | 198 | ||
| 200 | if (hslist.size() > 10) | 199 | if (hslist.size() > 10) { |
| 201 | { | 200 | hslist.resize(10); |
| 202 | hslist.resize(10); | 201 | } |
| 203 | } | ||
| 204 | 202 | ||
| 205 | return h->getRank(); | 203 | return h->getRank(); |
| 206 | } | 204 | } |
| 207 | 205 | ||
| 208 | void LocalHighscoreList::writeHighscores() | 206 | void LocalHighscoreList::writeHighscores() { |
| 209 | { | 207 | FILE* hsfile = fopen(getDataFile(), "w"); |
| 210 | FILE* hsfile = fopen(getDataFile(), "w"); | 208 | fprintf(hsfile, "%d ", (int)this->hslist.size()); |
| 211 | fprintf(hsfile, "%d ", (int) this->hslist.size()); | ||
| 212 | 209 | ||
| 213 | for (hslist_t::iterator it = hslist.begin(); it != this->hslist.end(); it++) | 210 | for (hslist_t::iterator it = hslist.begin(); it != this->hslist.end(); it++) { |
| 214 | { | 211 | Highscore* h = *it; |
| 215 | Highscore* h = *it; | ||
| 216 | 212 | ||
| 217 | fprintf(hsfile, "%d%s%d ", (int) strlen(h->getName()), h->getName(), h->getLevel()); | 213 | fprintf(hsfile, "%d%s%d ", (int)strlen(h->getName()), h->getName(), |
| 218 | } | 214 | h->getLevel()); |
| 215 | } | ||
| 219 | 216 | ||
| 220 | fclose(hsfile); | 217 | fclose(hsfile); |
| 221 | } | 218 | } |
| 222 | 219 | ||
| 223 | GlobalHighscoreList::GlobalHighscoreList() | 220 | GlobalHighscoreList::GlobalHighscoreList() { |
| 224 | { | 221 | fail = false; |
| 225 | fail = false; | ||
| 226 | |||
| 227 | try | ||
| 228 | { | ||
| 229 | this->hslist = getGlobalHighscores(); | ||
| 230 | } catch (int e) | ||
| 231 | { | ||
| 232 | fail = true; | ||
| 233 | } | ||
| 234 | } | ||
| 235 | 222 | ||
| 236 | GlobalHighscoreList::GlobalHighscoreList(Highscore* h) | 223 | try { |
| 237 | { | 224 | this->hslist = getGlobalHighscores(); |
| 238 | fail = false; | 225 | } catch (int e) { |
| 239 | 226 | fail = true; | |
| 240 | try | 227 | } |
| 241 | { | ||
| 242 | IPaddress ipaddress; | ||
| 243 | |||
| 244 | if (SDLNet_ResolveHost(&ipaddress, "other.fourisland.com", 80) == -1) | ||
| 245 | { | ||
| 246 | printf("Could not resolve host \"other.fourisland.com\": %s\n", SDLNet_GetError()); | ||
| 247 | throw 1; | ||
| 248 | } | ||
| 249 | |||
| 250 | TCPsocket tcpsock = SDLNet_TCP_Open(&ipaddress); | ||
| 251 | if (!tcpsock) | ||
| 252 | { | ||
| 253 | printf("Could not connect to host \"other.fourisland.com\": %s\n", SDLNet_GetError()); | ||
| 254 | throw 2; | ||
| 255 | } | ||
| 256 | |||
| 257 | char body[256]; | ||
| 258 | sprintf(body, "name=%s&level=%d", h->getName(), h->getLevel()); | ||
| 259 | char headers[256]; | ||
| 260 | sprintf(headers, "POST /mol/hslist.php?add HTTP/1.1\nHost: other.fourisland.com\nUser-Agent: Maze Of Life v2.0\nAccept: text/plain\nKeep-Alive: 300\nConnection: keep-alive\nContent-Type: application/x-www-form-urlencoded\nContent-Length: %d\n\n%s\n", (int) strlen(body), body); | ||
| 261 | if (SDLNet_TCP_Send(tcpsock, headers, strlen(headers)+1) < strlen(headers)) | ||
| 262 | { | ||
| 263 | printf("Connection closed by peer: %s\n", SDLNet_GetError()); | ||
| 264 | throw 3; | ||
| 265 | } | ||
| 266 | |||
| 267 | std::stringstream download(std::stringstream::in | std::stringstream::out); | ||
| 268 | char hslist[1024]; | ||
| 269 | SDLNet_TCP_Recv(tcpsock, hslist, 1024); | ||
| 270 | download << hslist; | ||
| 271 | SDLNet_TCP_Close(tcpsock); | ||
| 272 | |||
| 273 | char temps[256]; | ||
| 274 | download.getline(temps,256); | ||
| 275 | while (strlen(temps) != 1) | ||
| 276 | { | ||
| 277 | download.getline(temps,256); | ||
| 278 | } | ||
| 279 | |||
| 280 | int rank; | ||
| 281 | download.getline(temps, 256); | ||
| 282 | if (sscanf(temps, "%d%*c", &rank) != 1) | ||
| 283 | { | ||
| 284 | printf("Recieved data is of an invalid format: %s\n", temps); | ||
| 285 | throw 4; | ||
| 286 | } | ||
| 287 | |||
| 288 | this->hslist = getGlobalHighscores(); | ||
| 289 | |||
| 290 | if (this->hslist.empty()) | ||
| 291 | { | ||
| 292 | printf("Global Highscore List cannot be empty after adding a score to it.\n"); | ||
| 293 | throw 5; | ||
| 294 | } | ||
| 295 | |||
| 296 | if (rank > 10) | ||
| 297 | { | ||
| 298 | h->setRank(rank); | ||
| 299 | |||
| 300 | this->hslist[9] = h; | ||
| 301 | } else { | ||
| 302 | this->hslist.push_back(h); | ||
| 303 | std::sort(this->hslist.begin(), this->hslist.end(), hslist_comp_i); | ||
| 304 | resetRanks(this->hslist); | ||
| 305 | |||
| 306 | if (this->hslist.size() > 10) | ||
| 307 | { | ||
| 308 | this->hslist.resize(10); | ||
| 309 | } | ||
| 310 | } | ||
| 311 | } catch (int e) | ||
| 312 | { | ||
| 313 | fail = true; | ||
| 314 | } | ||
| 315 | } | 228 | } |
| 316 | 229 | ||
| 317 | SDL_Surface* GlobalHighscoreList::render() | 230 | GlobalHighscoreList::GlobalHighscoreList(Highscore* h) { |
| 318 | { | 231 | fail = false; |
| 319 | if (fail) | 232 | |
| 320 | { | 233 | try { |
| 321 | SDL_Surface* tmp = SDL_CreateRGBSurface(0, 480, 480, 32, 0,0,0,0); | 234 | IPaddress ipaddress; |
| 322 | Uint32 bgColor = SDL_MapRGB(tmp->format, 255, 255, 255); | 235 | |
| 323 | SDL_FillRect(tmp, NULL, bgColor); | 236 | if (SDLNet_ResolveHost(&ipaddress, "other.fourisland.com", 80) == -1) { |
| 324 | SDL_SetColorKey(tmp, SDL_TRUE, bgColor); | 237 | printf("Could not resolve host \"other.fourisland.com\": %s\n", |
| 325 | TTF_Font* dataFont = loadFont(25); | 238 | SDLNet_GetError()); |
| 326 | SDL_Color fontColor = {0, 0, 0, 0}; | 239 | throw 1; |
| 327 | SDL_Surface* text = TTF_RenderText_Blended(dataFont, "Error retrieving highscores", fontColor); | 240 | } |
| 328 | SDL_Rect tSpace = {240-(text->w/2), 240-(text->h/2), text->w, text->h}; | 241 | |
| 329 | SDL_BlitSurface(text, NULL, tmp, &tSpace); | 242 | TCPsocket tcpsock = SDLNet_TCP_Open(&ipaddress); |
| 330 | 243 | if (!tcpsock) { | |
| 331 | return tmp; | 244 | printf("Could not connect to host \"other.fourisland.com\": %s\n", |
| 332 | } else { | 245 | SDLNet_GetError()); |
| 333 | return super::render(); | 246 | throw 2; |
| 334 | } | 247 | } |
| 248 | |||
| 249 | char body[256]; | ||
| 250 | sprintf(body, "name=%s&level=%d", h->getName(), h->getLevel()); | ||
| 251 | char headers[256]; | ||
| 252 | sprintf( | ||
| 253 | headers, | ||
| 254 | "POST /mol/hslist.php?add HTTP/1.1\nHost: " | ||
| 255 | "other.fourisland.com\nUser-Agent: Maze Of Life v2.0\nAccept: " | ||
| 256 | "text/plain\nKeep-Alive: 300\nConnection: keep-alive\nContent-Type: " | ||
| 257 | "application/x-www-form-urlencoded\nContent-Length: %d\n\n%s\n", | ||
| 258 | (int)strlen(body), body); | ||
| 259 | if (SDLNet_TCP_Send(tcpsock, headers, strlen(headers) + 1) < | ||
| 260 | strlen(headers)) { | ||
| 261 | printf("Connection closed by peer: %s\n", SDLNet_GetError()); | ||
| 262 | throw 3; | ||
| 263 | } | ||
| 264 | |||
| 265 | std::stringstream download(std::stringstream::in | std::stringstream::out); | ||
| 266 | char hslist[1024]; | ||
| 267 | SDLNet_TCP_Recv(tcpsock, hslist, 1024); | ||
| 268 | download << hslist; | ||
| 269 | SDLNet_TCP_Close(tcpsock); | ||
| 270 | |||
| 271 | char temps[256]; | ||
| 272 | download.getline(temps, 256); | ||
| 273 | while (strlen(temps) != 1) { | ||
| 274 | download.getline(temps, 256); | ||
| 275 | } | ||
| 276 | |||
| 277 | int rank; | ||
| 278 | download.getline(temps, 256); | ||
| 279 | if (sscanf(temps, "%d%*c", &rank) != 1) { | ||
| 280 | printf("Recieved data is of an invalid format: %s\n", temps); | ||
| 281 | throw 4; | ||
| 282 | } | ||
| 283 | |||
| 284 | this->hslist = getGlobalHighscores(); | ||
| 285 | |||
| 286 | if (this->hslist.empty()) { | ||
| 287 | printf( | ||
| 288 | "Global Highscore List cannot be empty after adding a score to " | ||
| 289 | "it.\n"); | ||
| 290 | throw 5; | ||
| 291 | } | ||
| 292 | |||
| 293 | if (rank > 10) { | ||
| 294 | h->setRank(rank); | ||
| 295 | |||
| 296 | this->hslist[9] = h; | ||
| 297 | } else { | ||
| 298 | this->hslist.push_back(h); | ||
| 299 | std::sort(this->hslist.begin(), this->hslist.end(), hslist_comp_i); | ||
| 300 | resetRanks(this->hslist); | ||
| 301 | |||
| 302 | if (this->hslist.size() > 10) { | ||
| 303 | this->hslist.resize(10); | ||
| 304 | } | ||
| 305 | } | ||
| 306 | } catch (int e) { | ||
| 307 | fail = true; | ||
| 308 | } | ||
| 335 | } | 309 | } |
| 336 | 310 | ||
| 337 | bool GlobalHighscoreList::didFail() | 311 | SDL_Surface* GlobalHighscoreList::render() { |
| 338 | { | 312 | if (fail) { |
| 339 | return fail; | 313 | SDL_Surface* tmp = SDL_CreateRGBSurface(0, 480, 480, 32, 0, 0, 0, 0); |
| 314 | Uint32 bgColor = SDL_MapRGB(tmp->format, 255, 255, 255); | ||
| 315 | SDL_FillRect(tmp, NULL, bgColor); | ||
| 316 | SDL_SetColorKey(tmp, SDL_TRUE, bgColor); | ||
| 317 | TTF_Font* dataFont = loadFont(25); | ||
| 318 | SDL_Color fontColor = {0, 0, 0, 0}; | ||
| 319 | SDL_Surface* text = TTF_RenderText_Blended( | ||
| 320 | dataFont, "Error retrieving highscores", fontColor); | ||
| 321 | SDL_Rect tSpace = {240 - (text->w / 2), 240 - (text->h / 2), text->w, | ||
| 322 | text->h}; | ||
| 323 | SDL_BlitSurface(text, NULL, tmp, &tSpace); | ||
| 324 | |||
| 325 | return tmp; | ||
| 326 | } else { | ||
| 327 | return super::render(); | ||
| 328 | } | ||
| 340 | } | 329 | } |
| 341 | 330 | ||
| 342 | State* ChooseHighscoreListState::operator() (SDL_Window* window, SDL_Renderer* renderer) | 331 | bool GlobalHighscoreList::didFail() { return fail; } |
| 343 | { | 332 | |
| 344 | SDL_Texture* background = loadImage(renderer, "resources/chl.bmp"); | 333 | State* ChooseHighscoreListState::operator()(SDL_Window* window, |
| 345 | SDL_Texture* pointer = loadImage(renderer, "resources/pointer.bmp"); | 334 | SDL_Renderer* renderer) { |
| 346 | int selection = 0; | 335 | SDL_Texture* background = loadImage(renderer, "resources/chl.bmp"); |
| 347 | SDL_Event e; | 336 | SDL_Texture* pointer = loadImage(renderer, "resources/pointer.bmp"); |
| 348 | 337 | int selection = 0; | |
| 349 | for (;;) | 338 | SDL_Event e; |
| 350 | { | 339 | |
| 351 | SDL_RenderClear(renderer); | 340 | for (;;) { |
| 352 | SDL_RenderCopy(renderer, background, NULL, NULL); | 341 | SDL_RenderClear(renderer); |
| 353 | applyTexture(renderer, pointer, 127, selection==0?306:(selection==1?336:396)); | 342 | SDL_RenderCopy(renderer, background, NULL, NULL); |
| 354 | SDL_RenderPresent(renderer); | 343 | applyTexture(renderer, pointer, 127, |
| 355 | 344 | selection == 0 ? 306 : (selection == 1 ? 336 : 396)); | |
| 356 | while (SDL_PollEvent(&e)) | 345 | SDL_RenderPresent(renderer); |
| 357 | { | 346 | |
| 358 | if (e.type == SDL_QUIT) | 347 | while (SDL_PollEvent(&e)) { |
| 359 | { | 348 | if (e.type == SDL_QUIT) { |
| 360 | return NULL; | 349 | return NULL; |
| 361 | } else if (e.type == SDL_KEYDOWN) | 350 | } else if (e.type == SDL_KEYDOWN) { |
| 362 | { | 351 | if ((e.key.keysym.sym == SDLK_UP) && (selection != 0)) { |
| 363 | if ((e.key.keysym.sym == SDLK_UP) && (selection != 0)) | 352 | selection--; |
| 364 | { | 353 | } else if ((e.key.keysym.sym == SDLK_DOWN) && (selection != 2)) { |
| 365 | selection--; | 354 | selection++; |
| 366 | } else if ((e.key.keysym.sym == SDLK_DOWN) && (selection != 2)) | 355 | } else if (e.key.keysym.sym == SDLK_RETURN) { |
| 367 | { | 356 | switch (selection) { |
| 368 | selection++; | 357 | case 0: |
| 369 | } else if (e.key.keysym.sym == SDLK_RETURN) | 358 | return new DisplayLocalHighscoreListState(); |
| 370 | { | 359 | case 1: |
| 371 | switch (selection) | 360 | return new DisplayGlobalHighscoreListState(); |
| 372 | { | 361 | case 2: |
| 373 | case 0: return new DisplayLocalHighscoreListState(); | 362 | return new TitleState(); |
| 374 | case 1: return new DisplayGlobalHighscoreListState(); | 363 | } |
| 375 | case 2: return new TitleState(); | 364 | } |
| 376 | } | 365 | } |
| 377 | } | 366 | } |
| 378 | } | 367 | } |
| 379 | } | ||
| 380 | } | ||
| 381 | } | 368 | } |
| 382 | 369 | ||
| 383 | State* DisplayLocalHighscoreListState::operator() (SDL_Window* window, SDL_Renderer* renderer) | 370 | State* DisplayLocalHighscoreListState::operator()(SDL_Window* window, |
| 384 | { | 371 | SDL_Renderer* renderer) { |
| 385 | SDL_Texture* pointer = loadImage(renderer, "resources/pointer.bmp"); | 372 | SDL_Texture* pointer = loadImage(renderer, "resources/pointer.bmp"); |
| 386 | 373 | ||
| 387 | LocalHighscoreList* lhl = new LocalHighscoreList(); | 374 | LocalHighscoreList* lhl = new LocalHighscoreList(); |
| 388 | SDL_Surface* list_s = lhl->render(); | 375 | SDL_Surface* list_s = lhl->render(); |
| 389 | SDL_Color fontColor = {0, 0, 0, 0}; | 376 | SDL_Color fontColor = {0, 0, 0, 0}; |
| 390 | SDL_Surface* title = TTF_RenderText_Blended(loadFont(40), "Highscore List", fontColor); | 377 | SDL_Surface* title = |
| 391 | SDL_Rect tSpace = {240-(title->w/2), 0, title->w, title->h}; | 378 | TTF_RenderText_Blended(loadFont(40), "Highscore List", fontColor); |
| 392 | SDL_BlitSurface(title, NULL, list_s, &tSpace); | 379 | SDL_Rect tSpace = {240 - (title->w / 2), 0, title->w, title->h}; |
| 393 | SDL_FreeSurface(title); | 380 | SDL_BlitSurface(title, NULL, list_s, &tSpace); |
| 394 | 381 | SDL_FreeSurface(title); | |
| 395 | SDL_Surface* options_s = SDL_LoadBMP("resources/hlo_rtm.bmp"); | 382 | |
| 396 | SDL_Rect oSpace = {0, 440, options_s->w, options_s->h}; | 383 | SDL_Surface* options_s = SDL_LoadBMP("resources/hlo_rtm.bmp"); |
| 397 | SDL_BlitSurface(options_s, NULL, list_s, &oSpace); | 384 | SDL_Rect oSpace = {0, 440, options_s->w, options_s->h}; |
| 398 | SDL_FreeSurface(options_s); | 385 | SDL_BlitSurface(options_s, NULL, list_s, &oSpace); |
| 399 | 386 | SDL_FreeSurface(options_s); | |
| 400 | SDL_Texture* list = SDL_CreateTextureFromSurface(renderer, list_s); | 387 | |
| 401 | SDL_FreeSurface(list_s); | 388 | SDL_Texture* list = SDL_CreateTextureFromSurface(renderer, list_s); |
| 402 | 389 | SDL_FreeSurface(list_s); | |
| 403 | SDL_Event e; | 390 | |
| 404 | 391 | SDL_Event e; | |
| 405 | for (;;) | 392 | |
| 406 | { | 393 | for (;;) { |
| 407 | SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); | 394 | SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); |
| 408 | SDL_RenderClear(renderer); | 395 | SDL_RenderClear(renderer); |
| 409 | SDL_RenderCopy(renderer, list, NULL, NULL); | 396 | SDL_RenderCopy(renderer, list, NULL, NULL); |
| 410 | applyTexture(renderer, pointer, 137, 449); | 397 | applyTexture(renderer, pointer, 137, 449); |
| 411 | SDL_RenderPresent(renderer); | 398 | SDL_RenderPresent(renderer); |
| 412 | 399 | ||
| 413 | while (SDL_PollEvent(&e)) | 400 | while (SDL_PollEvent(&e)) { |
| 414 | { | 401 | if (e.type == SDL_QUIT) { |
| 415 | if (e.type == SDL_QUIT) | 402 | return NULL; |
| 416 | { | 403 | } else if (e.type == SDL_KEYDOWN) { |
| 417 | return NULL; | 404 | if (e.key.keysym.sym == SDLK_RETURN) { |
| 418 | } else if (e.type == SDL_KEYDOWN) | 405 | return new ChooseHighscoreListState(); |
| 419 | { | 406 | } |
| 420 | if (e.key.keysym.sym == SDLK_RETURN) | 407 | } |
| 421 | { | 408 | } |
| 422 | return new ChooseHighscoreListState(); | 409 | } |
| 423 | } | ||
| 424 | } | ||
| 425 | } | ||
| 426 | } | ||
| 427 | } | 410 | } |
| 428 | 411 | ||
| 429 | State* DisplayAndReturnLocalHighscoreListState::operator() (SDL_Window* window, SDL_Renderer* renderer) | 412 | State* DisplayAndReturnLocalHighscoreListState::operator()( |
| 430 | { | 413 | SDL_Window* window, SDL_Renderer* renderer) { |
| 431 | SDL_Texture* pointer = loadImage(renderer, "resources/pointer.bmp"); | 414 | SDL_Texture* pointer = loadImage(renderer, "resources/pointer.bmp"); |
| 432 | 415 | ||
| 433 | LocalHighscoreList* lhl = new LocalHighscoreList(); | 416 | LocalHighscoreList* lhl = new LocalHighscoreList(); |
| 434 | SDL_Surface* list_s = lhl->render(); | 417 | SDL_Surface* list_s = lhl->render(); |
| 435 | SDL_Color fontColor = {0, 0, 0, 0}; | 418 | SDL_Color fontColor = {0, 0, 0, 0}; |
| 436 | SDL_Surface* title = TTF_RenderText_Blended(loadFont(40), "Highscore List", fontColor); | 419 | SDL_Surface* title = |
| 437 | SDL_Rect tSpace = {240-(title->w/2), 0, title->w, title->h}; | 420 | TTF_RenderText_Blended(loadFont(40), "Highscore List", fontColor); |
| 438 | SDL_BlitSurface(title, NULL, list_s, &tSpace); | 421 | SDL_Rect tSpace = {240 - (title->w / 2), 0, title->w, title->h}; |
| 439 | SDL_FreeSurface(title); | 422 | SDL_BlitSurface(title, NULL, list_s, &tSpace); |
| 440 | 423 | SDL_FreeSurface(title); | |
| 441 | SDL_Surface* options_s = SDL_LoadBMP("resources/hlo_paartm.bmp"); | 424 | |
| 442 | SDL_Rect oSpace = {0, 440, options_s->w, options_s->h}; | 425 | SDL_Surface* options_s = SDL_LoadBMP("resources/hlo_paartm.bmp"); |
| 443 | SDL_BlitSurface(options_s, NULL, list_s, &oSpace); | 426 | SDL_Rect oSpace = {0, 440, options_s->w, options_s->h}; |
| 444 | SDL_FreeSurface(options_s); | 427 | SDL_BlitSurface(options_s, NULL, list_s, &oSpace); |
| 445 | 428 | SDL_FreeSurface(options_s); | |
| 446 | SDL_Texture* list = SDL_CreateTextureFromSurface(renderer, list_s); | 429 | |
| 447 | SDL_FreeSurface(list_s); | 430 | SDL_Texture* list = SDL_CreateTextureFromSurface(renderer, list_s); |
| 448 | 431 | SDL_FreeSurface(list_s); | |
| 449 | int selection = 0; | 432 | |
| 450 | SDL_Event e; | 433 | int selection = 0; |
| 451 | 434 | SDL_Event e; | |
| 452 | for (;;) | 435 | |
| 453 | { | 436 | for (;;) { |
| 454 | SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); | 437 | SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); |
| 455 | SDL_RenderClear(renderer); | 438 | SDL_RenderClear(renderer); |
| 456 | SDL_RenderCopy(renderer, list, NULL, NULL); | 439 | SDL_RenderCopy(renderer, list, NULL, NULL); |
| 457 | applyTexture(renderer, pointer, selection==0?52:225, 447); | 440 | applyTexture(renderer, pointer, selection == 0 ? 52 : 225, 447); |
| 458 | SDL_RenderPresent(renderer); | 441 | SDL_RenderPresent(renderer); |
| 459 | 442 | ||
| 460 | while (SDL_PollEvent(&e)) | 443 | while (SDL_PollEvent(&e)) { |
| 461 | { | 444 | if (e.type == SDL_QUIT) { |
| 462 | if (e.type == SDL_QUIT) | 445 | return NULL; |
| 463 | { | 446 | } else if (e.type == SDL_KEYDOWN) { |
| 464 | return NULL; | 447 | if ((e.key.keysym.sym == SDLK_LEFT) && (selection != 0)) { |
| 465 | } else if (e.type == SDL_KEYDOWN) | 448 | selection--; |
| 466 | { | 449 | } else if ((e.key.keysym.sym == SDLK_RIGHT) && (selection != 1)) { |
| 467 | if ((e.key.keysym.sym == SDLK_LEFT) && (selection != 0)) | 450 | selection++; |
| 468 | { | 451 | } else if (e.key.keysym.sym == SDLK_RETURN) { |
| 469 | selection--; | 452 | switch (selection) { |
| 470 | } else if ((e.key.keysym.sym == SDLK_RIGHT) && (selection != 1)) | 453 | case 0: |
| 471 | { | 454 | return new GameState(); |
| 472 | selection++; | 455 | case 1: |
| 473 | } else if (e.key.keysym.sym == SDLK_RETURN) | 456 | return new TitleState(); |
| 474 | { | 457 | } |
| 475 | switch (selection) | 458 | } |
| 476 | { | 459 | } |
| 477 | case 0: return new GameState(); | 460 | } |
| 478 | case 1: return new TitleState(); | 461 | } |
| 479 | } | ||
| 480 | } | ||
| 481 | } | ||
| 482 | } | ||
| 483 | } | ||
| 484 | } | 462 | } |
| 485 | 463 | ||
| 486 | State* DisplayGlobalHighscoreListState::operator() (SDL_Window* window, SDL_Renderer* renderer) | 464 | State* DisplayGlobalHighscoreListState::operator()(SDL_Window* window, |
| 487 | { | 465 | SDL_Renderer* renderer) { |
| 488 | SDL_Texture* pointer = loadImage(renderer, "resources/pointer.bmp"); | 466 | SDL_Texture* pointer = loadImage(renderer, "resources/pointer.bmp"); |
| 489 | 467 | ||
| 490 | // Display loading message | 468 | // Display loading message |
| 491 | SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); | 469 | SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); |
| 492 | SDL_RenderClear(renderer); | 470 | SDL_RenderClear(renderer); |
| 493 | 471 | ||
| 494 | SDL_Surface* list_s = SDL_CreateRGBSurface(0, 480, 480, 32, 0,0,0,0); | 472 | SDL_Surface* list_s = SDL_CreateRGBSurface(0, 480, 480, 32, 0, 0, 0, 0); |
| 495 | Uint32 bgColor = SDL_MapRGB(list_s->format, 255, 255, 255); | 473 | Uint32 bgColor = SDL_MapRGB(list_s->format, 255, 255, 255); |
| 496 | SDL_FillRect(list_s, NULL, bgColor); | 474 | SDL_FillRect(list_s, NULL, bgColor); |
| 497 | SDL_SetColorKey(list_s, SDL_TRUE, bgColor); | 475 | SDL_SetColorKey(list_s, SDL_TRUE, bgColor); |
| 498 | TTF_Font* dataFont = loadFont(25); | 476 | TTF_Font* dataFont = loadFont(25); |
| 499 | SDL_Color fontColor = {0, 0, 0, 0}; | 477 | SDL_Color fontColor = {0, 0, 0, 0}; |
| 500 | SDL_Surface* text = TTF_RenderText_Blended(dataFont, "Fetching highscores....", fontColor); | 478 | SDL_Surface* text = |
| 501 | SDL_Rect aSpace = {240-(text->w/2), 240-(text->h/2), text->w, text->h}; | 479 | TTF_RenderText_Blended(dataFont, "Fetching highscores....", fontColor); |
| 502 | SDL_BlitSurface(text, NULL, list_s, &aSpace); | 480 | SDL_Rect aSpace = {240 - (text->w / 2), 240 - (text->h / 2), text->w, |
| 503 | SDL_FreeSurface(text); | 481 | text->h}; |
| 504 | 482 | SDL_BlitSurface(text, NULL, list_s, &aSpace); | |
| 505 | SDL_Surface* title = TTF_RenderText_Blended(loadFont(40), "Highscore List", fontColor); | 483 | SDL_FreeSurface(text); |
| 506 | SDL_Rect tSpace = {240-(title->w/2), 0, title->w, title->h}; | 484 | |
| 507 | SDL_BlitSurface(title, NULL, list_s, &tSpace); | 485 | SDL_Surface* title = |
| 508 | SDL_FreeSurface(title); | 486 | TTF_RenderText_Blended(loadFont(40), "Highscore List", fontColor); |
| 509 | 487 | SDL_Rect tSpace = {240 - (title->w / 2), 0, title->w, title->h}; | |
| 510 | SDL_Surface* options_s = SDL_LoadBMP("resources/hlo_rtm.bmp"); | 488 | SDL_BlitSurface(title, NULL, list_s, &tSpace); |
| 511 | SDL_Rect oSpace = {0, 440, options_s->w, options_s->h}; | 489 | SDL_FreeSurface(title); |
| 512 | SDL_BlitSurface(options_s, NULL, list_s, &oSpace); | 490 | |
| 513 | SDL_FreeSurface(options_s); | 491 | SDL_Surface* options_s = SDL_LoadBMP("resources/hlo_rtm.bmp"); |
| 514 | 492 | SDL_Rect oSpace = {0, 440, options_s->w, options_s->h}; | |
| 515 | list = SDL_CreateTextureFromSurface(renderer, list_s); | 493 | SDL_BlitSurface(options_s, NULL, list_s, &oSpace); |
| 516 | SDL_FreeSurface(list_s); | 494 | SDL_FreeSurface(options_s); |
| 517 | 495 | ||
| 518 | m = SDL_CreateMutex(); | 496 | list = SDL_CreateTextureFromSurface(renderer, list_s); |
| 519 | 497 | SDL_FreeSurface(list_s); | |
| 520 | // Start downloading scores | 498 | |
| 521 | SDL_CreateThread(&LoadHighscoreList, "LoadHighscoreList", this); | 499 | m = SDL_CreateMutex(); |
| 522 | 500 | ||
| 523 | // Parse keyboard events | 501 | // Start downloading scores |
| 524 | SDL_Event e; | 502 | SDL_CreateThread(&LoadHighscoreList, "LoadHighscoreList", this); |
| 525 | 503 | ||
| 526 | for (;;) | 504 | // Parse keyboard events |
| 527 | { | 505 | SDL_Event e; |
| 528 | if (SDL_LockMutex(m) == 0) | 506 | |
| 529 | { | 507 | for (;;) { |
| 530 | if (lhl != NULL) | 508 | if (SDL_LockMutex(m) == 0) { |
| 531 | { | 509 | if (lhl != NULL) { |
| 532 | SDL_Surface* list_s = lhl->render(); | 510 | SDL_Surface* list_s = lhl->render(); |
| 533 | 511 | ||
| 534 | SDL_Color fontColor = {0, 0, 0, 0}; | 512 | SDL_Color fontColor = {0, 0, 0, 0}; |
| 535 | SDL_Surface* title = TTF_RenderText_Blended(loadFont(40), "Highscore List", fontColor); | 513 | SDL_Surface* title = |
| 536 | SDL_Rect tSpace = {240-(title->w/2), 0, title->w, title->h}; | 514 | TTF_RenderText_Blended(loadFont(40), "Highscore List", fontColor); |
| 537 | SDL_BlitSurface(title, NULL, list_s, &tSpace); | 515 | SDL_Rect tSpace = {240 - (title->w / 2), 0, title->w, title->h}; |
| 538 | SDL_FreeSurface(title); | 516 | SDL_BlitSurface(title, NULL, list_s, &tSpace); |
| 539 | 517 | SDL_FreeSurface(title); | |
| 540 | SDL_Surface* options_s = SDL_LoadBMP("resources/hlo_rtm.bmp"); | 518 | |
| 541 | SDL_Rect oSpace = {0, 440, options_s->w, options_s->h}; | 519 | SDL_Surface* options_s = SDL_LoadBMP("resources/hlo_rtm.bmp"); |
| 542 | SDL_BlitSurface(options_s, NULL, list_s, &oSpace); | 520 | SDL_Rect oSpace = {0, 440, options_s->w, options_s->h}; |
| 543 | SDL_FreeSurface(options_s); | 521 | SDL_BlitSurface(options_s, NULL, list_s, &oSpace); |
| 544 | 522 | SDL_FreeSurface(options_s); | |
| 545 | list = SDL_CreateTextureFromSurface(renderer, list_s); | 523 | |
| 546 | SDL_FreeSurface(list_s); | 524 | list = SDL_CreateTextureFromSurface(renderer, list_s); |
| 547 | 525 | SDL_FreeSurface(list_s); | |
| 548 | lhl = NULL; | 526 | |
| 549 | } | 527 | lhl = NULL; |
| 550 | 528 | } | |
| 551 | SDL_UnlockMutex(m); | 529 | |
| 552 | } | 530 | SDL_UnlockMutex(m); |
| 553 | 531 | } | |
| 554 | SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); | 532 | |
| 555 | SDL_RenderClear(renderer); | 533 | SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); |
| 556 | SDL_RenderCopy(renderer, list, NULL, NULL); | 534 | SDL_RenderClear(renderer); |
| 557 | applyTexture(renderer, pointer, 137, 449); | 535 | SDL_RenderCopy(renderer, list, NULL, NULL); |
| 558 | SDL_RenderPresent(renderer); | 536 | applyTexture(renderer, pointer, 137, 449); |
| 559 | 537 | SDL_RenderPresent(renderer); | |
| 560 | while (SDL_PollEvent(&e)) | 538 | |
| 561 | { | 539 | while (SDL_PollEvent(&e)) { |
| 562 | if (e.type == SDL_QUIT) | 540 | if (e.type == SDL_QUIT) { |
| 563 | { | 541 | SDL_DestroyMutex(m); |
| 564 | SDL_DestroyMutex(m); | 542 | |
| 565 | 543 | return NULL; | |
| 566 | return NULL; | 544 | } else if (e.type == SDL_KEYDOWN) { |
| 567 | } else if (e.type == SDL_KEYDOWN) | 545 | if (e.key.keysym.sym == SDLK_RETURN) { |
| 568 | { | 546 | SDL_DestroyMutex(m); |
| 569 | if (e.key.keysym.sym == SDLK_RETURN) | 547 | |
| 570 | { | 548 | return new ChooseHighscoreListState(); |
| 571 | SDL_DestroyMutex(m); | 549 | } |
| 572 | 550 | } | |
| 573 | return new ChooseHighscoreListState(); | 551 | } |
| 574 | } | 552 | } |
| 575 | } | ||
| 576 | } | ||
| 577 | } | ||
| 578 | } | 553 | } |
| 579 | 554 | ||
| 580 | int DisplayGlobalHighscoreListState::LoadHighscoreList(void* pParam) | 555 | int DisplayGlobalHighscoreListState::LoadHighscoreList(void* pParam) { |
| 581 | { | 556 | DisplayGlobalHighscoreListState* parent = |
| 582 | DisplayGlobalHighscoreListState* parent = ((DisplayGlobalHighscoreListState*)pParam); | 557 | ((DisplayGlobalHighscoreListState*)pParam); |
| 583 | if (SDL_LockMutex(parent->m) == 0) | 558 | if (SDL_LockMutex(parent->m) == 0) { |
| 584 | { | 559 | parent->lhl = new GlobalHighscoreList(); |
| 585 | parent->lhl = new GlobalHighscoreList(); | ||
| 586 | |||
| 587 | SDL_UnlockMutex(parent->m); | ||
| 588 | } else { | ||
| 589 | printf("Couldn't lock mutex: %s\n", SDL_GetError()); | ||
| 590 | } | ||
| 591 | } | ||
| 592 | |||
| 593 | EnterHighscoreState::EnterHighscoreState(int level) | ||
| 594 | { | ||
| 595 | this->level = level; | ||
| 596 | } | ||
| 597 | 560 | ||
| 598 | State* EnterHighscoreState::operator() (SDL_Window* window, SDL_Renderer* renderer) | 561 | SDL_UnlockMutex(parent->m); |
| 599 | { | 562 | } else { |
| 600 | SDL_Texture* pointer = loadImage(renderer, "resources/pointer.bmp"); | 563 | printf("Couldn't lock mutex: %s\n", SDL_GetError()); |
| 601 | 564 | } | |
| 602 | // Render highscore list | ||
| 603 | LocalHighscoreList* lhl = new LocalHighscoreList(); | ||
| 604 | char* emp = new char[1]; | ||
| 605 | emp[0] = 0; | ||
| 606 | Highscore* h = new Highscore(emp, level); | ||
| 607 | int newpos = lhl->addHighscore(h); | ||
| 608 | |||
| 609 | SDL_Surface* list_s = lhl->render(); | ||
| 610 | |||
| 611 | SDL_Color fontColor = {0, 0, 0, 0}; | ||
| 612 | SDL_Surface* title = TTF_RenderText_Blended(loadFont(40), "New Highscore!", fontColor); | ||
| 613 | SDL_Rect tSpace = {240-(title->w/2), 0, title->w, title->h}; | ||
| 614 | SDL_BlitSurface(title, NULL, list_s, &tSpace); | ||
| 615 | SDL_FreeSurface(title); | ||
| 616 | |||
| 617 | this->lp = 0; | ||
| 618 | this->hsname = (char*) calloc(25, sizeof(char)); | ||
| 619 | |||
| 620 | SDL_Surface* text = TTF_RenderText_Blended(loadFont(25), "Enter Your Name", fontColor); | ||
| 621 | SDL_Rect oSpace = {240-(text->w/2), 440, text->w, text->h}; | ||
| 622 | SDL_BlitSurface(text, NULL, list_s, &oSpace); | ||
| 623 | SDL_FreeSurface(text); | ||
| 624 | |||
| 625 | SDL_Texture* list = SDL_CreateTextureFromSurface(renderer, list_s); | ||
| 626 | SDL_FreeSurface(list_s); | ||
| 627 | |||
| 628 | int selection = 0; | ||
| 629 | SDL_Event e; | ||
| 630 | |||
| 631 | int posw, posh; | ||
| 632 | char pos[3]; // 2 max characters in rank plus the colon at the end | ||
| 633 | sprintf(pos, "%d:", newpos); | ||
| 634 | char name[26]; // 25 max characters in username plus the space at the beginning | ||
| 635 | sprintf(name, " %s", hsname); | ||
| 636 | SDL_Surface* newName_s = TTF_RenderText_Blended(loadFont(25), name, fontColor); | ||
| 637 | TTF_SizeText(loadFont(40), pos, &posw, &posh); | ||
| 638 | SDL_Rect rntSpace; | ||
| 639 | rntSpace.x = posw; | ||
| 640 | rntSpace.y = newpos*40+((posh/2)-(newName_s->h/2)); | ||
| 641 | rntSpace.w = newName_s->w; | ||
| 642 | rntSpace.h = newName_s->h; | ||
| 643 | newName = SDL_CreateTextureFromSurface(renderer, newName_s); | ||
| 644 | SDL_FreeSurface(newName_s); | ||
| 645 | |||
| 646 | for (;;) | ||
| 647 | { | ||
| 648 | SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); | ||
| 649 | SDL_RenderClear(renderer); | ||
| 650 | |||
| 651 | SDL_Rect eSpace = {0, newpos*40, 480, 40}; | ||
| 652 | SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255); | ||
| 653 | SDL_RenderFillRect(renderer, &eSpace); | ||
| 654 | |||
| 655 | SDL_RenderCopy(renderer, list, NULL, NULL); | ||
| 656 | SDL_RenderCopy(renderer, newName, NULL, &rntSpace); | ||
| 657 | |||
| 658 | SDL_RenderPresent(renderer); | ||
| 659 | |||
| 660 | while (SDL_PollEvent(&e)) | ||
| 661 | { | ||
| 662 | if (e.type == SDL_QUIT) | ||
| 663 | { | ||
| 664 | return NULL; | ||
| 665 | } else if (e.type == SDL_KEYDOWN) | ||
| 666 | { | ||
| 667 | if ((e.key.keysym.sym == SDLK_BACKSPACE) && (lp > 0)) | ||
| 668 | { | ||
| 669 | hsname[--lp] = 0; | ||
| 670 | |||
| 671 | SDL_Color fontColor = {0, 0, 0, 0}; | ||
| 672 | char name[26]; // 25 max characters in username plus the space at the beginning | ||
| 673 | sprintf(name, " %s", hsname); | ||
| 674 | newName_s = TTF_RenderText_Blended(loadFont(25), name, fontColor); | ||
| 675 | rntSpace.w = newName_s->w; | ||
| 676 | rntSpace.h = newName_s->h; | ||
| 677 | newName = SDL_CreateTextureFromSurface(renderer, newName_s); | ||
| 678 | SDL_FreeSurface(newName_s); | ||
| 679 | } else if ((e.key.keysym.sym == SDLK_RETURN) && (hsname[0] != 0)) | ||
| 680 | { | ||
| 681 | lhl = new LocalHighscoreList(); | ||
| 682 | Highscore* h2 = new Highscore(hsname, level); | ||
| 683 | lhl->addHighscore(h2); | ||
| 684 | lhl->writeHighscores(); | ||
| 685 | |||
| 686 | return new NewHighscoreState(h2); | ||
| 687 | } | ||
| 688 | } else if (e.type == SDL_TEXTINPUT) | ||
| 689 | { | ||
| 690 | if (((*e.text.text & 0xFF80) == 0) && (*e.text.text >= 32 && *e.text.text < 127) && (lp < 25)) | ||
| 691 | { | ||
| 692 | hsname[lp++] = *e.text.text & 0x7f; | ||
| 693 | hsname[lp] = 0; | ||
| 694 | |||
| 695 | SDL_Color fontColor = {0, 0, 0, 0}; | ||
| 696 | char name[26]; // 25 max characters in username plus the space at the beginning | ||
| 697 | sprintf(name, " %s", hsname); | ||
| 698 | newName_s = TTF_RenderText_Blended(loadFont(25), name, fontColor); | ||
| 699 | rntSpace.w = newName_s->w; | ||
| 700 | rntSpace.h = newName_s->h; | ||
| 701 | newName = SDL_CreateTextureFromSurface(renderer, newName_s); | ||
| 702 | SDL_FreeSurface(newName_s); | ||
| 703 | } | ||
| 704 | } | ||
| 705 | } | ||
| 706 | } | ||
| 707 | } | 565 | } |
| 708 | 566 | ||
| 709 | NewHighscoreState::NewHighscoreState(Highscore* h) | 567 | EnterHighscoreState::EnterHighscoreState(int level) { this->level = level; } |
| 710 | { | 568 | |
| 711 | this->h = h; | 569 | State* EnterHighscoreState::operator()(SDL_Window* window, |
| 570 | SDL_Renderer* renderer) { | ||
| 571 | SDL_Texture* pointer = loadImage(renderer, "resources/pointer.bmp"); | ||
| 572 | |||
| 573 | // Render highscore list | ||
| 574 | LocalHighscoreList* lhl = new LocalHighscoreList(); | ||
| 575 | char* emp = new char[1]; | ||
| 576 | emp[0] = 0; | ||
| 577 | Highscore* h = new Highscore(emp, level); | ||
| 578 | int newpos = lhl->addHighscore(h); | ||
| 579 | |||
| 580 | SDL_Surface* list_s = lhl->render(); | ||
| 581 | |||
| 582 | SDL_Color fontColor = {0, 0, 0, 0}; | ||
| 583 | SDL_Surface* title = | ||
| 584 | TTF_RenderText_Blended(loadFont(40), "New Highscore!", fontColor); | ||
| 585 | SDL_Rect tSpace = {240 - (title->w / 2), 0, title->w, title->h}; | ||
| 586 | SDL_BlitSurface(title, NULL, list_s, &tSpace); | ||
| 587 | SDL_FreeSurface(title); | ||
| 588 | |||
| 589 | this->lp = 0; | ||
| 590 | this->hsname = (char*)calloc(25, sizeof(char)); | ||
| 591 | |||
| 592 | SDL_Surface* text = | ||
| 593 | TTF_RenderText_Blended(loadFont(25), "Enter Your Name", fontColor); | ||
| 594 | SDL_Rect oSpace = {240 - (text->w / 2), 440, text->w, text->h}; | ||
| 595 | SDL_BlitSurface(text, NULL, list_s, &oSpace); | ||
| 596 | SDL_FreeSurface(text); | ||
| 597 | |||
| 598 | SDL_Texture* list = SDL_CreateTextureFromSurface(renderer, list_s); | ||
| 599 | SDL_FreeSurface(list_s); | ||
| 600 | |||
| 601 | int selection = 0; | ||
| 602 | SDL_Event e; | ||
| 603 | |||
| 604 | int posw, posh; | ||
| 605 | char pos[3]; // 2 max characters in rank plus the colon at the end | ||
| 606 | sprintf(pos, "%d:", newpos); | ||
| 607 | char name[26]; // 25 max characters in username plus the space at the | ||
| 608 | // beginning | ||
| 609 | sprintf(name, " %s", hsname); | ||
| 610 | SDL_Surface* newName_s = | ||
| 611 | TTF_RenderText_Blended(loadFont(25), name, fontColor); | ||
| 612 | TTF_SizeText(loadFont(40), pos, &posw, &posh); | ||
| 613 | SDL_Rect rntSpace; | ||
| 614 | rntSpace.x = posw; | ||
| 615 | rntSpace.y = newpos * 40 + ((posh / 2) - (newName_s->h / 2)); | ||
| 616 | rntSpace.w = newName_s->w; | ||
| 617 | rntSpace.h = newName_s->h; | ||
| 618 | newName = SDL_CreateTextureFromSurface(renderer, newName_s); | ||
| 619 | SDL_FreeSurface(newName_s); | ||
| 620 | |||
| 621 | for (;;) { | ||
| 622 | SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); | ||
| 623 | SDL_RenderClear(renderer); | ||
| 624 | |||
| 625 | SDL_Rect eSpace = {0, newpos * 40, 480, 40}; | ||
| 626 | SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255); | ||
| 627 | SDL_RenderFillRect(renderer, &eSpace); | ||
| 628 | |||
| 629 | SDL_RenderCopy(renderer, list, NULL, NULL); | ||
| 630 | SDL_RenderCopy(renderer, newName, NULL, &rntSpace); | ||
| 631 | |||
| 632 | SDL_RenderPresent(renderer); | ||
| 633 | |||
| 634 | while (SDL_PollEvent(&e)) { | ||
| 635 | if (e.type == SDL_QUIT) { | ||
| 636 | return NULL; | ||
| 637 | } else if (e.type == SDL_KEYDOWN) { | ||
| 638 | if ((e.key.keysym.sym == SDLK_BACKSPACE) && (lp > 0)) { | ||
| 639 | hsname[--lp] = 0; | ||
| 640 | |||
| 641 | SDL_Color fontColor = {0, 0, 0, 0}; | ||
| 642 | char name[26]; // 25 max characters in username plus the space at the | ||
| 643 | // beginning | ||
| 644 | sprintf(name, " %s", hsname); | ||
| 645 | newName_s = TTF_RenderText_Blended(loadFont(25), name, fontColor); | ||
| 646 | rntSpace.w = newName_s->w; | ||
| 647 | rntSpace.h = newName_s->h; | ||
| 648 | newName = SDL_CreateTextureFromSurface(renderer, newName_s); | ||
| 649 | SDL_FreeSurface(newName_s); | ||
| 650 | } else if ((e.key.keysym.sym == SDLK_RETURN) && (hsname[0] != 0)) { | ||
| 651 | lhl = new LocalHighscoreList(); | ||
| 652 | Highscore* h2 = new Highscore(hsname, level); | ||
| 653 | lhl->addHighscore(h2); | ||
| 654 | lhl->writeHighscores(); | ||
| 655 | |||
| 656 | return new NewHighscoreState(h2); | ||
| 657 | } | ||
| 658 | } else if (e.type == SDL_TEXTINPUT) { | ||
| 659 | if (((*e.text.text & 0xFF80) == 0) && | ||
| 660 | (*e.text.text >= 32 && *e.text.text < 127) && (lp < 25)) { | ||
| 661 | hsname[lp++] = *e.text.text & 0x7f; | ||
| 662 | hsname[lp] = 0; | ||
| 663 | |||
| 664 | SDL_Color fontColor = {0, 0, 0, 0}; | ||
| 665 | char name[26]; // 25 max characters in username plus the space at the | ||
| 666 | // beginning | ||
| 667 | sprintf(name, " %s", hsname); | ||
| 668 | newName_s = TTF_RenderText_Blended(loadFont(25), name, fontColor); | ||
| 669 | rntSpace.w = newName_s->w; | ||
| 670 | rntSpace.h = newName_s->h; | ||
| 671 | newName = SDL_CreateTextureFromSurface(renderer, newName_s); | ||
| 672 | SDL_FreeSurface(newName_s); | ||
| 673 | } | ||
| 674 | } | ||
| 675 | } | ||
| 676 | } | ||
| 712 | } | 677 | } |
| 713 | 678 | ||
| 714 | State* NewHighscoreState::operator() (SDL_Window* window, SDL_Renderer* renderer) | 679 | NewHighscoreState::NewHighscoreState(Highscore* h) { this->h = h; } |
| 715 | { | 680 | |
| 716 | SDL_Texture* pointer = loadImage(renderer, "resources/pointer.bmp"); | 681 | State* NewHighscoreState::operator()(SDL_Window* window, |
| 717 | 682 | SDL_Renderer* renderer) { | |
| 718 | // Render highscore list | 683 | SDL_Texture* pointer = loadImage(renderer, "resources/pointer.bmp"); |
| 719 | LocalHighscoreList* lhl = new LocalHighscoreList(); | 684 | |
| 720 | SDL_Surface* list_s = lhl->render(); | 685 | // Render highscore list |
| 721 | 686 | LocalHighscoreList* lhl = new LocalHighscoreList(); | |
| 722 | SDL_Color fontColor = {0, 0, 0, 0}; | 687 | SDL_Surface* list_s = lhl->render(); |
| 723 | SDL_Surface* title = TTF_RenderText_Blended(loadFont(40), "New Highscore!", fontColor); | 688 | |
| 724 | SDL_Rect tSpace = {240-(title->w/2), 0, title->w, title->h}; | 689 | SDL_Color fontColor = {0, 0, 0, 0}; |
| 725 | SDL_BlitSurface(title, NULL, list_s, &tSpace); | 690 | SDL_Surface* title = |
| 726 | SDL_FreeSurface(title); | 691 | TTF_RenderText_Blended(loadFont(40), "New Highscore!", fontColor); |
| 727 | 692 | SDL_Rect tSpace = {240 - (title->w / 2), 0, title->w, title->h}; | |
| 728 | SDL_Surface* options_s = SDL_LoadBMP("resources/hlo_passartm.bmp"); | 693 | SDL_BlitSurface(title, NULL, list_s, &tSpace); |
| 729 | SDL_Rect oSpace = {0, 440, options_s->w, options_s->h}; | 694 | SDL_FreeSurface(title); |
| 730 | SDL_BlitSurface(options_s, NULL, list_s, &oSpace); | 695 | |
| 731 | SDL_FreeSurface(options_s); | 696 | SDL_Surface* options_s = SDL_LoadBMP("resources/hlo_passartm.bmp"); |
| 732 | 697 | SDL_Rect oSpace = {0, 440, options_s->w, options_s->h}; | |
| 733 | SDL_Texture* list = SDL_CreateTextureFromSurface(renderer, list_s); | 698 | SDL_BlitSurface(options_s, NULL, list_s, &oSpace); |
| 734 | SDL_FreeSurface(list_s); | 699 | SDL_FreeSurface(options_s); |
| 735 | 700 | ||
| 736 | int selection = 0; | 701 | SDL_Texture* list = SDL_CreateTextureFromSurface(renderer, list_s); |
| 737 | SDL_Event e; | 702 | SDL_FreeSurface(list_s); |
| 738 | 703 | ||
| 739 | for (;;) | 704 | int selection = 0; |
| 740 | { | 705 | SDL_Event e; |
| 741 | SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); | 706 | |
| 742 | SDL_RenderClear(renderer); | 707 | for (;;) { |
| 743 | 708 | SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); | |
| 744 | SDL_Rect eSpace = {0, h->getRank()*40, 480, 40}; | 709 | SDL_RenderClear(renderer); |
| 745 | SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255); | 710 | |
| 746 | SDL_RenderFillRect(renderer, &eSpace); | 711 | SDL_Rect eSpace = {0, h->getRank() * 40, 480, 40}; |
| 747 | 712 | SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255); | |
| 748 | SDL_RenderCopy(renderer, list, NULL, NULL); | 713 | SDL_RenderFillRect(renderer, &eSpace); |
| 749 | applyTexture(renderer, pointer, selection==0?13:(selection==1?138:284), 448); | 714 | |
| 750 | SDL_RenderPresent(renderer); | 715 | SDL_RenderCopy(renderer, list, NULL, NULL); |
| 751 | 716 | applyTexture(renderer, pointer, | |
| 752 | while (SDL_PollEvent(&e)) | 717 | selection == 0 ? 13 : (selection == 1 ? 138 : 284), 448); |
| 753 | { | 718 | SDL_RenderPresent(renderer); |
| 754 | if (e.type == SDL_QUIT) | 719 | |
| 755 | { | 720 | while (SDL_PollEvent(&e)) { |
| 756 | return NULL; | 721 | if (e.type == SDL_QUIT) { |
| 757 | } else if (e.type == SDL_KEYDOWN) | 722 | return NULL; |
| 758 | { | 723 | } else if (e.type == SDL_KEYDOWN) { |
| 759 | if ((e.key.keysym.sym == SDLK_LEFT) && (selection != 0)) | 724 | if ((e.key.keysym.sym == SDLK_LEFT) && (selection != 0)) { |
| 760 | { | 725 | selection--; |
| 761 | selection--; | 726 | } else if ((e.key.keysym.sym == SDLK_RIGHT) && (selection != 2)) { |
| 762 | } else if ((e.key.keysym.sym == SDLK_RIGHT) && (selection != 2)) | 727 | selection++; |
| 763 | { | 728 | } else if (e.key.keysym.sym == SDLK_RETURN) { |
| 764 | selection++; | 729 | switch (selection) { |
| 765 | } else if (e.key.keysym.sym == SDLK_RETURN) | 730 | case 0: |
| 766 | { | 731 | return new GameState(); |
| 767 | switch (selection) | 732 | case 1: |
| 768 | { | 733 | return new SubmitHighscoreState(h); |
| 769 | case 0: return new GameState(); | 734 | case 2: |
| 770 | case 1: return new SubmitHighscoreState(h); | 735 | return new TitleState(); |
| 771 | case 2: return new TitleState(); | 736 | } |
| 772 | } | 737 | } |
| 773 | } | 738 | } |
| 774 | } | 739 | } |
| 775 | } | 740 | } |
| 776 | } | ||
| 777 | } | 741 | } |
| 778 | 742 | ||
| 779 | SubmitHighscoreState::SubmitHighscoreState(Highscore* h) | 743 | SubmitHighscoreState::SubmitHighscoreState(Highscore* h) { this->h = h; } |
| 780 | { | 744 | |
| 781 | this->h = h; | 745 | State* SubmitHighscoreState::operator()(SDL_Window* window, |
| 746 | SDL_Renderer* renderer) { | ||
| 747 | SDL_Surface* list_s = SDL_CreateRGBSurface(0, 480, 480, 32, 0, 0, 0, 0); | ||
| 748 | Uint32 bgColor = SDL_MapRGB(list_s->format, 255, 255, 255); | ||
| 749 | SDL_FillRect(list_s, NULL, bgColor); | ||
| 750 | SDL_SetColorKey(list_s, SDL_TRUE, bgColor); | ||
| 751 | TTF_Font* dataFont = loadFont(25); | ||
| 752 | SDL_Color fontColor = {0, 0, 0, 0}; | ||
| 753 | SDL_Surface* text = | ||
| 754 | TTF_RenderText_Blended(dataFont, "Sending highscore....", fontColor); | ||
| 755 | SDL_Rect aSpace = {240 - (text->w / 2), 240 - (text->h / 2), text->w, | ||
| 756 | text->h}; | ||
| 757 | SDL_BlitSurface(text, NULL, list_s, &aSpace); | ||
| 758 | SDL_FreeSurface(text); | ||
| 759 | |||
| 760 | SDL_Surface* title = | ||
| 761 | TTF_RenderText_Blended(loadFont(40), "Highscore List", fontColor); | ||
| 762 | SDL_Rect tSpace = {240 - (title->w / 2), 0, title->w, title->h}; | ||
| 763 | SDL_BlitSurface(title, NULL, list_s, &tSpace); | ||
| 764 | SDL_FreeSurface(title); | ||
| 765 | |||
| 766 | SDL_Texture* list = SDL_CreateTextureFromSurface(renderer, list_s); | ||
| 767 | SDL_FreeSurface(list_s); | ||
| 768 | |||
| 769 | // Start submitting score | ||
| 770 | m = SDL_CreateMutex(); | ||
| 771 | SDL_CreateThread(&SubmitHighscore, "SubmitHighscore", this); | ||
| 772 | |||
| 773 | SDL_Event e; | ||
| 774 | |||
| 775 | for (;;) { | ||
| 776 | SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); | ||
| 777 | SDL_RenderClear(renderer); | ||
| 778 | SDL_RenderCopy(renderer, list, NULL, NULL); | ||
| 779 | SDL_RenderPresent(renderer); | ||
| 780 | |||
| 781 | if (SDL_LockMutex(m) == 0) { | ||
| 782 | if (lhl != NULL) { | ||
| 783 | SDL_UnlockMutex(m); | ||
| 784 | SDL_DestroyMutex(m); | ||
| 785 | |||
| 786 | if (lhl->didFail()) { | ||
| 787 | return new FailedSubmittingHighscoreState(h); | ||
| 788 | } else { | ||
| 789 | return new SubmittedHighscoreState(lhl, h); | ||
| 790 | } | ||
| 791 | } else { | ||
| 792 | SDL_UnlockMutex(m); | ||
| 793 | } | ||
| 794 | } | ||
| 795 | |||
| 796 | while (SDL_PollEvent(&e)) { | ||
| 797 | if (e.type == SDL_QUIT) { | ||
| 798 | SDL_DestroyMutex(m); | ||
| 799 | |||
| 800 | return NULL; | ||
| 801 | } | ||
| 802 | } | ||
| 803 | } | ||
| 782 | } | 804 | } |
| 783 | 805 | ||
| 784 | State* SubmitHighscoreState::operator() (SDL_Window* window, SDL_Renderer* renderer) | 806 | int SubmitHighscoreState::SubmitHighscore(void* pParam) { |
| 785 | { | 807 | SubmitHighscoreState* parent = (SubmitHighscoreState*)pParam; |
| 786 | SDL_Surface* list_s = SDL_CreateRGBSurface(0, 480, 480, 32, 0,0,0,0); | 808 | if (SDL_LockMutex(parent->m) == 0) { |
| 787 | Uint32 bgColor = SDL_MapRGB(list_s->format, 255, 255, 255); | 809 | parent->lhl = new GlobalHighscoreList(parent->h); |
| 788 | SDL_FillRect(list_s, NULL, bgColor); | ||
| 789 | SDL_SetColorKey(list_s, SDL_TRUE, bgColor); | ||
| 790 | TTF_Font* dataFont = loadFont(25); | ||
| 791 | SDL_Color fontColor = {0, 0, 0, 0}; | ||
| 792 | SDL_Surface* text = TTF_RenderText_Blended(dataFont, "Sending highscore....", fontColor); | ||
| 793 | SDL_Rect aSpace = {240-(text->w/2), 240-(text->h/2), text->w, text->h}; | ||
| 794 | SDL_BlitSurface(text, NULL, list_s, &aSpace); | ||
| 795 | SDL_FreeSurface(text); | ||
| 796 | |||
| 797 | SDL_Surface* title = TTF_RenderText_Blended(loadFont(40), "Highscore List", fontColor); | ||
| 798 | SDL_Rect tSpace = {240-(title->w/2), 0, title->w, title->h}; | ||
| 799 | SDL_BlitSurface(title, NULL, list_s, &tSpace); | ||
| 800 | SDL_FreeSurface(title); | ||
| 801 | |||
| 802 | SDL_Texture* list = SDL_CreateTextureFromSurface(renderer, list_s); | ||
| 803 | SDL_FreeSurface(list_s); | ||
| 804 | |||
| 805 | // Start submitting score | ||
| 806 | m = SDL_CreateMutex(); | ||
| 807 | SDL_CreateThread(&SubmitHighscore, "SubmitHighscore", this); | ||
| 808 | |||
| 809 | SDL_Event e; | ||
| 810 | |||
| 811 | for (;;) | ||
| 812 | { | ||
| 813 | SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); | ||
| 814 | SDL_RenderClear(renderer); | ||
| 815 | SDL_RenderCopy(renderer, list, NULL, NULL); | ||
| 816 | SDL_RenderPresent(renderer); | ||
| 817 | |||
| 818 | if (SDL_LockMutex(m) == 0) | ||
| 819 | { | ||
| 820 | if (lhl != NULL) | ||
| 821 | { | ||
| 822 | SDL_UnlockMutex(m); | ||
| 823 | SDL_DestroyMutex(m); | ||
| 824 | |||
| 825 | if (lhl->didFail()) | ||
| 826 | { | ||
| 827 | return new FailedSubmittingHighscoreState(h); | ||
| 828 | } else { | ||
| 829 | return new SubmittedHighscoreState(lhl, h); | ||
| 830 | } | ||
| 831 | } else { | ||
| 832 | SDL_UnlockMutex(m); | ||
| 833 | } | ||
| 834 | } | ||
| 835 | |||
| 836 | while (SDL_PollEvent(&e)) | ||
| 837 | { | ||
| 838 | if (e.type == SDL_QUIT) | ||
| 839 | { | ||
| 840 | SDL_DestroyMutex(m); | ||
| 841 | |||
| 842 | return NULL; | ||
| 843 | } | ||
| 844 | } | ||
| 845 | } | ||
| 846 | } | ||
| 847 | 810 | ||
| 848 | int SubmitHighscoreState::SubmitHighscore(void* pParam) | 811 | SDL_UnlockMutex(parent->m); |
| 849 | { | 812 | } else { |
| 850 | SubmitHighscoreState* parent = (SubmitHighscoreState*) pParam; | 813 | printf("Could not lock mutex: %s\n", SDL_GetError()); |
| 851 | if (SDL_LockMutex(parent->m) == 0) | 814 | } |
| 852 | { | ||
| 853 | parent->lhl = new GlobalHighscoreList(parent->h); | ||
| 854 | |||
| 855 | SDL_UnlockMutex(parent->m); | ||
| 856 | } else { | ||
| 857 | printf("Could not lock mutex: %s\n", SDL_GetError()); | ||
| 858 | } | ||
| 859 | } | 815 | } |
| 860 | 816 | ||
| 861 | FailedSubmittingHighscoreState::FailedSubmittingHighscoreState(Highscore* h) | 817 | FailedSubmittingHighscoreState::FailedSubmittingHighscoreState(Highscore* h) { |
| 862 | { | 818 | this->h = h; |
| 863 | this->h = h; | ||
| 864 | } | 819 | } |
| 865 | 820 | ||
| 866 | State* FailedSubmittingHighscoreState::operator() (SDL_Window* window, SDL_Renderer* renderer) | 821 | State* FailedSubmittingHighscoreState::operator()(SDL_Window* window, |
| 867 | { | 822 | SDL_Renderer* renderer) { |
| 868 | SDL_Surface* list_s = SDL_CreateRGBSurface(0, 480, 480, 32, 0,0,0,0); | 823 | SDL_Surface* list_s = SDL_CreateRGBSurface(0, 480, 480, 32, 0, 0, 0, 0); |
| 869 | Uint32 bgColor = SDL_MapRGB(list_s->format, 255, 255, 255); | 824 | Uint32 bgColor = SDL_MapRGB(list_s->format, 255, 255, 255); |
| 870 | SDL_FillRect(list_s, NULL, bgColor); | 825 | SDL_FillRect(list_s, NULL, bgColor); |
| 871 | SDL_SetColorKey(list_s, SDL_TRUE, bgColor); | 826 | SDL_SetColorKey(list_s, SDL_TRUE, bgColor); |
| 872 | TTF_Font* dataFont = loadFont(25); | 827 | TTF_Font* dataFont = loadFont(25); |
| 873 | SDL_Color fontColor = {0, 0, 0, 0}; | 828 | SDL_Color fontColor = {0, 0, 0, 0}; |
| 874 | SDL_Surface* text = TTF_RenderText_Blended(dataFont, "Error submitting highscores", fontColor); | 829 | SDL_Surface* text = TTF_RenderText_Blended( |
| 875 | SDL_Rect tSpace = {240-(text->w/2), 240-(text->h/2), text->w, text->h}; | 830 | dataFont, "Error submitting highscores", fontColor); |
| 876 | SDL_BlitSurface(text, NULL, list_s, &tSpace); | 831 | SDL_Rect tSpace = {240 - (text->w / 2), 240 - (text->h / 2), text->w, |
| 877 | SDL_FreeSurface(text); | 832 | text->h}; |
| 878 | 833 | SDL_BlitSurface(text, NULL, list_s, &tSpace); | |
| 879 | SDL_Surface* title = TTF_RenderText_Blended(loadFont(40), "Highscore List", fontColor); | 834 | SDL_FreeSurface(text); |
| 880 | SDL_Rect aSpace = {240-(title->w/2), 0, title->w, title->h}; | 835 | |
| 881 | SDL_BlitSurface(title, NULL, list_s, &aSpace); | 836 | SDL_Surface* title = |
| 882 | SDL_FreeSurface(title); | 837 | TTF_RenderText_Blended(loadFont(40), "Highscore List", fontColor); |
| 883 | 838 | SDL_Rect aSpace = {240 - (title->w / 2), 0, title->w, title->h}; | |
| 884 | SDL_Surface* options_s = SDL_LoadBMP("resources/hlo_passartm.bmp"); | 839 | SDL_BlitSurface(title, NULL, list_s, &aSpace); |
| 885 | SDL_Rect oSpace = {0, 440, options_s->w, options_s->h}; | 840 | SDL_FreeSurface(title); |
| 886 | SDL_BlitSurface(options_s, NULL, list_s, &oSpace); | 841 | |
| 887 | SDL_FreeSurface(options_s); | 842 | SDL_Surface* options_s = SDL_LoadBMP("resources/hlo_passartm.bmp"); |
| 888 | 843 | SDL_Rect oSpace = {0, 440, options_s->w, options_s->h}; | |
| 889 | SDL_Texture* list = SDL_CreateTextureFromSurface(renderer, list_s); | 844 | SDL_BlitSurface(options_s, NULL, list_s, &oSpace); |
| 890 | SDL_FreeSurface(list_s); | 845 | SDL_FreeSurface(options_s); |
| 891 | 846 | ||
| 892 | SDL_Texture* pointer = loadImage(renderer, "resources/pointer.bmp"); | 847 | SDL_Texture* list = SDL_CreateTextureFromSurface(renderer, list_s); |
| 893 | int selection = 0; | 848 | SDL_FreeSurface(list_s); |
| 894 | SDL_Event e; | 849 | |
| 895 | 850 | SDL_Texture* pointer = loadImage(renderer, "resources/pointer.bmp"); | |
| 896 | for (;;) | 851 | int selection = 0; |
| 897 | { | 852 | SDL_Event e; |
| 898 | SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); | 853 | |
| 899 | SDL_RenderClear(renderer); | 854 | for (;;) { |
| 900 | SDL_RenderCopy(renderer, list, NULL, NULL); | 855 | SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); |
| 901 | applyTexture(renderer, pointer, selection==0?13:(selection==1?138:284), 448); | 856 | SDL_RenderClear(renderer); |
| 902 | SDL_RenderPresent(renderer); | 857 | SDL_RenderCopy(renderer, list, NULL, NULL); |
| 903 | 858 | applyTexture(renderer, pointer, | |
| 904 | while (SDL_PollEvent(&e)) | 859 | selection == 0 ? 13 : (selection == 1 ? 138 : 284), 448); |
| 905 | { | 860 | SDL_RenderPresent(renderer); |
| 906 | if (e.type == SDL_QUIT) | 861 | |
| 907 | { | 862 | while (SDL_PollEvent(&e)) { |
| 908 | return NULL; | 863 | if (e.type == SDL_QUIT) { |
| 909 | } else if (e.type == SDL_KEYDOWN) | 864 | return NULL; |
| 910 | { | 865 | } else if (e.type == SDL_KEYDOWN) { |
| 911 | if ((e.key.keysym.sym == SDLK_LEFT) && (selection != 0)) | 866 | if ((e.key.keysym.sym == SDLK_LEFT) && (selection != 0)) { |
| 912 | { | 867 | selection--; |
| 913 | selection--; | 868 | } else if ((e.key.keysym.sym == SDLK_RIGHT) && (selection != 2)) { |
| 914 | } else if ((e.key.keysym.sym == SDLK_RIGHT) && (selection != 2)) | 869 | selection++; |
| 915 | { | 870 | } else if (e.key.keysym.sym == SDLK_RETURN) { |
| 916 | selection++; | 871 | switch (selection) { |
| 917 | } else if (e.key.keysym.sym == SDLK_RETURN) | 872 | case 0: |
| 918 | { | 873 | return new GameState(); |
| 919 | switch (selection) | 874 | case 1: |
| 920 | { | 875 | return new SubmitHighscoreState(h); |
| 921 | case 0: return new GameState(); | 876 | case 2: |
| 922 | case 1: return new SubmitHighscoreState(h); | 877 | return new TitleState(); |
| 923 | case 2: return new TitleState(); | 878 | } |
| 924 | } | 879 | } |
| 925 | } | 880 | } |
| 926 | } | 881 | } |
| 927 | } | 882 | } |
| 928 | } | ||
| 929 | } | 883 | } |
| 930 | 884 | ||
| 931 | SubmittedHighscoreState::SubmittedHighscoreState(GlobalHighscoreList* lhl, Highscore* h) | 885 | SubmittedHighscoreState::SubmittedHighscoreState(GlobalHighscoreList* lhl, |
| 932 | { | 886 | Highscore* h) { |
| 933 | this->lhl = lhl; | 887 | this->lhl = lhl; |
| 934 | this->h = h; | 888 | this->h = h; |
| 935 | } | 889 | } |
| 936 | 890 | ||
| 937 | State* SubmittedHighscoreState::operator() (SDL_Window* window, SDL_Renderer* renderer) | 891 | State* SubmittedHighscoreState::operator()(SDL_Window* window, |
| 938 | { | 892 | SDL_Renderer* renderer) { |
| 939 | SDL_Surface* list_s = lhl->render(); | 893 | SDL_Surface* list_s = lhl->render(); |
| 940 | 894 | ||
| 941 | SDL_Color fontColor = {0, 0, 0, 0}; | 895 | SDL_Color fontColor = {0, 0, 0, 0}; |
| 942 | SDL_Surface* title = TTF_RenderText_Blended(loadFont(40), "Highscore List", fontColor); | 896 | SDL_Surface* title = |
| 943 | SDL_Rect tSpace = {240-(title->w/2), 0, title->w, title->h}; | 897 | TTF_RenderText_Blended(loadFont(40), "Highscore List", fontColor); |
| 944 | SDL_BlitSurface(title, NULL, list_s, &tSpace); | 898 | SDL_Rect tSpace = {240 - (title->w / 2), 0, title->w, title->h}; |
| 945 | SDL_FreeSurface(title); | 899 | SDL_BlitSurface(title, NULL, list_s, &tSpace); |
| 946 | 900 | SDL_FreeSurface(title); | |
| 947 | SDL_Surface* options_s = SDL_LoadBMP("resources/hlo_paartm.bmp"); | 901 | |
| 948 | SDL_Rect oSpace = {0, 440, options_s->w, options_s->h}; | 902 | SDL_Surface* options_s = SDL_LoadBMP("resources/hlo_paartm.bmp"); |
| 949 | SDL_BlitSurface(options_s, NULL, list_s, &oSpace); | 903 | SDL_Rect oSpace = {0, 440, options_s->w, options_s->h}; |
| 950 | SDL_FreeSurface(options_s); | 904 | SDL_BlitSurface(options_s, NULL, list_s, &oSpace); |
| 951 | 905 | SDL_FreeSurface(options_s); | |
| 952 | SDL_Texture* list = SDL_CreateTextureFromSurface(renderer, list_s); | 906 | |
| 953 | SDL_FreeSurface(list_s); | 907 | SDL_Texture* list = SDL_CreateTextureFromSurface(renderer, list_s); |
| 954 | 908 | SDL_FreeSurface(list_s); | |
| 955 | SDL_Texture* pointer = loadImage(renderer, "resources/pointer.bmp"); | 909 | |
| 956 | int selection = 0; | 910 | SDL_Texture* pointer = loadImage(renderer, "resources/pointer.bmp"); |
| 957 | SDL_Event e; | 911 | int selection = 0; |
| 958 | 912 | SDL_Event e; | |
| 959 | int newpos = h->getRank(); | 913 | |
| 960 | if (newpos > 10) | 914 | int newpos = h->getRank(); |
| 961 | { | 915 | if (newpos > 10) { |
| 962 | newpos = 10; | 916 | newpos = 10; |
| 963 | } | 917 | } |
| 964 | 918 | ||
| 965 | for (;;) | 919 | for (;;) { |
| 966 | { | 920 | SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); |
| 967 | SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); | 921 | SDL_RenderClear(renderer); |
| 968 | SDL_RenderClear(renderer); | 922 | |
| 969 | 923 | SDL_Rect eSpace = {0, newpos * 40, 480, 40}; | |
| 970 | SDL_Rect eSpace = {0, newpos*40, 480, 40}; | 924 | SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255); |
| 971 | SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255); | 925 | SDL_RenderFillRect(renderer, &eSpace); |
| 972 | SDL_RenderFillRect(renderer, &eSpace); | 926 | |
| 973 | 927 | SDL_RenderCopy(renderer, list, NULL, NULL); | |
| 974 | SDL_RenderCopy(renderer, list, NULL, NULL); | 928 | applyTexture(renderer, pointer, selection == 0 ? 52 : 225, 447); |
| 975 | applyTexture(renderer, pointer, selection==0?52:225, 447); | 929 | SDL_RenderPresent(renderer); |
| 976 | SDL_RenderPresent(renderer); | 930 | |
| 977 | 931 | while (SDL_PollEvent(&e)) { | |
| 978 | while (SDL_PollEvent(&e)) | 932 | if (e.type == SDL_QUIT) { |
| 979 | { | 933 | return NULL; |
| 980 | if (e.type == SDL_QUIT) | 934 | } else if (e.type == SDL_KEYDOWN) { |
| 981 | { | 935 | if ((e.key.keysym.sym == SDLK_LEFT) && (selection != 0)) { |
| 982 | return NULL; | 936 | selection--; |
| 983 | } else if (e.type == SDL_KEYDOWN) | 937 | } else if ((e.key.keysym.sym == SDLK_RIGHT) && (selection != 1)) { |
| 984 | { | 938 | selection++; |
| 985 | if ((e.key.keysym.sym == SDLK_LEFT) && (selection != 0)) | 939 | } else if (e.key.keysym.sym == SDLK_RETURN) { |
| 986 | { | 940 | switch (selection) { |
| 987 | selection--; | 941 | case 0: |
| 988 | } else if ((e.key.keysym.sym == SDLK_RIGHT) && (selection != 1)) | 942 | return new GameState(); |
| 989 | { | 943 | case 1: |
| 990 | selection++; | 944 | return new TitleState(); |
| 991 | } else if (e.key.keysym.sym == SDLK_RETURN) | 945 | } |
| 992 | { | 946 | } |
| 993 | switch (selection) | 947 | } |
| 994 | { | 948 | } |
| 995 | case 0: return new GameState(); | 949 | } |
| 996 | case 1: return new TitleState(); | ||
| 997 | } | ||
| 998 | } | ||
| 999 | } | ||
| 1000 | } | ||
| 1001 | } | ||
| 1002 | } | 950 | } |
