aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Makefile2
-rw-r--r--README.md3
-rw-r--r--include/game.hpp23
-rw-r--r--include/graphics.hpp8
-rw-r--r--sprite/crate.jpg (renamed from sprite/caisse.jpg)bin9011 -> 9011 bytes
-rw-r--r--sprite/crate_ok.jpg (renamed from sprite/caisse_ok.jpg)bin8978 -> 8978 bytes
-rw-r--r--sprite/mario_down.gif (renamed from sprite/mario_bas.gif)bin1062 -> 1062 bytes
-rw-r--r--sprite/mario_left.gif (renamed from sprite/mario_gauche.gif)bin1035 -> 1035 bytes
-rw-r--r--sprite/mario_right.gif (renamed from sprite/mario_droite.gif)bin1037 -> 1037 bytes
-rw-r--r--sprite/mario_up.gif (renamed from sprite/mario_haut.gif)bin1035 -> 1035 bytes
-rw-r--r--sprite/payload.png (renamed from sprite/objectif.png)bin1315 -> 1315 bytes
-rw-r--r--sprite/wall.jpg (renamed from sprite/mur.jpg)bin9333 -> 9333 bytes
-rw-r--r--src/game.cpp34
-rw-r--r--src/graphics.cpp85
-rw-r--r--src/main.cpp14
15 files changed, 134 insertions, 35 deletions
diff --git a/Makefile b/Makefile
index 97f282a..0f715ee 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)
+LDFLAGS = $(shell sdl2-config --libs) -lSDL2_image
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
index dbe29dd..99861a7 100644
--- a/README.md
+++ b/README.md
@@ -1,6 +1,6 @@
# mario sokoban
-Mario sokoban projec2048t of [openclassrooms](https://openclassrooms.com/fr/courses/19980-apprenez-a-programmer-en-c/18709-tp-mario-sokoban)
+Mario sokoban project 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
@@ -13,3 +13,4 @@ make all
# Dependencies
- [SDL2](https://libsdl.org)
+- [SDL_Image](https://www.libsdl.org/projects/SDL_image)
diff --git a/include/game.hpp b/include/game.hpp
index fcdd2c1..40ca113 100644
--- a/include/game.hpp
+++ b/include/game.hpp
@@ -26,21 +26,24 @@ public:
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;
+ void move(Direction direction);
+ Cell get(int y, int x) const;
+ size_t getHeight() const;
+ size_t getWidth() const;
+ Position const &getPlayer() const;
+
+private:
+
+ size_t m_width;
+ size_t m_height;
+ Cell **m_grid;
+ Position m_playerPos;
+ Direction m_playerDirection;
};
#endif
diff --git a/include/graphics.hpp b/include/graphics.hpp
index d51b47a..54f7d35 100644
--- a/include/graphics.hpp
+++ b/include/graphics.hpp
@@ -4,6 +4,7 @@
#include <iostream>
#include <string>
#include <SDL2/SDL.h>
+#include <SDL2/SDL_image.h>
#include "game.hpp"
class Graphics
@@ -23,10 +24,17 @@ private:
int m_height;
SDL_Renderer *m_renderer;
SDL_Window *m_window;
+ SDL_Texture *m_wallTex;
+ SDL_Texture *m_crateTex;
+ SDL_Texture *m_payloadTex;
+ SDL_Texture *m_playerTex;
void drawGame();
void drawCell(Game::Cell cell, int y, int x);
+ void drawPlayer();
void handleEvent();
+ SDL_Texture *loadImage(std::string filename);
+ void putImage(SDL_Texture *tex, SDL_Rect *destRect);
void error() const;
};
diff --git a/sprite/caisse.jpg b/sprite/crate.jpg
index 18e3bf3..18e3bf3 100644
--- a/sprite/caisse.jpg
+++ b/sprite/crate.jpg
Binary files differ
diff --git a/sprite/caisse_ok.jpg b/sprite/crate_ok.jpg
index 966a292..966a292 100644
--- a/sprite/caisse_ok.jpg
+++ b/sprite/crate_ok.jpg
Binary files differ
diff --git a/sprite/mario_bas.gif b/sprite/mario_down.gif
index 12f759a..12f759a 100644
--- a/sprite/mario_bas.gif
+++ b/sprite/mario_down.gif
Binary files differ
diff --git a/sprite/mario_gauche.gif b/sprite/mario_left.gif
index 369522c..369522c 100644
--- a/sprite/mario_gauche.gif
+++ b/sprite/mario_left.gif
Binary files differ
diff --git a/sprite/mario_droite.gif b/sprite/mario_right.gif
index dbfcb7b..dbfcb7b 100644
--- a/sprite/mario_droite.gif
+++ b/sprite/mario_right.gif
Binary files differ
diff --git a/sprite/mario_haut.gif b/sprite/mario_up.gif
index f70a02f..f70a02f 100644
--- a/sprite/mario_haut.gif
+++ b/sprite/mario_up.gif
Binary files differ
diff --git a/sprite/objectif.png b/sprite/payload.png
index 82287f6..82287f6 100644
--- a/sprite/objectif.png
+++ b/sprite/payload.png
Binary files differ
diff --git a/sprite/mur.jpg b/sprite/wall.jpg
index 37d0dbe..37d0dbe 100644
--- a/sprite/mur.jpg
+++ b/sprite/wall.jpg
Binary files differ
diff --git a/src/game.cpp b/src/game.cpp
index 3e94883..88ff394 100644
--- a/src/game.cpp
+++ b/src/game.cpp
@@ -6,8 +6,8 @@
#define PAYLOAD_CHAR '*'
#define MARIO_CHAR 'm'
-#include <iostream>
Game::Game(std::string fmt)
+ : m_playerDirection(DirectionDown)
{
size_t p;
@@ -35,10 +35,12 @@ Game::Game(std::string fmt)
m_grid[i][j] = CellPayload;
break;
case MARIO_CHAR:
+ m_grid[i][j] = CellEmpty;
m_playerPos.y = i;
m_playerPos.x = j;
break;
default:
+ m_grid[i][j] = CellEmpty;
exit(1);
}
}
@@ -53,17 +55,41 @@ Game::~Game()
delete []m_grid;
}
-Game::Cell Game::get(int y, int x)
+void Game::move(Direction direction)
+{
+ m_playerDirection = direction;
+ switch (direction)
+ {
+ case DirectionUp:
+ m_playerPos.y--;
+ break;
+ case DirectionDown:
+ m_playerPos.y++;
+ break;
+ case DirectionLeft:
+ m_playerPos.x--;
+ break;
+ case DirectionRight:
+ m_playerPos.x++;
+ }
+}
+
+Game::Cell Game::get(int y, int x) const
{
return m_grid[y][x];
}
-size_t Game::getHeight()
+size_t Game::getHeight() const
{
return m_height;
}
-size_t Game::getWidth()
+size_t Game::getWidth() const
{
return m_width;
}
+
+Game::Position const &Game::getPlayer() const
+{
+ return m_playerPos;
+}
diff --git a/src/graphics.cpp b/src/graphics.cpp
index 5846ab7..734fd12 100644
--- a/src/graphics.cpp
+++ b/src/graphics.cpp
@@ -7,14 +7,22 @@ Graphics::Graphics(Game &game, std::string title, int width, int height):
{
if (SDL_Init(SDL_INIT_VIDEO) < 0)
error();
- if ((m_window = SDL_CreateWindow(m_title.c_str(), 0, 0, m_width, m_height, 0)) == NULL)
+ if ((m_window = SDL_CreateWindow(m_title.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, m_width, m_height, 0)) == NULL)
error();
if ((m_renderer = SDL_CreateRenderer(m_window, -1, 0)) == NULL)
error();
+ m_wallTex = loadImage("sprite/wall.jpg");
+ m_crateTex = loadImage("sprite/crate.jpg");
+ m_payloadTex = loadImage("sprite/payload.png");
+ m_playerTex = loadImage("sprite/mario_down.gif");
}
Graphics::~Graphics()
{
+ SDL_DestroyTexture(m_playerTex);
+ SDL_DestroyTexture(m_payloadTex);
+ SDL_DestroyTexture(m_crateTex);
+ SDL_DestroyTexture(m_wallTex);
SDL_DestroyRenderer(m_renderer);
SDL_DestroyWindow(m_window);
SDL_Quit();
@@ -40,28 +48,52 @@ 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);
+ drawPlayer();
}
void Graphics::drawCell(Game::Cell cell, int y, int x)
{
SDL_Rect r;
+ 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();
+
switch (cell)
{
+ case Game::CellEmpty:
+ SDL_SetRenderDrawColor(m_renderer, 100, 100, 100, 255);
+ SDL_RenderFillRect(m_renderer, &r);
+ break;
case Game::CellWall:
- SDL_SetRenderDrawColor(m_renderer, 40, 40, 100, 255);
+ putImage(m_wallTex, &r);
break;
- case Game::CellEmpty:
- SDL_SetRenderDrawColor(m_renderer, 10, 10, 10, 255);
+ case Game::CellCrate:
+ putImage(m_crateTex, &r);
+ break;
+ case Game::CellPayload:
+ SDL_SetRenderDrawColor(m_renderer, 100, 100, 100, 255);
+ SDL_RenderFillRect(m_renderer, &r);
+ putImage(m_payloadTex, &r);
break;
default:
- SDL_SetRenderDrawColor(m_renderer, 150, 150, 150, 255);
+ break;
+ // SDL_SetRenderDrawColor(m_renderer, 150, 150, 150, 255);
}
- r.x = x * (m_width / m_game.getWidth());
- r.y = y * (m_height / m_game.getHeight());
+}
+
+void Graphics::drawPlayer()
+{
+ SDL_Rect r;
+ Game::Position pos = m_game.getPlayer();
+
+ r.x = pos.x * (m_width / m_game.getWidth());
+ r.y = pos.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);
+ putImage(m_playerTex, &r);
+
}
void Graphics::handleEvent()
@@ -78,19 +110,48 @@ void Graphics::handleEvent()
case SDL_KEYDOWN:
switch (e.key.keysym.sym)
{
- case SDLK_LEFT:
- break;
- case SDLK_RIGHT:
+ case SDLK_UP:
+ m_game.move(Game::DirectionUp);
break;
case SDLK_DOWN:
+ m_game.move(Game::DirectionDown);
break;
- case SDLK_UP:
+ case SDLK_LEFT:
+ m_game.move(Game::DirectionLeft);
+ break;
+ case SDLK_RIGHT:
+ m_game.move(Game::DirectionRight);
break;
}
}
}
}
+SDL_Texture *Graphics::loadImage(std::string filename)
+{
+ SDL_Surface *tmpSurface;
+ SDL_Texture *tex;
+
+ tmpSurface = IMG_Load(filename.c_str());
+ if (tmpSurface == NULL)
+ error();
+ tex = SDL_CreateTextureFromSurface(m_renderer, tmpSurface);
+ if (tex == NULL)
+ error();
+ SDL_FreeSurface(tmpSurface);
+ return tex;
+}
+
+void Graphics::putImage(SDL_Texture *tex, SDL_Rect *destRect)
+{
+ SDL_Rect srcRect;
+
+ srcRect.x = 0;
+ srcRect.y = 0;
+ SDL_QueryTexture(tex, NULL, NULL, &srcRect.w, &srcRect.h);
+ SDL_RenderCopy(m_renderer, tex, &srcRect, destRect);
+}
+
void Graphics::error() const
{
std::cerr << SDL_GetError() << std::endl;
diff --git a/src/main.cpp b/src/main.cpp
index 3a52cee..0bdf404 100644
--- a/src/main.cpp
+++ b/src/main.cpp
@@ -4,13 +4,13 @@
int main()
{
Game game(
- "##########\n"
- "# m #\n"
- "# * #\n"
- "# #\n"
- "# U #\n"
- "# #\n"
- "##########\n"
+ "#######\n"
+ "# m #\n"
+ "#* #\n"
+ "# #\n"
+ "# U #\n"
+ "# #\n"
+ "#######\n"
);
Graphics graphics(game, "mario sokoban", 500, 500);