aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorCharles <sircharlesaze@gmail.com>2020-03-21 03:56:19 +0100
committerCharles <sircharlesaze@gmail.com>2020-03-21 03:56:19 +0100
commit88f5eb3447073fc6a87786da6a9b6b788f9bde66 (patch)
tree2fb30f59e539b08cb53c4b7d7336238acc4ca1ed
parent166c06083212a5657fcaf03328bf530f9eb8b0d8 (diff)
downloadmario_sokoban-88f5eb3447073fc6a87786da6a9b6b788f9bde66.tar.gz
mario_sokoban-88f5eb3447073fc6a87786da6a9b6b788f9bde66.tar.bz2
mario_sokoban-88f5eb3447073fc6a87786da6a9b6b788f9bde66.zip
Added basic game init with string and game display
-rw-r--r--Makefile2
-rw-r--r--README.md15
-rw-r--r--include/game.hpp37
-rw-r--r--include/graphics.hpp5
-rw-r--r--sprite/caisse.jpgbin0 -> 9011 bytes
-rw-r--r--sprite/caisse_ok.jpgbin0 -> 8978 bytes
-rw-r--r--sprite/mario_bas.gifbin0 -> 1062 bytes
-rw-r--r--sprite/mario_droite.gifbin0 -> 1037 bytes
-rw-r--r--sprite/mario_gauche.gifbin0 -> 1035 bytes
-rw-r--r--sprite/mario_haut.gifbin0 -> 1035 bytes
-rw-r--r--sprite/menu.jpgbin0 -> 24864 bytes
-rw-r--r--sprite/mur.jpgbin0 -> 9333 bytes
-rw-r--r--sprite/objectif.pngbin0 -> 1315 bytes
-rw-r--r--src/game.cpp66
-rw-r--r--src/graphics.cpp24
-rw-r--r--src/main.cpp13
16 files changed, 155 insertions, 7 deletions
diff --git a/Makefile b/Makefile
index b06a6c8..97f282a 100644
--- a/Makefile
+++ b/Makefile
@@ -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
new file mode 100644
index 0000000..18e3bf3
--- /dev/null
+++ b/sprite/caisse.jpg
Binary files differ
diff --git a/sprite/caisse_ok.jpg b/sprite/caisse_ok.jpg
new file mode 100644
index 0000000..966a292
--- /dev/null
+++ b/sprite/caisse_ok.jpg
Binary files differ
diff --git a/sprite/mario_bas.gif b/sprite/mario_bas.gif
new file mode 100644
index 0000000..12f759a
--- /dev/null
+++ b/sprite/mario_bas.gif
Binary files differ
diff --git a/sprite/mario_droite.gif b/sprite/mario_droite.gif
new file mode 100644
index 0000000..dbfcb7b
--- /dev/null
+++ b/sprite/mario_droite.gif
Binary files differ
diff --git a/sprite/mario_gauche.gif b/sprite/mario_gauche.gif
new file mode 100644
index 0000000..369522c
--- /dev/null
+++ b/sprite/mario_gauche.gif
Binary files differ
diff --git a/sprite/mario_haut.gif b/sprite/mario_haut.gif
new file mode 100644
index 0000000..f70a02f
--- /dev/null
+++ b/sprite/mario_haut.gif
Binary files differ
diff --git a/sprite/menu.jpg b/sprite/menu.jpg
new file mode 100644
index 0000000..a1bec8d
--- /dev/null
+++ b/sprite/menu.jpg
Binary files differ
diff --git a/sprite/mur.jpg b/sprite/mur.jpg
new file mode 100644
index 0000000..37d0dbe
--- /dev/null
+++ b/sprite/mur.jpg
Binary files differ
diff --git a/sprite/objectif.png b/sprite/objectif.png
new file mode 100644
index 0000000..82287f6
--- /dev/null
+++ b/sprite/objectif.png
Binary files differ
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();