diff options
| -rw-r--r-- | Makefile | 2 | ||||
| -rw-r--r-- | README.md | 15 | ||||
| -rw-r--r-- | include/game.hpp | 37 | ||||
| -rw-r--r-- | include/graphics.hpp | 5 | ||||
| -rw-r--r-- | sprite/caisse.jpg | bin | 0 -> 9011 bytes | |||
| -rw-r--r-- | sprite/caisse_ok.jpg | bin | 0 -> 8978 bytes | |||
| -rw-r--r-- | sprite/mario_bas.gif | bin | 0 -> 1062 bytes | |||
| -rw-r--r-- | sprite/mario_droite.gif | bin | 0 -> 1037 bytes | |||
| -rw-r--r-- | sprite/mario_gauche.gif | bin | 0 -> 1035 bytes | |||
| -rw-r--r-- | sprite/mario_haut.gif | bin | 0 -> 1035 bytes | |||
| -rw-r--r-- | sprite/menu.jpg | bin | 0 -> 24864 bytes | |||
| -rw-r--r-- | sprite/mur.jpg | bin | 0 -> 9333 bytes | |||
| -rw-r--r-- | sprite/objectif.png | bin | 0 -> 1315 bytes | |||
| -rw-r--r-- | src/game.cpp | 66 | ||||
| -rw-r--r-- | src/graphics.cpp | 24 | ||||
| -rw-r--r-- | src/main.cpp | 13 |
16 files changed, 155 insertions, 7 deletions
@@ -9,7 +9,7 @@ OBJDIR = build CXX = g++ CXXFLAGS = -Wall -Wextra -I$(INCDIR) $(shell sdl2-config --cflags) -std=c++98 -LDFLAGS = $(shell sdl2-config --libs) -lSDL2_ttf +LDFLAGS = $(shell sdl2-config --libs) SRC = $(shell find $(SRCDIR) -type f -name "*.cpp") INC = $(shell find $(INCDIR) -type f -name "*.h" -o -name "*.hpp") diff --git a/README.md b/README.md new file mode 100644 index 0000000..dbe29dd --- /dev/null +++ b/README.md @@ -0,0 +1,15 @@ +# mario sokoban + +Mario sokoban projec2048t of [openclassrooms](https://openclassrooms.com/fr/courses/19980-apprenez-a-programmer-en-c/18709-tp-mario-sokoban) +(in C++ instead of C because I can't be bothered). + +# Usage + +``` +make all +./mario_sokoban +``` + +# Dependencies + +- [SDL2](https://libsdl.org) diff --git a/include/game.hpp b/include/game.hpp index 017d21b..fcdd2c1 100644 --- a/include/game.hpp +++ b/include/game.hpp @@ -1,11 +1,46 @@ #ifndef GAME_HPP # define GAME_HPP +#include <string> +#include <algorithm> + class Game { public: - Game(); + Game(std::string fmt); + ~Game(); + + enum Direction + { + DirectionUp, + DirectionDown, + DirectionLeft, + DirectionRight, + }; + + enum Cell + { + CellEmpty = 0, + CellWall, + CellCrate, + CellPayload, + }; + + Cell get(int y, int x); + size_t getHeight(); + size_t getWidth(); + +private: + + struct Position + { + int y, x; + }; + size_t m_width; + size_t m_height; + Cell **m_grid; + Position m_playerPos; }; #endif diff --git a/include/graphics.hpp b/include/graphics.hpp index ef0c813..d51b47a 100644 --- a/include/graphics.hpp +++ b/include/graphics.hpp @@ -8,14 +8,14 @@ class Graphics { - public: +public: Graphics(Game &game, std::string title, int width, int height); ~Graphics(); void update(); bool isRunning() const; - private: +private: bool m_running; Game &m_game; std::string m_title; @@ -25,6 +25,7 @@ class Graphics SDL_Window *m_window; void drawGame(); + void drawCell(Game::Cell cell, int y, int x); void handleEvent(); void error() const; }; diff --git a/sprite/caisse.jpg b/sprite/caisse.jpg Binary files differnew file mode 100644 index 0000000..18e3bf3 --- /dev/null +++ b/sprite/caisse.jpg diff --git a/sprite/caisse_ok.jpg b/sprite/caisse_ok.jpg Binary files differnew file mode 100644 index 0000000..966a292 --- /dev/null +++ b/sprite/caisse_ok.jpg diff --git a/sprite/mario_bas.gif b/sprite/mario_bas.gif Binary files differnew file mode 100644 index 0000000..12f759a --- /dev/null +++ b/sprite/mario_bas.gif diff --git a/sprite/mario_droite.gif b/sprite/mario_droite.gif Binary files differnew file mode 100644 index 0000000..dbfcb7b --- /dev/null +++ b/sprite/mario_droite.gif diff --git a/sprite/mario_gauche.gif b/sprite/mario_gauche.gif Binary files differnew file mode 100644 index 0000000..369522c --- /dev/null +++ b/sprite/mario_gauche.gif diff --git a/sprite/mario_haut.gif b/sprite/mario_haut.gif Binary files differnew file mode 100644 index 0000000..f70a02f --- /dev/null +++ b/sprite/mario_haut.gif diff --git a/sprite/menu.jpg b/sprite/menu.jpg Binary files differnew file mode 100644 index 0000000..a1bec8d --- /dev/null +++ b/sprite/menu.jpg diff --git a/sprite/mur.jpg b/sprite/mur.jpg Binary files differnew file mode 100644 index 0000000..37d0dbe --- /dev/null +++ b/sprite/mur.jpg diff --git a/sprite/objectif.png b/sprite/objectif.png Binary files differnew file mode 100644 index 0000000..82287f6 --- /dev/null +++ b/sprite/objectif.png 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(); |
