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 --- src/game.cpp | 34 ++++++++++++++++++++--- src/graphics.cpp | 85 ++++++++++++++++++++++++++++++++++++++++++++++++-------- src/main.cpp | 14 +++++----- 3 files changed, 110 insertions(+), 23 deletions(-) (limited to 'src') 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