#include "hslist.h" #include #include #include #include #include "util.h" SDL_Surface* HighscoreList::render() { SDL_Surface* tmp = SDL_CreateRGBSurface(0, 480, 480, 32, 0,0,0,0); Uint32 bgColor = SDL_MapRGB(tmp->format, 255, 255, 255); SDL_FillRect(tmp, NULL, bgColor); SDL_SetColorKey(tmp, SDL_TRUE, bgColor); TTF_Font* posFont = loadFont(40); TTF_Font* dataFont = loadFont(25); SDL_Color fontColor = {0, 0, 0, 0}; for (int i=0; i HighscoreList::getLocalHighscores() { std::vector temp = std::vector(); std::ifstream exists(getDataFile()); if (exists) { FILE* hslist = fopen(getDataFile(), "r"); int scores; fscanf(hslist, "%d%*c", &scores); for (int i=0; i HighscoreList::getGlobalHighscores() { IPaddress ipaddress; if (SDLNet_ResolveHost(&ipaddress, "other.fourisland.com", 80) == -1) { printf("Could not resolve host \"other.fourisland.com\": %s\n", SDLNet_GetError()); throw 1; } TCPsocket tcpsock = SDLNet_TCP_Open(&ipaddress); if (!tcpsock) { printf("Could not connect to host \"other.fourisland.com\": %s\n", SDLNet_GetError()); 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"; if (SDLNet_TCP_Send(tcpsock, headers, strlen(headers)+1) < strlen(headers)) { printf("Connection closed by peer: %s\n", SDLNet_GetError()); throw 3; } std::stringstream download(std::stringstream::in | std::stringstream::out); char hslist[1024]; SDLNet_TCP_Recv(tcpsock, hslist, 1024); download << hslist; SDLNet_TCP_Close(tcpsock); char temps[256]; download.getline(temps,256); while (strlen(temps) != 1) { download.getline(temps,256); } std::vector temp = std::vector(); int scores; download.getline(temps, 256); if (sscanf(temps, "%d%*c", &scores) != 1) { printf("Recieved data is of an invalid format: %s\n", temps); throw 4; } for (int i=0; ihslist = getLocalHighscores(); } GlobalHighscoreList::GlobalHighscoreList() { fail = false; try { this->hslist = getGlobalHighscores(); } catch (int e) { fail = true; } } SDL_Surface* GlobalHighscoreList::render() { if (fail) { SDL_Surface* tmp = SDL_CreateRGBSurface(0, 480, 480, 32, 0,0,0,0); Uint32 bgColor = SDL_MapRGB(tmp->format, 255, 255, 255); SDL_FillRect(tmp, NULL, bgColor); SDL_SetColorKey(tmp, SDL_TRUE, bgColor); TTF_Font* dataFont = loadFont(25); SDL_Color fontColor = {0, 0, 0, 0}; SDL_Surface* text = TTF_RenderText_Blended(dataFont, "Error retrieving highscores", fontColor); SDL_Rect tSpace = {240-(text->w/2), 240-(text->h/2), text->w, text->h}; SDL_BlitSurface(text, NULL, tmp, &tSpace); return tmp; } else { return super::render(); } }