From ad8ae988ba1928af5063ca1dc7375356e467979b Mon Sep 17 00:00:00 2001 From: Charles Date: Sat, 21 Mar 2020 13:22:38 +0100 Subject: Sprites loading and display, player move --- Makefile | 2 +- README.md | 3 +- include/game.hpp | 23 +++++++------ include/graphics.hpp | 8 +++++ sprite/caisse.jpg | Bin 9011 -> 0 bytes sprite/caisse_ok.jpg | Bin 8978 -> 0 bytes sprite/crate.jpg | Bin 0 -> 9011 bytes sprite/crate_ok.jpg | Bin 0 -> 8978 bytes sprite/mario_bas.gif | Bin 1062 -> 0 bytes sprite/mario_down.gif | Bin 0 -> 1062 bytes sprite/mario_droite.gif | Bin 1037 -> 0 bytes sprite/mario_gauche.gif | Bin 1035 -> 0 bytes sprite/mario_haut.gif | Bin 1035 -> 0 bytes sprite/mario_left.gif | Bin 0 -> 1035 bytes sprite/mario_right.gif | Bin 0 -> 1037 bytes sprite/mario_up.gif | Bin 0 -> 1035 bytes sprite/mur.jpg | Bin 9333 -> 0 bytes sprite/objectif.png | Bin 1315 -> 0 bytes sprite/payload.png | Bin 0 -> 1315 bytes sprite/wall.jpg | Bin 0 -> 9333 bytes src/game.cpp | 34 ++++++++++++++++--- src/graphics.cpp | 85 +++++++++++++++++++++++++++++++++++++++++------- src/main.cpp | 14 ++++---- 23 files changed, 134 insertions(+), 35 deletions(-) delete mode 100644 sprite/caisse.jpg delete mode 100644 sprite/caisse_ok.jpg create mode 100644 sprite/crate.jpg create mode 100644 sprite/crate_ok.jpg delete mode 100644 sprite/mario_bas.gif create mode 100644 sprite/mario_down.gif delete mode 100644 sprite/mario_droite.gif delete mode 100644 sprite/mario_gauche.gif delete mode 100644 sprite/mario_haut.gif create mode 100644 sprite/mario_left.gif create mode 100644 sprite/mario_right.gif create mode 100644 sprite/mario_up.gif delete mode 100644 sprite/mur.jpg delete mode 100644 sprite/objectif.png create mode 100644 sprite/payload.png create mode 100644 sprite/wall.jpg 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 #include #include +#include #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/caisse.jpg deleted file mode 100644 index 18e3bf3..0000000 Binary files a/sprite/caisse.jpg and /dev/null differ diff --git a/sprite/caisse_ok.jpg b/sprite/caisse_ok.jpg deleted file mode 100644 index 966a292..0000000 Binary files a/sprite/caisse_ok.jpg and /dev/null differ diff --git a/sprite/crate.jpg b/sprite/crate.jpg new file mode 100644 index 0000000..18e3bf3 Binary files /dev/null and b/sprite/crate.jpg differ diff --git a/sprite/crate_ok.jpg b/sprite/crate_ok.jpg new file mode 100644 index 0000000..966a292 Binary files /dev/null and b/sprite/crate_ok.jpg differ diff --git a/sprite/mario_bas.gif b/sprite/mario_bas.gif deleted file mode 100644 index 12f759a..0000000 Binary files a/sprite/mario_bas.gif and /dev/null differ diff --git a/sprite/mario_down.gif b/sprite/mario_down.gif new file mode 100644 index 0000000..12f759a Binary files /dev/null and b/sprite/mario_down.gif differ diff --git a/sprite/mario_droite.gif b/sprite/mario_droite.gif deleted file mode 100644 index dbfcb7b..0000000 Binary files a/sprite/mario_droite.gif and /dev/null differ diff --git a/sprite/mario_gauche.gif b/sprite/mario_gauche.gif deleted file mode 100644 index 369522c..0000000 Binary files a/sprite/mario_gauche.gif and /dev/null differ diff --git a/sprite/mario_haut.gif b/sprite/mario_haut.gif deleted file mode 100644 index f70a02f..0000000 Binary files a/sprite/mario_haut.gif and /dev/null differ diff --git a/sprite/mario_left.gif b/sprite/mario_left.gif new file mode 100644 index 0000000..369522c Binary files /dev/null and b/sprite/mario_left.gif differ diff --git a/sprite/mario_right.gif b/sprite/mario_right.gif new file mode 100644 index 0000000..dbfcb7b Binary files /dev/null and b/sprite/mario_right.gif differ diff --git a/sprite/mario_up.gif b/sprite/mario_up.gif new file mode 100644 index 0000000..f70a02f Binary files /dev/null and b/sprite/mario_up.gif differ diff --git a/sprite/mur.jpg b/sprite/mur.jpg deleted file mode 100644 index 37d0dbe..0000000 Binary files a/sprite/mur.jpg and /dev/null differ diff --git a/sprite/objectif.png b/sprite/objectif.png deleted file mode 100644 index 82287f6..0000000 Binary files a/sprite/objectif.png and /dev/null differ diff --git a/sprite/payload.png b/sprite/payload.png new file mode 100644 index 0000000..82287f6 Binary files /dev/null and b/sprite/payload.png differ diff --git a/sprite/wall.jpg b/sprite/wall.jpg new file mode 100644 index 0000000..37d0dbe Binary files /dev/null and b/sprite/wall.jpg 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 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); -- cgit