diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-03-21 13:22:38 +0100 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-03-21 13:22:38 +0100 |
| commit | ad8ae988ba1928af5063ca1dc7375356e467979b (patch) | |
| tree | 27a6738ff0172e4ebdaed7174cd7b47cc3ec9d59 /src/graphics.cpp | |
| parent | 88f5eb3447073fc6a87786da6a9b6b788f9bde66 (diff) | |
| download | mario_sokoban-ad8ae988ba1928af5063ca1dc7375356e467979b.tar.gz mario_sokoban-ad8ae988ba1928af5063ca1dc7375356e467979b.tar.bz2 mario_sokoban-ad8ae988ba1928af5063ca1dc7375356e467979b.zip | |
Sprites loading and display, player move
Diffstat (limited to 'src/graphics.cpp')
| -rw-r--r-- | src/graphics.cpp | 85 |
1 files changed, 73 insertions, 12 deletions
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; |
