diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-03-21 03:56:19 +0100 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-03-21 03:56:19 +0100 |
| commit | 88f5eb3447073fc6a87786da6a9b6b788f9bde66 (patch) | |
| tree | 2fb30f59e539b08cb53c4b7d7336238acc4ca1ed /src | |
| parent | 166c06083212a5657fcaf03328bf530f9eb8b0d8 (diff) | |
| download | mario_sokoban-88f5eb3447073fc6a87786da6a9b6b788f9bde66.tar.gz mario_sokoban-88f5eb3447073fc6a87786da6a9b6b788f9bde66.tar.bz2 mario_sokoban-88f5eb3447073fc6a87786da6a9b6b788f9bde66.zip | |
Added basic game init with string and game display
Diffstat (limited to 'src')
| -rw-r--r-- | src/game.cpp | 66 | ||||
| -rw-r--r-- | src/graphics.cpp | 24 | ||||
| -rw-r--r-- | src/main.cpp | 13 |
3 files changed, 100 insertions, 3 deletions
diff --git a/src/game.cpp b/src/game.cpp index 4e25d8a..3e94883 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -1,5 +1,69 @@ #include "game.hpp" -Game::Game() +#define EMPTY_CHAR ' ' +#define WALL_CHAR '#' +#define CRATE_CHAR 'U' +#define PAYLOAD_CHAR '*' +#define MARIO_CHAR 'm' + +#include <iostream> +Game::Game(std::string fmt) +{ + size_t p; + + m_height = std::count(fmt.begin(), fmt.end(), '\n'); + m_grid = new Cell*[m_height]; + m_width = fmt.find("\n"); + for (int i = 0; (p = fmt.find("\n")) != std::string::npos; i++) + { + std::string token = fmt.substr(0, p); + m_grid[i] = new Cell[m_width]; + for (size_t j = 0; j < token.size(); j++) + { + switch (token[j]) + { + case EMPTY_CHAR: + m_grid[i][j] = CellEmpty; + break; + case WALL_CHAR: + m_grid[i][j] = CellWall; + break; + case CRATE_CHAR: + m_grid[i][j] = CellCrate; + break; + case PAYLOAD_CHAR: + m_grid[i][j] = CellPayload; + break; + case MARIO_CHAR: + m_playerPos.y = i; + m_playerPos.x = j; + break; + default: + exit(1); + } + } + fmt.erase(0, p + 1); + } +} + +Game::~Game() +{ + for (size_t i = 0; i < m_height; i++) + delete []m_grid[i]; + delete []m_grid; +} + +Game::Cell Game::get(int y, int x) +{ + return m_grid[y][x]; +} + +size_t Game::getHeight() +{ + return m_height; +} + +size_t Game::getWidth() { + return m_width; } diff --git a/src/graphics.cpp b/src/graphics.cpp index c434bcc..5846ab7 100644 --- a/src/graphics.cpp +++ b/src/graphics.cpp @@ -37,7 +37,31 @@ void Graphics::update() void Graphics::drawGame() { + for (size_t i = 0; i < m_game.getHeight(); i++) + for (size_t j = 0; j < m_game.getWidth(); j++) + drawCell(m_game.get(i, j), i, j); +} + +void Graphics::drawCell(Game::Cell cell, int y, int x) +{ + SDL_Rect r; + switch (cell) + { + case Game::CellWall: + SDL_SetRenderDrawColor(m_renderer, 40, 40, 100, 255); + break; + case Game::CellEmpty: + SDL_SetRenderDrawColor(m_renderer, 10, 10, 10, 255); + break; + default: + SDL_SetRenderDrawColor(m_renderer, 150, 150, 150, 255); + } + r.x = x * (m_width / m_game.getWidth()); + r.y = y * (m_height / m_game.getHeight()); + r.w = m_width / m_game.getWidth(); + r.h = m_height / m_game.getHeight(); + SDL_RenderFillRect(m_renderer, &r); } void Graphics::handleEvent() diff --git a/src/main.cpp b/src/main.cpp index 8960149..3a52cee 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -3,8 +3,17 @@ int main() { - Game game; - Graphics graphics(game, "mario sokoban", 640, 480); + Game game( + "##########\n" + "# m #\n" + "# * #\n" + "# #\n" + "# U #\n" + "# #\n" + "##########\n" + ); + + Graphics graphics(game, "mario sokoban", 500, 500); while (graphics.isRunning()) graphics.update(); |
