From 45d6e635c880a7fae8711fba366519dd314d9faf Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Thu, 2 Nov 2023 09:17:25 -0400 Subject: Formatted source code --- .clang-format | 2 + gamestate.cpp | 933 +++++++++++++---------------- gamestate.h | 5 +- highscore.cpp | 27 +- highscore.h | 20 +- hslist.cpp | 1814 +++++++++++++++++++++++++++----------------------------- hslist.h | 141 ++--- mazeoflife.cpp | 100 ++-- state.h | 9 +- titlestate.cpp | 216 ++++--- titlestate.h | 13 +- util.cpp | 89 ++- util.h | 1 + 13 files changed, 1603 insertions(+), 1767 deletions(-) create mode 100644 .clang-format diff --git a/.clang-format b/.clang-format new file mode 100644 index 0000000..8de7fe6 --- /dev/null +++ b/.clang-format @@ -0,0 +1,2 @@ +--- + BasedOnStyle: Google diff --git a/gamestate.cpp b/gamestate.cpp index 8c2df9c..e77ff76 100644 --- a/gamestate.cpp +++ b/gamestate.cpp @@ -1,378 +1,363 @@ #include "gamestate.h" + #include -#include "util.h" -#include "mazeoflife.h" -#include "highscore.h" -#include "titlestate.h" -#include -#include "hslist.h" -#include -#include -#include + #include -#include +#include #include +#include #include -#include #include +#include +#include +#include #include #include +#include + +#include "highscore.h" +#include "hslist.h" +#include "mazeoflife.h" +#include "titlestate.h" +#include "util.h" class GameBoard { - public: - GameBoard(int level, int playerx, int playery); - void tick(int playerx, int playery); - void render(SDL_Renderer* renderer, int level) const; - bool isObstructed(int x, int y) const; - bool operator<(const GameBoard& other) const; - - using coord = std::tuple; - - private: - void initialize(int level); - bool solve(int playerx, int playery) const; - std::string dump() const; - - using board_type = std::bitset; - - void incrementIfNeighbor( - int x, - int y, - const board_type& temp, - int playerx, - int playery, - int& tick) const; - - bool applyNeighbors( - int x, - int y, - const board_type& temp, - int playerx, - int playery) const; - - board_type blocks; - board_type updateable; - int oldx; - int oldy; + public: + GameBoard(int level, int playerx, int playery); + void tick(int playerx, int playery); + void render(SDL_Renderer* renderer, int level) const; + bool isObstructed(int x, int y) const; + bool operator<(const GameBoard& other) const; + + using coord = std::tuple; + + private: + void initialize(int level); + bool solve(int playerx, int playery) const; + std::string dump() const; + + using board_type = std::bitset; + + void incrementIfNeighbor(int x, int y, const board_type& temp, int playerx, + int playery, int& tick) const; + + bool applyNeighbors(int x, int y, const board_type& temp, int playerx, + int playery) const; + + board_type blocks; + board_type updateable; + int oldx; + int oldy; }; class LoadGameState : public State { - public: - LoadGameState(int level); - State* operator() (SDL_Window* window, SDL_Renderer* renderer); + public: + LoadGameState(int level); + State* operator()(SDL_Window* window, SDL_Renderer* renderer); - private: - int level; + private: + int level; }; class PlayGameState : public State { - public: - PlayGameState(int level, GameBoard* board, int playerx, int playery); - State* operator() (SDL_Window* window, SDL_Renderer* renderer); - - private: - bool move(int x, int y); - int level; - GameBoard* board; - int playerx; - int playery; + public: + PlayGameState(int level, GameBoard* board, int playerx, int playery); + State* operator()(SDL_Window* window, SDL_Renderer* renderer); + + private: + bool move(int x, int y); + int level; + GameBoard* board; + int playerx; + int playery; }; -void setRendererAliveColor(SDL_Renderer* renderer, int level) -{ - switch ((level/10)%5) - { - case 0: SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); break; // Black - case 1: SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); break; // Red - case 2: SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255); break; // Green - case 3: SDL_SetRenderDrawColor(renderer, 85, 85, 85, 255); break; // Dark Gray - case 4: SDL_SetRenderDrawColor(renderer, 255, 0, 255, 255); break; // Magenta - } +void setRendererAliveColor(SDL_Renderer* renderer, int level) { + switch ((level / 10) % 5) { + case 0: + SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255); + break; // Black + case 1: + SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255); + break; // Red + case 2: + SDL_SetRenderDrawColor(renderer, 0, 255, 0, 255); + break; // Green + case 3: + SDL_SetRenderDrawColor(renderer, 85, 85, 85, 255); + break; // Dark Gray + case 4: + SDL_SetRenderDrawColor(renderer, 255, 0, 255, 255); + break; // Magenta + } } -void setRendererDeadColor(SDL_Renderer* renderer, int level) -{ - switch ((level/10)%5) - { - case 0: SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); break; // White - case 1: SDL_SetRenderDrawColor(renderer, 255, 192, 203, 255); break; // Pink - case 2: SDL_SetRenderDrawColor(renderer, 0, 255, 255, 255); break; // Cyan - case 3: SDL_SetRenderDrawColor(renderer, 170, 170, 170, 255); break; // Light Gray - case 4: SDL_SetRenderDrawColor(renderer, 255, 128, 0, 255); break; // Orange - } +void setRendererDeadColor(SDL_Renderer* renderer, int level) { + switch ((level / 10) % 5) { + case 0: + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + break; // White + case 1: + SDL_SetRenderDrawColor(renderer, 255, 192, 203, 255); + break; // Pink + case 2: + SDL_SetRenderDrawColor(renderer, 0, 255, 255, 255); + break; // Cyan + case 3: + SDL_SetRenderDrawColor(renderer, 170, 170, 170, 255); + break; // Light Gray + case 4: + SDL_SetRenderDrawColor(renderer, 255, 128, 0, 255); + break; // Orange + } } -void GameBoard::incrementIfNeighbor( - int x, - int y, - const board_type& temp, - int playerx, - int playery, - int& tick) const -{ - int nx = x; - int ny = y; - - wrap(x, y); - - if (!((nx!=x)&&(ny!=y))) - { - if ((temp[x+y*WIDTH])||((playerx==x)&&(playery==y))||((x==15)&&(y==15))) - { - ++tick; - } - } -} +void GameBoard::incrementIfNeighbor(int x, int y, const board_type& temp, + int playerx, int playery, int& tick) const { + int nx = x; + int ny = y; + + wrap(x, y); -State* GameState::operator() (SDL_Window* window, SDL_Renderer* renderer) -{ - return new LoadGameState(0); + if (!((nx != x) && (ny != y))) { + if ((temp[x + y * WIDTH]) || ((playerx == x) && (playery == y)) || + ((x == 15) && (y == 15))) { + ++tick; + } + } } -LoadGameState::LoadGameState(int m_level) -{ - level = m_level; +State* GameState::operator()(SDL_Window* window, SDL_Renderer* renderer) { + return new LoadGameState(0); } -State* LoadGameState::operator() (SDL_Window* window, SDL_Renderer* renderer) -{ - char* wintitle = new char[50]; - sprintf(wintitle, "Maze Of Life - Level %d", level); - SDL_SetWindowTitle(window, wintitle); - - // Randomly place the player in a corner - int playerx, playery; - switch (rand()%4) - { - case 0: playerx = 1; playery = 1; break; - case 1: playerx = 1; playery = HEIGHT-2; break; - case 2: playerx = WIDTH-2; playery = HEIGHT-2; break; - case 3: playerx = WIDTH-2; playery = 1; break; - } - - // Display the level number - setRendererDeadColor(renderer, level); - SDL_RenderClear(renderer); - - TTF_Font* font = loadFont(100); - SDL_Color fontColor = {0, 0, 0, 0}; - char levelnum[8]; - sprintf(levelnum, "%d", level); - SDL_Surface* dispsurf = TTF_RenderText_Solid(font, levelnum, fontColor); - SDL_Texture* disptext = SDL_CreateTextureFromSurface(renderer, dispsurf); - SDL_FreeSurface(dispsurf); - - SDL_Rect pos; - SDL_QueryTexture(disptext, NULL, NULL, &pos.w, &pos.h); - pos.x = 240-(pos.w/2); - pos.y = 240-(pos.h/2); - - SDL_RenderCopy(renderer, disptext, NULL, &pos); - SDL_RenderPresent(renderer); - - // Do 50 gens of Conway - GameBoard* board = new GameBoard(level, playerx, playery); - - // Wait a bit - SDL_Delay(500); - - // Start the level - return new PlayGameState(level, board, playerx, playery); +LoadGameState::LoadGameState(int m_level) { level = m_level; } + +State* LoadGameState::operator()(SDL_Window* window, SDL_Renderer* renderer) { + char* wintitle = new char[50]; + sprintf(wintitle, "Maze Of Life - Level %d", level); + SDL_SetWindowTitle(window, wintitle); + + // Randomly place the player in a corner + int playerx, playery; + switch (rand() % 4) { + case 0: + playerx = 1; + playery = 1; + break; + case 1: + playerx = 1; + playery = HEIGHT - 2; + break; + case 2: + playerx = WIDTH - 2; + playery = HEIGHT - 2; + break; + case 3: + playerx = WIDTH - 2; + playery = 1; + break; + } + + // Display the level number + setRendererDeadColor(renderer, level); + SDL_RenderClear(renderer); + + TTF_Font* font = loadFont(100); + SDL_Color fontColor = {0, 0, 0, 0}; + char levelnum[8]; + sprintf(levelnum, "%d", level); + SDL_Surface* dispsurf = TTF_RenderText_Solid(font, levelnum, fontColor); + SDL_Texture* disptext = SDL_CreateTextureFromSurface(renderer, dispsurf); + SDL_FreeSurface(dispsurf); + + SDL_Rect pos; + SDL_QueryTexture(disptext, NULL, NULL, &pos.w, &pos.h); + pos.x = 240 - (pos.w / 2); + pos.y = 240 - (pos.h / 2); + + SDL_RenderCopy(renderer, disptext, NULL, &pos); + SDL_RenderPresent(renderer); + + // Do 50 gens of Conway + GameBoard* board = new GameBoard(level, playerx, playery); + + // Wait a bit + SDL_Delay(500); + + // Start the level + return new PlayGameState(level, board, playerx, playery); } -PlayGameState::PlayGameState(int m_level, GameBoard* m_board, int m_playerx, int m_playery) -{ - level = m_level; - board = m_board; - playerx = m_playerx; - playery = m_playery; +PlayGameState::PlayGameState(int m_level, GameBoard* m_board, int m_playerx, + int m_playery) { + level = m_level; + board = m_board; + playerx = m_playerx; + playery = m_playery; } -State* PlayGameState::operator() (SDL_Window* window, SDL_Renderer* renderer) -{ - SDL_Event e; - - for (;;) - { - // Tick board - board->tick(playerx, playery); - - // Paint board - board->render(renderer, level); - - // Paint event - SDL_Rect block; - block.w = 16; - block.h = 16; - block.x = 15*16; - block.y = 15*16; - SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255); - SDL_RenderFillRect(renderer, &block); - - // Paint player - block.x = playerx*16; - block.y = playery*16; - SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255); - SDL_RenderFillRect(renderer, &block); - - SDL_RenderPresent(renderer); - - while (SDL_PollEvent(&e)) - { - if (e.type == SDL_QUIT) - { - return NULL; - } else if (e.type == SDL_KEYDOWN) - { - switch (e.key.keysym.sym) - { - case SDLK_LEFT: - if (move(playerx-1, playery)) - { - return new LoadGameState(level+1); - } else { - break; - } - - case SDLK_RIGHT: - if (move(playerx+1, playery)) - { - return new LoadGameState(level+1); - } else { - break; - } - - case SDLK_UP: - if (move(playerx, playery-1)) - { - return new LoadGameState(level+1); - } else { - break; - } - - case SDLK_DOWN: - if (move(playerx, playery+1)) - { - return new LoadGameState(level+1); - } else { - break; - } - - case SDLK_ESCAPE: - SDL_SetWindowTitle(window, ""); - - std::ifstream exists(getDataFile()); - if (exists) - { - FILE* hslist = fopen(getDataFile(), "r"); - int scores; - Highscore* h; - - fscanf(hslist, "%d%*c", &scores); - - if (scores < 10) - { - fclose(hslist); - - return new EnterHighscoreState(level); - } else { - for (int i=0; igetLevel() < level) - { - return new EnterHighscoreState(level); - } else { - return new DisplayAndReturnLocalHighscoreListState(); - } - } - } else { - return new EnterHighscoreState(level); - } - } - } - } - - SDL_Delay(5); - } +State* PlayGameState::operator()(SDL_Window* window, SDL_Renderer* renderer) { + SDL_Event e; + + for (;;) { + // Tick board + board->tick(playerx, playery); + + // Paint board + board->render(renderer, level); + + // Paint event + SDL_Rect block; + block.w = 16; + block.h = 16; + block.x = 15 * 16; + block.y = 15 * 16; + SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255); + SDL_RenderFillRect(renderer, &block); + + // Paint player + block.x = playerx * 16; + block.y = playery * 16; + SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255); + SDL_RenderFillRect(renderer, &block); + + SDL_RenderPresent(renderer); + + while (SDL_PollEvent(&e)) { + if (e.type == SDL_QUIT) { + return NULL; + } else if (e.type == SDL_KEYDOWN) { + switch (e.key.keysym.sym) { + case SDLK_LEFT: + if (move(playerx - 1, playery)) { + return new LoadGameState(level + 1); + } else { + break; + } + + case SDLK_RIGHT: + if (move(playerx + 1, playery)) { + return new LoadGameState(level + 1); + } else { + break; + } + + case SDLK_UP: + if (move(playerx, playery - 1)) { + return new LoadGameState(level + 1); + } else { + break; + } + + case SDLK_DOWN: + if (move(playerx, playery + 1)) { + return new LoadGameState(level + 1); + } else { + break; + } + + case SDLK_ESCAPE: + SDL_SetWindowTitle(window, ""); + + std::ifstream exists(getDataFile()); + if (exists) { + FILE* hslist = fopen(getDataFile(), "r"); + int scores; + Highscore* h; + + fscanf(hslist, "%d%*c", &scores); + + if (scores < 10) { + fclose(hslist); + + return new EnterHighscoreState(level); + } else { + for (int i = 0; i < scores; i++) { + int namelen; + char namelens[4]; + char* name = (char*)calloc(25, sizeof(char)); + int score; + + fscanf(hslist, "%d", &namelen); + sprintf(namelens, "%%%dc", namelen); + fscanf(hslist, namelens, name); + fscanf(hslist, "%d%*c", &score); + + h = new Highscore(name, score); + } + + fclose(hslist); + + if (h->getLevel() < level) { + return new EnterHighscoreState(level); + } else { + return new DisplayAndReturnLocalHighscoreListState(); + } + } + } else { + return new EnterHighscoreState(level); + } + } + } + } + + SDL_Delay(5); + } } -bool PlayGameState::move(int x, int y) -{ - wrap(x, y); +bool PlayGameState::move(int x, int y) { + wrap(x, y); - // Are we at the event? - if ((x==15)&&(y==15)) - { - return true; - } + // Are we at the event? + if ((x == 15) && (y == 15)) { + return true; + } - // Can we even go there? - if (!board->isObstructed(x,y)) - { - playerx = x; - playery = y; - } + // Can we even go there? + if (!board->isObstructed(x, y)) { + playerx = x; + playery = y; + } - return false; + return false; } -GameBoard::GameBoard(int level, int playerx, int playery) -{ - for (;;) - { +GameBoard::GameBoard(int level, int playerx, int playery) { + for (;;) { initialize(level); updateable.set(); oldx = playerx; oldy = playery; - for (int i=0; i<50; i++) - { + for (int i = 0; i < 50; i++) { tick(playerx, playery); } - if (solve(playerx, playery)) - { + if (solve(playerx, playery)) { break; } else { - std::cout << "Impossible board: " << playerx << "," << playery << "," << dump() << std::endl; + std::cout << "Impossible board: " << playerx << "," << playery << "," + << dump() << std::endl; } } } -void GameBoard::tick(int playerx, int playery) -{ - board_type temp {blocks}; - board_type tempdateable {updateable}; - if ((playerx != oldx) || (playery != oldy)) - { - for (int dy = -1; dy <= 1; dy++) - { - for (int dx = -1; dx <=1; dx++) - { - int tdx = oldx+dx; - int tdy = oldy+dy; +void GameBoard::tick(int playerx, int playery) { + board_type temp{blocks}; + board_type tempdateable{updateable}; + if ((playerx != oldx) || (playery != oldy)) { + for (int dy = -1; dy <= 1; dy++) { + for (int dx = -1; dx <= 1; dx++) { + int tdx = oldx + dx; + int tdy = oldy + dy; wrap(tdx, tdy); - tempdateable.set(tdx+tdy*WIDTH); + tempdateable.set(tdx + tdy * WIDTH); - tdx = playerx+dx; - tdy = playery+dy; + tdx = playerx + dx; + tdy = playery + dy; wrap(tdx, tdy); - tempdateable.set(tdx+tdy*WIDTH); + tempdateable.set(tdx + tdy * WIDTH); } } } @@ -382,135 +367,108 @@ void GameBoard::tick(int playerx, int playery) updateable.reset(); - for (int y=0;y= 1) && (neighbors <= 4)); - } else { - return (neighbors == 3); - } +bool GameBoard::applyNeighbors(int x, int y, const board_type& temp, + int playerx, int playery) const { + int neighbors = 0; + + incrementIfNeighbor(x - 1, y - 1, temp, playerx, playery, neighbors); + incrementIfNeighbor(x - 1, y, temp, playerx, playery, neighbors); + incrementIfNeighbor(x - 1, y + 1, temp, playerx, playery, neighbors); + incrementIfNeighbor(x, y - 1, temp, playerx, playery, neighbors); + incrementIfNeighbor(x, y + 1, temp, playerx, playery, neighbors); + incrementIfNeighbor(x + 1, y - 1, temp, playerx, playery, neighbors); + incrementIfNeighbor(x + 1, y, temp, playerx, playery, neighbors); + incrementIfNeighbor(x + 1, y + 1, temp, playerx, playery, neighbors); + + if (temp[x + y * WIDTH]) { + return ((neighbors >= 1) && (neighbors <= 4)); + } else { + return (neighbors == 3); + } } -void GameBoard::render(SDL_Renderer* renderer, int level) const -{ - SDL_Rect block; - block.w = 16; - block.h = 16; - - for (int y=0; y13)&&(x<17)&&(y>13)&&(y<17)) - { - blocks[x+y*WIDTH] = rand() % 2; - } - break; - case 2: - case 3: - if ((x>12)&&(x<18)&&(y>12)&&(y<18)) - { - blocks[x+y*WIDTH] = rand() % 2; - } - break; - case 4: - case 5: - if ((x>11)&&(x<19)&&(y>11)&&(y<19)) - { - blocks[x+y*WIDTH] = rand() % 2; - } - break; - default: - blocks[x+y*WIDTH] = rand() % 2; - } - } - } - - blocks[15+15*WIDTH] = false; +void GameBoard::initialize(int level) { + for (int y = 0; y < HEIGHT; y++) { + for (int x = 0; x < WIDTH; x++) { + blocks[x + y * WIDTH] = false; + + switch (level / 10 + 1) { + case 1: + if ((x > 13) && (x < 17) && (y > 13) && (y < 17)) { + blocks[x + y * WIDTH] = rand() % 2; + } + break; + case 2: + case 3: + if ((x > 12) && (x < 18) && (y > 12) && (y < 18)) { + blocks[x + y * WIDTH] = rand() % 2; + } + break; + case 4: + case 5: + if ((x > 11) && (x < 19) && (y > 11) && (y < 19)) { + blocks[x + y * WIDTH] = rand() % 2; + } + break; + default: + blocks[x + y * WIDTH] = rand() % 2; + } + } + } + + blocks[15 + 15 * WIDTH] = false; } -bool GameBoard::operator<(const GameBoard& other) const -{ - for (int i = WIDTH*HEIGHT-1; i >= 0; i--) - { - if (blocks[i] ^ other.blocks[i]) - { +bool GameBoard::operator<(const GameBoard& other) const { + for (int i = WIDTH * HEIGHT - 1; i >= 0; i--) { + if (blocks[i] ^ other.blocks[i]) { return other.blocks[i]; } } @@ -518,8 +476,7 @@ bool GameBoard::operator<(const GameBoard& other) const return false; } -bool GameBoard::solve(int playerx, int playery) const -{ +bool GameBoard::solve(int playerx, int playery) const { std::deque> search; std::unordered_map done; @@ -532,12 +489,10 @@ bool GameBoard::solve(int playerx, int playery) const std::unordered_set pastStates; pastStates.insert(original.blocks); - while (original.updateable.any()) - { + while (original.updateable.any()) { original.tick(playerx, playery); - if (pastStates.count(original.blocks)) - { + if (pastStates.count(original.blocks)) { break; } @@ -549,8 +504,7 @@ bool GameBoard::solve(int playerx, int playery) const // Use breadth first search to find a solution. bool exists = false; - while (!search.empty()) - { + while (!search.empty()) { auto cur = std::move(search.front()); search.pop_front(); @@ -559,8 +513,7 @@ bool GameBoard::solve(int playerx, int playery) const int cns = std::get<2>(cur); // If it has been over 100 generations, give up. - if (cns > 100) - { + if (cns > 100) { continue; } @@ -568,8 +521,7 @@ bool GameBoard::solve(int playerx, int playery) const int cply = std::get<1>(cpl); // If this section of this board state has already been checked, skip it. - if (done.count(cbr.blocks) && done.at(cbr.blocks)[cplx+cply*WIDTH]) - { + if (done.count(cbr.blocks) && done.at(cbr.blocks)[cplx + cply * WIDTH]) { continue; } @@ -579,44 +531,39 @@ bool GameBoard::solve(int playerx, int playery) const board_type flood; std::deque front; front.push_front(cpl); - flood[cplx+cply*WIDTH] = true; + flood[cplx + cply * WIDTH] = true; std::set edges; - while (!front.empty()) - { + while (!front.empty()) { coord frontLoc = std::move(front.front()); front.pop_front(); // Iterate over the positions 4-adjacent to the current one. for (coord& fc : std::list{ - {std::get<0>(frontLoc) - 1, std::get<1>(frontLoc) }, - {std::get<0>(frontLoc) + 1, std::get<1>(frontLoc) }, - {std::get<0>(frontLoc) , std::get<1>(frontLoc) - 1}, - {std::get<0>(frontLoc) , std::get<1>(frontLoc) + 1}, - }) - { + {std::get<0>(frontLoc) - 1, std::get<1>(frontLoc)}, + {std::get<0>(frontLoc) + 1, std::get<1>(frontLoc)}, + {std::get<0>(frontLoc), std::get<1>(frontLoc) - 1}, + {std::get<0>(frontLoc), std::get<1>(frontLoc) + 1}, + }) { wrap(std::get<0>(fc), std::get<1>(fc)); int fcx = std::get<0>(fc); int fcy = std::get<1>(fc); // If this position is already in the flood, skip it. - if (flood[fcx+fcy*WIDTH]) - { + if (flood[fcx + fcy * WIDTH]) { continue; } // If the player could not move into this position, skip it. - if (cbr.isObstructed(fcx, fcy)) - { + if (cbr.isObstructed(fcx, fcy)) { continue; } // If this position is adjacent to the event, then the board is // solvable. if (((fcx == 15) && ((fcy == 14) || (fcy == 16))) || - ((fcy == 15) && ((fcx == 14) || (fcx == 16)))) - { + ((fcy == 15) && ((fcx == 14) || (fcx == 16)))) { exists = true; break; } @@ -625,12 +572,9 @@ bool GameBoard::solve(int playerx, int playery) const // the start or end positions to change. This is more efficient than // copying the board state and then running tick. bool changed = false; - for (int dy = -1; dy <= 1; dy++) - { - for (int dx = -1; dx <=1; dx++) - { - if (dx == 0 && dy == 0) - { + for (int dy = -1; dy <= 1; dy++) { + for (int dx = -1; dx <= 1; dx++) { + if (dx == 0 && dy == 0) { continue; } @@ -639,13 +583,7 @@ bool GameBoard::solve(int playerx, int playery) const wrap(cpldx, cpldy); if (cbr.isObstructed(cpldx, cpldy) != - applyNeighbors( - cpldx, - cpldy, - cbr.blocks, - fcx, - fcy)) - { + applyNeighbors(cpldx, cpldy, cbr.blocks, fcx, fcy)) { changed = true; break; } @@ -655,43 +593,33 @@ bool GameBoard::solve(int playerx, int playery) const wrap(fcxdx, fcydy); if (cbr.isObstructed(fcxdx, fcydy) != - applyNeighbors( - fcxdx, - fcydy, - cbr.blocks, - fcx, - fcy)) - { + applyNeighbors(fcxdx, fcydy, cbr.blocks, fcx, fcy)) { changed = true; break; } } - if (changed) - { + if (changed) { break; } } // If moving to this position would change the board state, add it to // the set of edges; otherwise, add it to the flood and the flood front. - if (changed) - { + if (changed) { edges.insert(fc); } else { - flood[fcx+fcy*WIDTH] = true; + flood[fcx + fcy * WIDTH] = true; front.push_back(fc); } } - if (exists) - { + if (exists) { break; } } - if (exists) - { + if (exists) { break; } @@ -699,8 +627,7 @@ bool GameBoard::solve(int playerx, int playery) const done[cbr.blocks] |= flood; // Add the edges to the search queue. - for (const coord& newLoc : edges) - { + for (const coord& newLoc : edges) { GameBoard nextState1 = cbr; nextState1.tick(std::get<0>(newLoc), std::get<1>(newLoc)); @@ -710,12 +637,10 @@ bool GameBoard::solve(int playerx, int playery) const std::unordered_set pastStates; pastStates.insert(nextState1.blocks); - while (nextState1.updateable.any()) - { + while (nextState1.updateable.any()) { nextState1.tick(std::get<0>(newLoc), std::get<1>(newLoc)); - if (pastStates.count(nextState1.blocks)) - { + if (pastStates.count(nextState1.blocks)) { break; } @@ -723,10 +648,9 @@ bool GameBoard::solve(int playerx, int playery) const } if (!done.count(nextState1.blocks) || - !done.at(nextState1.blocks) - [std::get<0>(newLoc) + std::get<1>(newLoc)*WIDTH]) - { - search.emplace_back(std::move(nextState1), newLoc, cns+1); + !done.at(nextState1.blocks)[std::get<0>(newLoc) + + std::get<1>(newLoc) * WIDTH]) { + search.emplace_back(std::move(nextState1), newLoc, cns + 1); } } } @@ -734,13 +658,12 @@ bool GameBoard::solve(int playerx, int playery) const return exists; } -std::string GameBoard::dump() const -{ +std::string GameBoard::dump() const { std::stringstream output; output << std::hex; - for (int i=0; i + #include "state.h" #ifndef GAMESTATE_H #define GAMESTATE_H class GameState : public State { - public: - State* operator() (SDL_Window* window, SDL_Renderer* renderer); + public: + State* operator()(SDL_Window* window, SDL_Renderer* renderer); }; #endif \ No newline at end of file diff --git a/highscore.cpp b/highscore.cpp index 7133e1a..eefe058 100644 --- a/highscore.cpp +++ b/highscore.cpp @@ -1,27 +1,14 @@ #include "highscore.h" -Highscore::Highscore(char* name, int level) -{ - this->name = name; - this->level = level; +Highscore::Highscore(char* name, int level) { + this->name = name; + this->level = level; } -char* Highscore::getName() -{ - return name; -} +char* Highscore::getName() { return name; } -int Highscore::getLevel() -{ - return level; -} +int Highscore::getLevel() { return level; } -void Highscore::setRank(int rank) -{ - this->rank = rank; -} +void Highscore::setRank(int rank) { this->rank = rank; } -int Highscore::getRank() -{ - return rank; -} +int Highscore::getRank() { return rank; } diff --git a/highscore.h b/highscore.h index 721b226..f7ecacc 100644 --- a/highscore.h +++ b/highscore.h @@ -2,17 +2,17 @@ #define HIGHSCORE_H class Highscore { - public: - Highscore(char* name, int level); - char* getName(); - int getLevel(); - void setRank(int rank); - int getRank(); + public: + Highscore(char* name, int level); + char* getName(); + int getLevel(); + void setRank(int rank); + int getRank(); - private: - char* name; - int level; - int rank; + private: + char* name; + int level; + int rank; }; #endif diff --git a/hslist.cpp b/hslist.cpp index f71d8be..5eb9b6c 100644 --- a/hslist.cpp +++ b/hslist.cpp @@ -1,1002 +1,950 @@ #include "hslist.h" -#include + #include -#include -#include +#include + #include -#include "util.h" -#include "titlestate.h" +#include +#include + #include "gamestate.h" +#include "titlestate.h" +#include "util.h" // We want to be able to sort Highscore objects in descending score order struct hslist_comp { - bool operator() (Highscore* lhs, Highscore* rhs) const - { - return (lhs->getLevel() > rhs->getLevel()); - } + bool operator()(Highscore* lhs, Highscore* rhs) const { + return (lhs->getLevel() > rhs->getLevel()); + } } hslist_comp_i; -// resetRanks : sets the rank of all Highscore objects in a hslist_t to their (one-based) index in the list -void resetRanks(hslist_t in) -{ - int i=1; - for (hslist_t::iterator it = in.begin(); it != in.end(); ++it, ++i) - { - ((Highscore*)*it)->setRank(i); - } +// resetRanks : sets the rank of all Highscore objects in a hslist_t to their +// (one-based) index in the list +void resetRanks(hslist_t in) { + int i = 1; + for (hslist_t::iterator it = in.begin(); it != in.end(); ++it, ++i) { + ((Highscore*)*it)->setRank(i); + } } -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}; - - int i=0; - for (hslist_t::iterator it = hslist.begin(); it != hslist.end(); ++it, ++i) - { - Highscore* h = *it; - - int posw, posh; - char pos[4]; // 2 max characters in rank plus the colon at the end, plus terminator - 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); - - int namew, nameh; - char name[27]; // 25 max characters in username plus the space at the beginning, plus terminator - sprintf(name, " %s", h->getName()); - TTF_SizeText(dataFont, name, &namew, &nameh); - SDL_Rect nameSpace = {posw, (i+1)*40+((posh/2)-(nameh/2)), namew, nameh}; - SDL_BlitSurface(TTF_RenderText_Blended(dataFont, name, fontColor), NULL, tmp, &nameSpace); - - int lvlw, lvlh; - 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 - sprintf(lvl, "%d", h->getLevel()); - TTF_SizeText(dataFont, lvl, &lvlw, &lvlh); - SDL_Rect lvlSpace = {480-lvlw, (i+1)*40+((posh/2)-(nameh/2)), lvlw, lvlh}; - SDL_BlitSurface(TTF_RenderText_Blended(dataFont, lvl, fontColor), NULL, tmp, &lvlSpace); - } - - return tmp; +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}; + + int i = 0; + for (hslist_t::iterator it = hslist.begin(); it != hslist.end(); ++it, ++i) { + Highscore* h = *it; + + int posw, posh; + char pos[4]; // 2 max characters in rank plus the colon at the end, plus + // terminator + 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); + + int namew, nameh; + char name[27]; // 25 max characters in username plus the space at the + // beginning, plus terminator + sprintf(name, " %s", h->getName()); + TTF_SizeText(dataFont, name, &namew, &nameh); + SDL_Rect nameSpace = {posw, (i + 1) * 40 + ((posh / 2) - (nameh / 2)), + namew, nameh}; + SDL_BlitSurface(TTF_RenderText_Blended(dataFont, name, fontColor), NULL, + tmp, &nameSpace); + + int lvlw, lvlh; + 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 + sprintf(lvl, "%d", h->getLevel()); + TTF_SizeText(dataFont, lvl, &lvlw, &lvlh); + SDL_Rect lvlSpace = {480 - lvlw, (i + 1) * 40 + ((posh / 2) - (nameh / 2)), + lvlw, lvlh}; + SDL_BlitSurface(TTF_RenderText_Blended(dataFont, lvl, fontColor), NULL, tmp, + &lvlSpace); + } + + return tmp; } -hslist_t HighscoreList::getLocalHighscores() -{ - hslist_t temp; +hslist_t HighscoreList::getLocalHighscores() { + hslist_t temp; - std::ifstream exists(getDataFile()); - if (exists) - { - FILE* hslist = fopen(getDataFile(), "r"); - int scores; - fscanf(hslist, "%d%*c", &scores); + std::ifstream exists(getDataFile()); + if (exists) { + FILE* hslist = fopen(getDataFile(), "r"); + int scores; + fscanf(hslist, "%d%*c", &scores); - for (int i=0; isetRank(i+1); + Highscore* h = new Highscore(name, score); + h->setRank(i + 1); - temp.push_back(h); - } + temp.push_back(h); + } - fclose(hslist); - } + fclose(hslist); + } - return temp; + return temp; } -hslist_t 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; - } - - 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()); - 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); - } - - hslist_t temp; - 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; isetRank(i+1); - - temp.push_back(h); - } - - return temp; +hslist_t 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; + } + + 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()); + 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); + } + + hslist_t temp; + 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; i < scores; i++) { + int namelen; + char namelens[13]; + char* name = (char*)calloc(25, sizeof(char)); + int score; + download.getline(temps, 256); + + if (sscanf(temps, "%d", &namelen) != 1) { + printf("Recieved data is of an invalid format (1-%d): %s\n", i, temps); + throw 4; + } + + sprintf(namelens, "%%*d%%%dc", namelen); + + if (sscanf(temps, namelens, name) != 1) { + printf("Recieved data is of an invalid format (2-%d): %s\n", i, temps); + throw 4; + } + + sprintf(namelens, "%%*d%%*%dc%%d", namelen); + + if (sscanf(temps, namelens, &score) != 1) { + printf("Recieved data is of an invalid format (3-%d): %s\n", i, temps); + throw 4; + } + + Highscore* h = new Highscore(name, score); + h->setRank(i + 1); + + temp.push_back(h); + } + + return temp; } -LocalHighscoreList::LocalHighscoreList() -{ - this->hslist = getLocalHighscores(); +LocalHighscoreList::LocalHighscoreList() { + this->hslist = getLocalHighscores(); } -int LocalHighscoreList::addHighscore(Highscore* h) -{ - hslist.push_back(h); - std::sort(hslist.begin(), hslist.end(), hslist_comp_i); - resetRanks(hslist); +int LocalHighscoreList::addHighscore(Highscore* h) { + hslist.push_back(h); + std::sort(hslist.begin(), hslist.end(), hslist_comp_i); + resetRanks(hslist); - if (hslist.size() > 10) - { - hslist.resize(10); - } + if (hslist.size() > 10) { + hslist.resize(10); + } - return h->getRank(); + return h->getRank(); } -void LocalHighscoreList::writeHighscores() -{ - FILE* hsfile = fopen(getDataFile(), "w"); - fprintf(hsfile, "%d ", (int) this->hslist.size()); +void LocalHighscoreList::writeHighscores() { + FILE* hsfile = fopen(getDataFile(), "w"); + fprintf(hsfile, "%d ", (int)this->hslist.size()); - for (hslist_t::iterator it = hslist.begin(); it != this->hslist.end(); it++) - { - Highscore* h = *it; + for (hslist_t::iterator it = hslist.begin(); it != this->hslist.end(); it++) { + Highscore* h = *it; - fprintf(hsfile, "%d%s%d ", (int) strlen(h->getName()), h->getName(), h->getLevel()); - } + fprintf(hsfile, "%d%s%d ", (int)strlen(h->getName()), h->getName(), + h->getLevel()); + } - fclose(hsfile); + fclose(hsfile); } -GlobalHighscoreList::GlobalHighscoreList() -{ - fail = false; - - try - { - this->hslist = getGlobalHighscores(); - } catch (int e) - { - fail = true; - } -} +GlobalHighscoreList::GlobalHighscoreList() { + fail = false; -GlobalHighscoreList::GlobalHighscoreList(Highscore* h) -{ - fail = false; - - try - { - 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 body[256]; - sprintf(body, "name=%s&level=%d", h->getName(), h->getLevel()); - char headers[256]; - 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); - 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); - } - - int rank; - download.getline(temps, 256); - if (sscanf(temps, "%d%*c", &rank) != 1) - { - printf("Recieved data is of an invalid format: %s\n", temps); - throw 4; - } - - this->hslist = getGlobalHighscores(); - - if (this->hslist.empty()) - { - printf("Global Highscore List cannot be empty after adding a score to it.\n"); - throw 5; - } - - if (rank > 10) - { - h->setRank(rank); - - this->hslist[9] = h; - } else { - this->hslist.push_back(h); - std::sort(this->hslist.begin(), this->hslist.end(), hslist_comp_i); - resetRanks(this->hslist); - - if (this->hslist.size() > 10) - { - this->hslist.resize(10); - } - } - } catch (int e) - { - fail = true; - } + 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(); - } +GlobalHighscoreList::GlobalHighscoreList(Highscore* h) { + fail = false; + + try { + 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 body[256]; + sprintf(body, "name=%s&level=%d", h->getName(), h->getLevel()); + char headers[256]; + 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); + 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); + } + + int rank; + download.getline(temps, 256); + if (sscanf(temps, "%d%*c", &rank) != 1) { + printf("Recieved data is of an invalid format: %s\n", temps); + throw 4; + } + + this->hslist = getGlobalHighscores(); + + if (this->hslist.empty()) { + printf( + "Global Highscore List cannot be empty after adding a score to " + "it.\n"); + throw 5; + } + + if (rank > 10) { + h->setRank(rank); + + this->hslist[9] = h; + } else { + this->hslist.push_back(h); + std::sort(this->hslist.begin(), this->hslist.end(), hslist_comp_i); + resetRanks(this->hslist); + + if (this->hslist.size() > 10) { + this->hslist.resize(10); + } + } + } catch (int e) { + fail = true; + } } -bool GlobalHighscoreList::didFail() -{ - return fail; +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(); + } } -State* ChooseHighscoreListState::operator() (SDL_Window* window, 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(); - } - } - } - } - } +bool GlobalHighscoreList::didFail() { return fail; } + +State* ChooseHighscoreListState::operator()(SDL_Window* window, + 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_Window* window, 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* DisplayLocalHighscoreListState::operator()(SDL_Window* window, + 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* DisplayAndReturnLocalHighscoreListState::operator() (SDL_Window* window, 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_paartm.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); - - int selection = 0; - SDL_Event e; - - for (;;) - { - SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); - SDL_RenderClear(renderer); - SDL_RenderCopy(renderer, list, NULL, NULL); - applyTexture(renderer, pointer, selection==0?52:225, 447); - 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_LEFT) && (selection != 0)) - { - selection--; - } else if ((e.key.keysym.sym == SDLK_RIGHT) && (selection != 1)) - { - selection++; - } else if (e.key.keysym.sym == SDLK_RETURN) - { - switch (selection) - { - case 0: return new GameState(); - case 1: return new TitleState(); - } - } - } - } - } +State* DisplayAndReturnLocalHighscoreListState::operator()( + SDL_Window* window, 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_paartm.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); + + int selection = 0; + SDL_Event e; + + for (;;) { + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + SDL_RenderClear(renderer); + SDL_RenderCopy(renderer, list, NULL, NULL); + applyTexture(renderer, pointer, selection == 0 ? 52 : 225, 447); + 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_LEFT) && (selection != 0)) { + selection--; + } else if ((e.key.keysym.sym == SDLK_RIGHT) && (selection != 1)) { + selection++; + } else if (e.key.keysym.sym == SDLK_RETURN) { + switch (selection) { + case 0: + return new GameState(); + case 1: + return new TitleState(); + } + } + } + } + } } -State* DisplayGlobalHighscoreListState::operator() (SDL_Window* window, 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(); - } - } - } - } +State* DisplayGlobalHighscoreListState::operator()(SDL_Window* window, + 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()); - } -} - -EnterHighscoreState::EnterHighscoreState(int level) -{ - this->level = level; -} +int DisplayGlobalHighscoreListState::LoadHighscoreList(void* pParam) { + DisplayGlobalHighscoreListState* parent = + ((DisplayGlobalHighscoreListState*)pParam); + if (SDL_LockMutex(parent->m) == 0) { + parent->lhl = new GlobalHighscoreList(); -State* EnterHighscoreState::operator() (SDL_Window* window, SDL_Renderer* renderer) -{ - SDL_Texture* pointer = loadImage(renderer, "resources/pointer.bmp"); - - // Render highscore list - LocalHighscoreList* lhl = new LocalHighscoreList(); - char* emp = new char[1]; - emp[0] = 0; - Highscore* h = new Highscore(emp, level); - int newpos = lhl->addHighscore(h); - - SDL_Surface* list_s = lhl->render(); - - SDL_Color fontColor = {0, 0, 0, 0}; - SDL_Surface* title = TTF_RenderText_Blended(loadFont(40), "New Highscore!", fontColor); - SDL_Rect tSpace = {240-(title->w/2), 0, title->w, title->h}; - SDL_BlitSurface(title, NULL, list_s, &tSpace); - SDL_FreeSurface(title); - - this->lp = 0; - this->hsname = (char*) calloc(25, sizeof(char)); - - SDL_Surface* text = TTF_RenderText_Blended(loadFont(25), "Enter Your Name", fontColor); - SDL_Rect oSpace = {240-(text->w/2), 440, text->w, text->h}; - SDL_BlitSurface(text, NULL, list_s, &oSpace); - SDL_FreeSurface(text); - - SDL_Texture* list = SDL_CreateTextureFromSurface(renderer, list_s); - SDL_FreeSurface(list_s); - - int selection = 0; - SDL_Event e; - - int posw, posh; - char pos[3]; // 2 max characters in rank plus the colon at the end - sprintf(pos, "%d:", newpos); - char name[26]; // 25 max characters in username plus the space at the beginning - sprintf(name, " %s", hsname); - SDL_Surface* newName_s = TTF_RenderText_Blended(loadFont(25), name, fontColor); - TTF_SizeText(loadFont(40), pos, &posw, &posh); - SDL_Rect rntSpace; - rntSpace.x = posw; - rntSpace.y = newpos*40+((posh/2)-(newName_s->h/2)); - rntSpace.w = newName_s->w; - rntSpace.h = newName_s->h; - newName = SDL_CreateTextureFromSurface(renderer, newName_s); - SDL_FreeSurface(newName_s); - - for (;;) - { - SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); - SDL_RenderClear(renderer); - - SDL_Rect eSpace = {0, newpos*40, 480, 40}; - SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255); - SDL_RenderFillRect(renderer, &eSpace); - - SDL_RenderCopy(renderer, list, NULL, NULL); - SDL_RenderCopy(renderer, newName, NULL, &rntSpace); - - 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_BACKSPACE) && (lp > 0)) - { - hsname[--lp] = 0; - - SDL_Color fontColor = {0, 0, 0, 0}; - char name[26]; // 25 max characters in username plus the space at the beginning - sprintf(name, " %s", hsname); - newName_s = TTF_RenderText_Blended(loadFont(25), name, fontColor); - rntSpace.w = newName_s->w; - rntSpace.h = newName_s->h; - newName = SDL_CreateTextureFromSurface(renderer, newName_s); - SDL_FreeSurface(newName_s); - } else if ((e.key.keysym.sym == SDLK_RETURN) && (hsname[0] != 0)) - { - lhl = new LocalHighscoreList(); - Highscore* h2 = new Highscore(hsname, level); - lhl->addHighscore(h2); - lhl->writeHighscores(); - - return new NewHighscoreState(h2); - } - } else if (e.type == SDL_TEXTINPUT) - { - if (((*e.text.text & 0xFF80) == 0) && (*e.text.text >= 32 && *e.text.text < 127) && (lp < 25)) - { - hsname[lp++] = *e.text.text & 0x7f; - hsname[lp] = 0; - - SDL_Color fontColor = {0, 0, 0, 0}; - char name[26]; // 25 max characters in username plus the space at the beginning - sprintf(name, " %s", hsname); - newName_s = TTF_RenderText_Blended(loadFont(25), name, fontColor); - rntSpace.w = newName_s->w; - rntSpace.h = newName_s->h; - newName = SDL_CreateTextureFromSurface(renderer, newName_s); - SDL_FreeSurface(newName_s); - } - } - } - } + SDL_UnlockMutex(parent->m); + } else { + printf("Couldn't lock mutex: %s\n", SDL_GetError()); + } } -NewHighscoreState::NewHighscoreState(Highscore* h) -{ - this->h = h; +EnterHighscoreState::EnterHighscoreState(int level) { this->level = level; } + +State* EnterHighscoreState::operator()(SDL_Window* window, + SDL_Renderer* renderer) { + SDL_Texture* pointer = loadImage(renderer, "resources/pointer.bmp"); + + // Render highscore list + LocalHighscoreList* lhl = new LocalHighscoreList(); + char* emp = new char[1]; + emp[0] = 0; + Highscore* h = new Highscore(emp, level); + int newpos = lhl->addHighscore(h); + + SDL_Surface* list_s = lhl->render(); + + SDL_Color fontColor = {0, 0, 0, 0}; + SDL_Surface* title = + TTF_RenderText_Blended(loadFont(40), "New Highscore!", fontColor); + SDL_Rect tSpace = {240 - (title->w / 2), 0, title->w, title->h}; + SDL_BlitSurface(title, NULL, list_s, &tSpace); + SDL_FreeSurface(title); + + this->lp = 0; + this->hsname = (char*)calloc(25, sizeof(char)); + + SDL_Surface* text = + TTF_RenderText_Blended(loadFont(25), "Enter Your Name", fontColor); + SDL_Rect oSpace = {240 - (text->w / 2), 440, text->w, text->h}; + SDL_BlitSurface(text, NULL, list_s, &oSpace); + SDL_FreeSurface(text); + + SDL_Texture* list = SDL_CreateTextureFromSurface(renderer, list_s); + SDL_FreeSurface(list_s); + + int selection = 0; + SDL_Event e; + + int posw, posh; + char pos[3]; // 2 max characters in rank plus the colon at the end + sprintf(pos, "%d:", newpos); + char name[26]; // 25 max characters in username plus the space at the + // beginning + sprintf(name, " %s", hsname); + SDL_Surface* newName_s = + TTF_RenderText_Blended(loadFont(25), name, fontColor); + TTF_SizeText(loadFont(40), pos, &posw, &posh); + SDL_Rect rntSpace; + rntSpace.x = posw; + rntSpace.y = newpos * 40 + ((posh / 2) - (newName_s->h / 2)); + rntSpace.w = newName_s->w; + rntSpace.h = newName_s->h; + newName = SDL_CreateTextureFromSurface(renderer, newName_s); + SDL_FreeSurface(newName_s); + + for (;;) { + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + SDL_RenderClear(renderer); + + SDL_Rect eSpace = {0, newpos * 40, 480, 40}; + SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255); + SDL_RenderFillRect(renderer, &eSpace); + + SDL_RenderCopy(renderer, list, NULL, NULL); + SDL_RenderCopy(renderer, newName, NULL, &rntSpace); + + 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_BACKSPACE) && (lp > 0)) { + hsname[--lp] = 0; + + SDL_Color fontColor = {0, 0, 0, 0}; + char name[26]; // 25 max characters in username plus the space at the + // beginning + sprintf(name, " %s", hsname); + newName_s = TTF_RenderText_Blended(loadFont(25), name, fontColor); + rntSpace.w = newName_s->w; + rntSpace.h = newName_s->h; + newName = SDL_CreateTextureFromSurface(renderer, newName_s); + SDL_FreeSurface(newName_s); + } else if ((e.key.keysym.sym == SDLK_RETURN) && (hsname[0] != 0)) { + lhl = new LocalHighscoreList(); + Highscore* h2 = new Highscore(hsname, level); + lhl->addHighscore(h2); + lhl->writeHighscores(); + + return new NewHighscoreState(h2); + } + } else if (e.type == SDL_TEXTINPUT) { + if (((*e.text.text & 0xFF80) == 0) && + (*e.text.text >= 32 && *e.text.text < 127) && (lp < 25)) { + hsname[lp++] = *e.text.text & 0x7f; + hsname[lp] = 0; + + SDL_Color fontColor = {0, 0, 0, 0}; + char name[26]; // 25 max characters in username plus the space at the + // beginning + sprintf(name, " %s", hsname); + newName_s = TTF_RenderText_Blended(loadFont(25), name, fontColor); + rntSpace.w = newName_s->w; + rntSpace.h = newName_s->h; + newName = SDL_CreateTextureFromSurface(renderer, newName_s); + SDL_FreeSurface(newName_s); + } + } + } + } } -State* NewHighscoreState::operator() (SDL_Window* window, SDL_Renderer* renderer) -{ - SDL_Texture* pointer = loadImage(renderer, "resources/pointer.bmp"); - - // Render highscore list - 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), "New Highscore!", 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_passartm.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); - - int selection = 0; - SDL_Event e; - - for (;;) - { - SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); - SDL_RenderClear(renderer); - - SDL_Rect eSpace = {0, h->getRank()*40, 480, 40}; - SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255); - SDL_RenderFillRect(renderer, &eSpace); - - SDL_RenderCopy(renderer, list, NULL, NULL); - applyTexture(renderer, pointer, selection==0?13:(selection==1?138:284), 448); - 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_LEFT) && (selection != 0)) - { - selection--; - } else if ((e.key.keysym.sym == SDLK_RIGHT) && (selection != 2)) - { - selection++; - } else if (e.key.keysym.sym == SDLK_RETURN) - { - switch (selection) - { - case 0: return new GameState(); - case 1: return new SubmitHighscoreState(h); - case 2: return new TitleState(); - } - } - } - } - } +NewHighscoreState::NewHighscoreState(Highscore* h) { this->h = h; } + +State* NewHighscoreState::operator()(SDL_Window* window, + SDL_Renderer* renderer) { + SDL_Texture* pointer = loadImage(renderer, "resources/pointer.bmp"); + + // Render highscore list + 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), "New Highscore!", 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_passartm.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); + + int selection = 0; + SDL_Event e; + + for (;;) { + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + SDL_RenderClear(renderer); + + SDL_Rect eSpace = {0, h->getRank() * 40, 480, 40}; + SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255); + SDL_RenderFillRect(renderer, &eSpace); + + SDL_RenderCopy(renderer, list, NULL, NULL); + applyTexture(renderer, pointer, + selection == 0 ? 13 : (selection == 1 ? 138 : 284), 448); + 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_LEFT) && (selection != 0)) { + selection--; + } else if ((e.key.keysym.sym == SDLK_RIGHT) && (selection != 2)) { + selection++; + } else if (e.key.keysym.sym == SDLK_RETURN) { + switch (selection) { + case 0: + return new GameState(); + case 1: + return new SubmitHighscoreState(h); + case 2: + return new TitleState(); + } + } + } + } + } } -SubmitHighscoreState::SubmitHighscoreState(Highscore* h) -{ - this->h = h; +SubmitHighscoreState::SubmitHighscoreState(Highscore* h) { this->h = h; } + +State* SubmitHighscoreState::operator()(SDL_Window* window, + SDL_Renderer* 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, "Sending highscore....", 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_Texture* list = SDL_CreateTextureFromSurface(renderer, list_s); + SDL_FreeSurface(list_s); + + // Start submitting score + m = SDL_CreateMutex(); + SDL_CreateThread(&SubmitHighscore, "SubmitHighscore", this); + + SDL_Event e; + + for (;;) { + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + SDL_RenderClear(renderer); + SDL_RenderCopy(renderer, list, NULL, NULL); + SDL_RenderPresent(renderer); + + if (SDL_LockMutex(m) == 0) { + if (lhl != NULL) { + SDL_UnlockMutex(m); + SDL_DestroyMutex(m); + + if (lhl->didFail()) { + return new FailedSubmittingHighscoreState(h); + } else { + return new SubmittedHighscoreState(lhl, h); + } + } else { + SDL_UnlockMutex(m); + } + } + + while (SDL_PollEvent(&e)) { + if (e.type == SDL_QUIT) { + SDL_DestroyMutex(m); + + return NULL; + } + } + } } -State* SubmitHighscoreState::operator() (SDL_Window* window, SDL_Renderer* 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, "Sending highscore....", 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_Texture* list = SDL_CreateTextureFromSurface(renderer, list_s); - SDL_FreeSurface(list_s); - - // Start submitting score - m = SDL_CreateMutex(); - SDL_CreateThread(&SubmitHighscore, "SubmitHighscore", this); - - SDL_Event e; - - for (;;) - { - SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); - SDL_RenderClear(renderer); - SDL_RenderCopy(renderer, list, NULL, NULL); - SDL_RenderPresent(renderer); - - if (SDL_LockMutex(m) == 0) - { - if (lhl != NULL) - { - SDL_UnlockMutex(m); - SDL_DestroyMutex(m); - - if (lhl->didFail()) - { - return new FailedSubmittingHighscoreState(h); - } else { - return new SubmittedHighscoreState(lhl, h); - } - } else { - SDL_UnlockMutex(m); - } - } - - while (SDL_PollEvent(&e)) - { - if (e.type == SDL_QUIT) - { - SDL_DestroyMutex(m); - - return NULL; - } - } - } -} +int SubmitHighscoreState::SubmitHighscore(void* pParam) { + SubmitHighscoreState* parent = (SubmitHighscoreState*)pParam; + if (SDL_LockMutex(parent->m) == 0) { + parent->lhl = new GlobalHighscoreList(parent->h); -int SubmitHighscoreState::SubmitHighscore(void* pParam) -{ - SubmitHighscoreState* parent = (SubmitHighscoreState*) pParam; - if (SDL_LockMutex(parent->m) == 0) - { - parent->lhl = new GlobalHighscoreList(parent->h); - - SDL_UnlockMutex(parent->m); - } else { - printf("Could not lock mutex: %s\n", SDL_GetError()); - } + SDL_UnlockMutex(parent->m); + } else { + printf("Could not lock mutex: %s\n", SDL_GetError()); + } } -FailedSubmittingHighscoreState::FailedSubmittingHighscoreState(Highscore* h) -{ - this->h = h; +FailedSubmittingHighscoreState::FailedSubmittingHighscoreState(Highscore* h) { + this->h = h; } -State* FailedSubmittingHighscoreState::operator() (SDL_Window* window, SDL_Renderer* 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, "Error submitting highscores", fontColor); - SDL_Rect tSpace = {240-(text->w/2), 240-(text->h/2), text->w, text->h}; - SDL_BlitSurface(text, NULL, list_s, &tSpace); - SDL_FreeSurface(text); - - SDL_Surface* title = TTF_RenderText_Blended(loadFont(40), "Highscore List", fontColor); - SDL_Rect aSpace = {240-(title->w/2), 0, title->w, title->h}; - SDL_BlitSurface(title, NULL, list_s, &aSpace); - SDL_FreeSurface(title); - - SDL_Surface* options_s = SDL_LoadBMP("resources/hlo_passartm.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_Texture* pointer = loadImage(renderer, "resources/pointer.bmp"); - int selection = 0; - SDL_Event e; - - for (;;) - { - SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); - SDL_RenderClear(renderer); - SDL_RenderCopy(renderer, list, NULL, NULL); - applyTexture(renderer, pointer, selection==0?13:(selection==1?138:284), 448); - 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_LEFT) && (selection != 0)) - { - selection--; - } else if ((e.key.keysym.sym == SDLK_RIGHT) && (selection != 2)) - { - selection++; - } else if (e.key.keysym.sym == SDLK_RETURN) - { - switch (selection) - { - case 0: return new GameState(); - case 1: return new SubmitHighscoreState(h); - case 2: return new TitleState(); - } - } - } - } - } +State* FailedSubmittingHighscoreState::operator()(SDL_Window* window, + SDL_Renderer* 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, "Error submitting highscores", fontColor); + SDL_Rect tSpace = {240 - (text->w / 2), 240 - (text->h / 2), text->w, + text->h}; + SDL_BlitSurface(text, NULL, list_s, &tSpace); + SDL_FreeSurface(text); + + SDL_Surface* title = + TTF_RenderText_Blended(loadFont(40), "Highscore List", fontColor); + SDL_Rect aSpace = {240 - (title->w / 2), 0, title->w, title->h}; + SDL_BlitSurface(title, NULL, list_s, &aSpace); + SDL_FreeSurface(title); + + SDL_Surface* options_s = SDL_LoadBMP("resources/hlo_passartm.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_Texture* pointer = loadImage(renderer, "resources/pointer.bmp"); + int selection = 0; + SDL_Event e; + + for (;;) { + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + SDL_RenderClear(renderer); + SDL_RenderCopy(renderer, list, NULL, NULL); + applyTexture(renderer, pointer, + selection == 0 ? 13 : (selection == 1 ? 138 : 284), 448); + 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_LEFT) && (selection != 0)) { + selection--; + } else if ((e.key.keysym.sym == SDLK_RIGHT) && (selection != 2)) { + selection++; + } else if (e.key.keysym.sym == SDLK_RETURN) { + switch (selection) { + case 0: + return new GameState(); + case 1: + return new SubmitHighscoreState(h); + case 2: + return new TitleState(); + } + } + } + } + } } -SubmittedHighscoreState::SubmittedHighscoreState(GlobalHighscoreList* lhl, Highscore* h) -{ - this->lhl = lhl; - this->h = h; +SubmittedHighscoreState::SubmittedHighscoreState(GlobalHighscoreList* lhl, + Highscore* h) { + this->lhl = lhl; + this->h = h; } -State* SubmittedHighscoreState::operator() (SDL_Window* window, SDL_Renderer* renderer) -{ - 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_paartm.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_Texture* pointer = loadImage(renderer, "resources/pointer.bmp"); - int selection = 0; - SDL_Event e; - - int newpos = h->getRank(); - if (newpos > 10) - { - newpos = 10; - } - - for (;;) - { - SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); - SDL_RenderClear(renderer); - - SDL_Rect eSpace = {0, newpos*40, 480, 40}; - SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255); - SDL_RenderFillRect(renderer, &eSpace); - - SDL_RenderCopy(renderer, list, NULL, NULL); - applyTexture(renderer, pointer, selection==0?52:225, 447); - 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_LEFT) && (selection != 0)) - { - selection--; - } else if ((e.key.keysym.sym == SDLK_RIGHT) && (selection != 1)) - { - selection++; - } else if (e.key.keysym.sym == SDLK_RETURN) - { - switch (selection) - { - case 0: return new GameState(); - case 1: return new TitleState(); - } - } - } - } - } +State* SubmittedHighscoreState::operator()(SDL_Window* window, + SDL_Renderer* renderer) { + 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_paartm.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_Texture* pointer = loadImage(renderer, "resources/pointer.bmp"); + int selection = 0; + SDL_Event e; + + int newpos = h->getRank(); + if (newpos > 10) { + newpos = 10; + } + + for (;;) { + SDL_SetRenderDrawColor(renderer, 255, 255, 255, 255); + SDL_RenderClear(renderer); + + SDL_Rect eSpace = {0, newpos * 40, 480, 40}; + SDL_SetRenderDrawColor(renderer, 255, 255, 0, 255); + SDL_RenderFillRect(renderer, &eSpace); + + SDL_RenderCopy(renderer, list, NULL, NULL); + applyTexture(renderer, pointer, selection == 0 ? 52 : 225, 447); + 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_LEFT) && (selection != 0)) { + selection--; + } else if ((e.key.keysym.sym == SDLK_RIGHT) && (selection != 1)) { + selection++; + } else if (e.key.keysym.sym == SDLK_RETURN) { + switch (selection) { + case 0: + return new GameState(); + case 1: + return new TitleState(); + } + } + } + } + } } diff --git a/hslist.h b/hslist.h index 291a526..efffa9d 100644 --- a/hslist.h +++ b/hslist.h @@ -1,5 +1,7 @@ #include + #include + #include "highscore.h" #include "state.h" @@ -10,120 +12,119 @@ typedef std::vector hslist_t; void resetRanks(hslist_t in); -class HighscoreList -{ - public: - SDL_Surface* render(); +class HighscoreList { + public: + SDL_Surface* render(); - protected: - hslist_t getLocalHighscores(); - hslist_t getGlobalHighscores(); + protected: + hslist_t getLocalHighscores(); + hslist_t getGlobalHighscores(); - hslist_t hslist; + hslist_t hslist; }; class LocalHighscoreList : public HighscoreList { - public: - LocalHighscoreList(); - int addHighscore(Highscore* h); - void writeHighscores(); + public: + LocalHighscoreList(); + int addHighscore(Highscore* h); + void writeHighscores(); }; class GlobalHighscoreList : public HighscoreList { - public: - GlobalHighscoreList(); - GlobalHighscoreList(Highscore* h); - SDL_Surface* render(); - bool didFail(); + public: + GlobalHighscoreList(); + GlobalHighscoreList(Highscore* h); + SDL_Surface* render(); + bool didFail(); - private: - typedef HighscoreList super; + private: + typedef HighscoreList super; - protected: - bool fail; + protected: + bool fail; }; class ChooseHighscoreListState : public State { - public: - State* operator() (SDL_Window* window, SDL_Renderer* renderer); + public: + State* operator()(SDL_Window* window, SDL_Renderer* renderer); }; class DisplayLocalHighscoreListState : public State { - public: - State* operator() (SDL_Window* window, SDL_Renderer* renderer); + public: + State* operator()(SDL_Window* window, SDL_Renderer* renderer); }; class DisplayAndReturnLocalHighscoreListState : public State { - public: - State* operator() (SDL_Window* window, SDL_Renderer* renderer); + public: + State* operator()(SDL_Window* window, SDL_Renderer* renderer); }; class DisplayGlobalHighscoreListState : public State { - public: - State* operator() (SDL_Window* window, SDL_Renderer* renderer); + public: + State* operator()(SDL_Window* window, SDL_Renderer* renderer); - protected: - SDL_Surface* list_s; - SDL_Texture* list; - GlobalHighscoreList* lhl; - SDL_mutex* m; + protected: + SDL_Surface* list_s; + SDL_Texture* list; + GlobalHighscoreList* lhl; + SDL_mutex* m; - private: - static int LoadHighscoreList(void* pParam); + private: + static int LoadHighscoreList(void* pParam); }; class EnterHighscoreState : public State { - public: - EnterHighscoreState(int level); - State* operator() (SDL_Window* window, SDL_Renderer* renderer); - - private: - int level; - int lp; - char* hsname; - SDL_Texture* newName; + public: + EnterHighscoreState(int level); + State* operator()(SDL_Window* window, SDL_Renderer* renderer); + + private: + int level; + int lp; + char* hsname; + SDL_Texture* newName; }; class NewHighscoreState : public State { - public: - NewHighscoreState(Highscore* h); - State* operator() (SDL_Window* window, SDL_Renderer* renderer); + public: + NewHighscoreState(Highscore* h); + State* operator()(SDL_Window* window, SDL_Renderer* renderer); - private: - Highscore* h; + private: + Highscore* h; }; class SubmitHighscoreState : public State { - public: - SubmitHighscoreState(Highscore* h); - State* operator() (SDL_Window* window, SDL_Renderer* renderer); + public: + SubmitHighscoreState(Highscore* h); + State* operator()(SDL_Window* window, SDL_Renderer* renderer); - protected: - Highscore* h; - SDL_mutex* m; - GlobalHighscoreList* lhl; + protected: + Highscore* h; + SDL_mutex* m; + GlobalHighscoreList* lhl; - private: - static int SubmitHighscore(void* pParam); + private: + static int SubmitHighscore(void* pParam); }; class FailedSubmittingHighscoreState : public State { - public: - FailedSubmittingHighscoreState(Highscore* h); - State* operator() (SDL_Window* window, SDL_Renderer* renderer); + public: + FailedSubmittingHighscoreState(Highscore* h); + State* operator()(SDL_Window* window, SDL_Renderer* renderer); - private: - Highscore* h; + private: + Highscore* h; }; class SubmittedHighscoreState : public State { - public: - SubmittedHighscoreState(GlobalHighscoreList* lhl, Highscore* h); - State* operator() (SDL_Window* window, SDL_Renderer* renderer); + public: + SubmittedHighscoreState(GlobalHighscoreList* lhl, Highscore* h); + State* operator()(SDL_Window* window, SDL_Renderer* renderer); - private: - GlobalHighscoreList* lhl; - Highscore* h; + private: + GlobalHighscoreList* lhl; + Highscore* h; }; #endif diff --git a/mazeoflife.cpp b/mazeoflife.cpp index cf21d2a..952fbdb 100644 --- a/mazeoflife.cpp +++ b/mazeoflife.cpp @@ -1,61 +1,59 @@ #include "mazeoflife.h" + #include -#include #include +#include + #include #include #include + #include "state.h" #include "titlestate.h" -int main(int argc, char *argv[]) -{ - srand(time(NULL)); - - if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) - { - printf("Could not initialize SDL: %s.\n", SDL_GetError()); - exit(-1); - } - - if (TTF_Init() == -1) - { - printf("Could not initialize SDL_ttf: %s.\n", TTF_GetError()); - exit(-1); - } - - if (SDLNet_Init() == -1) - { - printf("Cound not initalize SDL_net: %s.\n", SDLNet_GetError()); - exit(-1); - } - - SDL_Window* window = SDL_CreateWindow("Maze of Life", 100, 100, 480, 480, SDL_WINDOW_SHOWN); - if (window == NULL) - { - std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl; - return 1; - } - - SDL_Surface* icon = SDL_LoadBMP("resources/icon.bmp"); - SDL_SetWindowIcon(window, icon); - - SDL_Renderer* renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); - if (renderer == NULL) - { - std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl; - return 1; - } - - State* state = new TitleState(); - while (state != NULL) - { - state = (*state)(window, renderer); - } - - SDL_DestroyRenderer(renderer); - SDL_DestroyWindow(window); - SDLNet_Quit(); - TTF_Quit(); - SDL_Quit(); +int main(int argc, char* argv[]) { + srand(time(NULL)); + + if (SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER) == -1) { + printf("Could not initialize SDL: %s.\n", SDL_GetError()); + exit(-1); + } + + if (TTF_Init() == -1) { + printf("Could not initialize SDL_ttf: %s.\n", TTF_GetError()); + exit(-1); + } + + if (SDLNet_Init() == -1) { + printf("Cound not initalize SDL_net: %s.\n", SDLNet_GetError()); + exit(-1); + } + + SDL_Window* window = + SDL_CreateWindow("Maze of Life", 100, 100, 480, 480, SDL_WINDOW_SHOWN); + if (window == NULL) { + std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl; + return 1; + } + + SDL_Surface* icon = SDL_LoadBMP("resources/icon.bmp"); + SDL_SetWindowIcon(window, icon); + + SDL_Renderer* renderer = SDL_CreateRenderer( + window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC); + if (renderer == NULL) { + std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl; + return 1; + } + + State* state = new TitleState(); + while (state != NULL) { + state = (*state)(window, renderer); + } + + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + SDLNet_Quit(); + TTF_Quit(); + SDL_Quit(); } \ No newline at end of file diff --git a/state.h b/state.h index 5e8c4e7..0d50ab3 100644 --- a/state.h +++ b/state.h @@ -3,10 +3,11 @@ #ifndef STATE_H #define STATE_H -class State -{ - public: - virtual State* operator() (SDL_Window* window, SDL_Renderer* renderer) {return NULL;}; +class State { + public: + virtual State* operator()(SDL_Window* window, SDL_Renderer* renderer) { + return NULL; + }; }; #endif \ No newline at end of file diff --git a/titlestate.cpp b/titlestate.cpp index 014b138..166a55b 100644 --- a/titlestate.cpp +++ b/titlestate.cpp @@ -1,134 +1,116 @@ #include "titlestate.h" -#include "util.h" + #include "gamestate.h" #include "hslist.h" +#include "util.h" -State* TitleState::operator() (SDL_Window* window, SDL_Renderer* renderer) -{ - SDL_Texture* background = loadImage(renderer, "resources/title.bmp"); - SDL_Texture* pointer = loadImage(renderer, "resources/pointer.bmp"); - int selection = 0; - SDL_Event e; +State* TitleState::operator()(SDL_Window* window, SDL_Renderer* renderer) { + SDL_Texture* background = loadImage(renderer, "resources/title.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, 136, selection==0?316:(selection==1?350:(selection==2?381:417))); - SDL_RenderPresent(renderer); + for (;;) { + SDL_RenderClear(renderer); + SDL_RenderCopy(renderer, background, NULL, NULL); + applyTexture(renderer, pointer, 136, + selection == 0 + ? 316 + : (selection == 1 ? 350 : (selection == 2 ? 381 : 417))); + 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 != 3)) - { - selection++; - } else if (e.key.keysym.sym == SDLK_RETURN) - { - switch (selection) - { - case 0: return new GameState(); - case 1: return new HowToPlayState(); - case 2: return new ChooseHighscoreListState(); - case 3: return NULL; - } - } - } - } - } + 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 != 3)) { + selection++; + } else if (e.key.keysym.sym == SDLK_RETURN) { + switch (selection) { + case 0: + return new GameState(); + case 1: + return new HowToPlayState(); + case 2: + return new ChooseHighscoreListState(); + case 3: + return NULL; + } + } + } + } + } } -State* HowToPlayState::operator() (SDL_Window* window, SDL_Renderer* renderer) -{ - SDL_Texture* background = loadImage(renderer, "resources/htp1.bmp"); - SDL_Texture* pointer = loadImage(renderer, "resources/pointer.bmp"); - int selection = 0; - SDL_Event e; +State* HowToPlayState::operator()(SDL_Window* window, SDL_Renderer* renderer) { + SDL_Texture* background = loadImage(renderer, "resources/htp1.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, selection==0?74:216, 430); - SDL_RenderPresent(renderer); + for (;;) { + SDL_RenderClear(renderer); + SDL_RenderCopy(renderer, background, NULL, NULL); + applyTexture(renderer, pointer, selection == 0 ? 74 : 216, 430); + 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_LEFT) && (selection != 0)) - { - selection--; - } else if ((e.key.keysym.sym == SDLK_RIGHT) && (selection != 1)) - { - selection++; - } else if (e.key.keysym.sym == SDLK_RETURN) - { - switch (selection) - { - case 0: - return new HowToPlayPageTwoState(); + while (SDL_PollEvent(&e)) { + if (e.type == SDL_QUIT) { + return NULL; + } else if (e.type == SDL_KEYDOWN) { + if ((e.key.keysym.sym == SDLK_LEFT) && (selection != 0)) { + selection--; + } else if ((e.key.keysym.sym == SDLK_RIGHT) && (selection != 1)) { + selection++; + } else if (e.key.keysym.sym == SDLK_RETURN) { + switch (selection) { + case 0: + return new HowToPlayPageTwoState(); - break; - case 1: - return new TitleState(); - } - } - } - } - } + break; + case 1: + return new TitleState(); + } + } + } + } + } } -State* HowToPlayPageTwoState::operator() (SDL_Window* window, SDL_Renderer* renderer) -{ - SDL_Texture* background = loadImage(renderer, "resources/htp2.bmp"); - SDL_Texture* pointer = loadImage(renderer, "resources/pointer.bmp"); - int selection = 0; - SDL_Event e; +State* HowToPlayPageTwoState::operator()(SDL_Window* window, + SDL_Renderer* renderer) { + SDL_Texture* background = loadImage(renderer, "resources/htp2.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, selection==0?45:238, 430); - SDL_RenderPresent(renderer); + for (;;) { + SDL_RenderClear(renderer); + SDL_RenderCopy(renderer, background, NULL, NULL); + applyTexture(renderer, pointer, selection == 0 ? 45 : 238, 430); + 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_LEFT) && (selection != 0)) - { - selection--; - } else if ((e.key.keysym.sym == SDLK_RIGHT) && (selection != 1)) - { - selection++; - } else if (e.key.keysym.sym == SDLK_RETURN) - { - switch (selection) - { - case 0: - return new HowToPlayState(); + while (SDL_PollEvent(&e)) { + if (e.type == SDL_QUIT) { + return NULL; + } else if (e.type == SDL_KEYDOWN) { + if ((e.key.keysym.sym == SDLK_LEFT) && (selection != 0)) { + selection--; + } else if ((e.key.keysym.sym == SDLK_RIGHT) && (selection != 1)) { + selection++; + } else if (e.key.keysym.sym == SDLK_RETURN) { + switch (selection) { + case 0: + return new HowToPlayState(); - break; - case 1: - return new TitleState(); - } - } - } - } - } + break; + case 1: + return new TitleState(); + } + } + } + } + } } diff --git a/titlestate.h b/titlestate.h index 8e6ffbc..e989659 100644 --- a/titlestate.h +++ b/titlestate.h @@ -1,22 +1,23 @@ #include + #include "state.h" #ifndef TITLESTATE_H #define TITLESTATE_H class TitleState : public State { - public: - State* operator() (SDL_Window* window, SDL_Renderer* renderer); + public: + State* operator()(SDL_Window* window, SDL_Renderer* renderer); }; class HowToPlayState : public State { - public: - State* operator() (SDL_Window* window, SDL_Renderer* renderer); + public: + State* operator()(SDL_Window* window, SDL_Renderer* renderer); }; class HowToPlayPageTwoState : public State { - public: - State* operator() (SDL_Window* window, SDL_Renderer* renderer); + public: + State* operator()(SDL_Window* window, SDL_Renderer* renderer); }; #endif \ No newline at end of file diff --git a/util.cpp b/util.cpp index 9fcdbbd..693d0ad 100644 --- a/util.cpp +++ b/util.cpp @@ -1,71 +1,62 @@ #include "util.h" -#include "mazeoflife.h" + #include -void wrap(int& x, int& y) -{ - if (x < 0) - { - x = WIDTH+x; - } else if (x >= WIDTH) - { - x = x-WIDTH; - } +#include "mazeoflife.h" - if (y < 0) - { - y = HEIGHT+y; - } else if (y >= HEIGHT) - { - y = y-HEIGHT; - } +void wrap(int& x, int& y) { + if (x < 0) { + x = WIDTH + x; + } else if (x >= WIDTH) { + x = x - WIDTH; + } + + if (y < 0) { + y = HEIGHT + y; + } else if (y >= HEIGHT) { + y = y - HEIGHT; + } } -TTF_Font* loadFont(int size) -{ - TTF_Font* tmpfont = TTF_OpenFont("resources/mono.ttf", size); +TTF_Font* loadFont(int size) { + TTF_Font* tmpfont = TTF_OpenFont("resources/mono.ttf", size); - if (tmpfont == NULL) - { - printf("Unable to load font: %s\n", TTF_GetError()); - exit(1); - } + if (tmpfont == NULL) { + printf("Unable to load font: %s\n", TTF_GetError()); + exit(1); + } - return tmpfont; + return tmpfont; } -const char* getDataFile() -{ +const char* getDataFile() { #ifdef WINDOWS - char* dir = getenv("USERPROFILE"); + char* dir = getenv("USERPROFILE"); #else - char* dir = getenv("HOME"); + char* dir = getenv("HOME"); #endif - return (std::string(dir) + "/.molhslist").c_str(); + return (std::string(dir) + "/.molhslist").c_str(); } -SDL_Texture* loadImage(SDL_Renderer* renderer, std::string file) -{ - SDL_Surface* surface = SDL_LoadBMP(file.c_str()); - if (surface == NULL) - { - std::cout << SDL_GetError() << std::endl; - return NULL; - } +SDL_Texture* loadImage(SDL_Renderer* renderer, std::string file) { + SDL_Surface* surface = SDL_LoadBMP(file.c_str()); + if (surface == NULL) { + std::cout << SDL_GetError() << std::endl; + return NULL; + } - SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface); - SDL_FreeSurface(surface); + SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface); + SDL_FreeSurface(surface); - return texture; + return texture; } -void applyTexture(SDL_Renderer* renderer, SDL_Texture* tex, int x, int y) -{ - SDL_Rect pos; - pos.x = x; - pos.y = y; - SDL_QueryTexture(tex, NULL, NULL, &pos.w, &pos.h); +void applyTexture(SDL_Renderer* renderer, SDL_Texture* tex, int x, int y) { + SDL_Rect pos; + pos.x = x; + pos.y = y; + SDL_QueryTexture(tex, NULL, NULL, &pos.w, &pos.h); - SDL_RenderCopy(renderer, tex, NULL, &pos); + SDL_RenderCopy(renderer, tex, NULL, &pos); } \ No newline at end of file diff --git a/util.h b/util.h index 0e1fa2e..836f065 100644 --- a/util.h +++ b/util.h @@ -1,5 +1,6 @@ #include #include + #include #ifndef UTIL_H -- cgit 1.4.1