From b628c80f988c1fb250b94b17dca8e082bc4c1d0b Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Tue, 2 Feb 2021 16:51:22 -0500 Subject: Added moving camera --- src/game.cpp | 24 +++++++++++++++++++++++- src/game.h | 21 +++++++++++++++++++++ src/main.cpp | 6 ++++++ src/renderer.cpp | 27 ++++++++++++++------------- src/vector.h | 2 ++ 5 files changed, 66 insertions(+), 14 deletions(-) diff --git a/src/game.cpp b/src/game.cpp index ccca10f..ebb792e 100644 --- a/src/game.cpp +++ b/src/game.cpp @@ -17,4 +17,26 @@ void Game::moveSprite(int id, vec2i newLoc) { if (changedY) { spritesByY_.emplace(newLoc.y(), id); } -} \ No newline at end of file +} + +void Game::tick() { + if (!cameraLocked_) { + const Sprite& follow = getSprite(followingSprite_); + vec2i mapBounds = map_->getMapSize() * map_->getTileSize(); + + cameraPos_ = follow.loc() - (cameraFov_ / 2); + + if (cameraPos_.x() < 0) { + cameraPos_.x() = 0; + } + if (cameraPos_.y() < 0) { + cameraPos_.y() = 0; + } + if (cameraPos_.x() + cameraFov_.w() >= mapBounds.w()) { + cameraPos_.x() = mapBounds.w() - cameraFov_.w() - 1; + } + if (cameraPos_.y() + cameraFov_.h() >= mapBounds.h()) { + cameraPos_.y() = mapBounds.h() - cameraFov_.h() - 1; + } + } +} diff --git a/src/game.h b/src/game.h index 2c1680a..a717d53 100644 --- a/src/game.h +++ b/src/game.h @@ -7,6 +7,7 @@ #include #include "sprite.h" #include "map.h" +#include "consts.h" struct Input { bool left = false; @@ -60,11 +61,31 @@ public: const Map& getMap() const { return *map_; } + // Camera + + const vec2i& getCameraPosition() const { return cameraPos_; } + + const vec2i& getFieldOfView() const { return cameraFov_; } + + void setFollowingSprite(int spriteId) { followingSprite_ = spriteId; } + + void lockCamera() { cameraLocked_ = true; } + + void unlockCamera() { cameraLocked_ = false; } + + void tick(); + private: std::vector sprites_; std::set> spritesByY_; std::unique_ptr map_; + + // Camera + vec2i cameraPos_; + vec2i cameraFov_ { CANVAS_WIDTH, CANVAS_HEIGHT }; + int followingSprite_; + bool cameraLocked_ = true; }; #endif /* end of include guard: GAME_H_E6F1396E */ diff --git a/src/main.cpp b/src/main.cpp index 587ae15..b3aee1a 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -26,6 +26,10 @@ void loop(Renderer& renderer) { party.addMember(game, dusterSprite); party.addMember(game, boneySprite); + game.setFollowingSprite(lucasSprite); + game.unlockCamera(); + game.tick(); + renderer.render(game); Timer inputTimer(50); @@ -65,6 +69,8 @@ void loop(Renderer& renderer) { game.tickSpriteAnim(); } + game.tick(); + renderer.render(game); } } diff --git a/src/renderer.cpp b/src/renderer.cpp index 9d20fee..03d5173 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -88,13 +88,15 @@ void Renderer::render(Game& game) { renLay0_ = renderMapLayer(game.getMap(), 1); } + vec2i mapBounds = game.getMap().getMapSize() * game.getMap().getTileSize(); + texture_ptr canvas( SDL_CreateTexture( ren_.get(), SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, - CANVAS_WIDTH, - CANVAS_HEIGHT)); + mapBounds.w(), + mapBounds.h())); if (!canvas) { throw sdl_error(); @@ -105,12 +107,7 @@ void Renderer::render(Game& game) { SDL_SetRenderDrawColor(ren_.get(), 255, 255, 255, 255); SDL_RenderClear(ren_.get()); - vec2i mapBounds = game.getMap().getMapSize() * game.getMap().getTileSize(); - - { - SDL_Rect dest { -80, -80, mapBounds.w(), mapBounds.h() }; - SDL_RenderCopy(ren_.get(), renLay1_.get(), nullptr, &dest); - } + SDL_RenderCopy(ren_.get(), renLay1_.get(), nullptr, nullptr); for (const Sprite& sprite : game.getSpritesByY() | game.spriteView()) { const SpriteFrame& frame = sprite.getFrame(); @@ -119,13 +116,17 @@ void Renderer::render(Game& game) { SDL_RenderCopy(ren_.get(), textures_.at(sprite.getTextureId()).get(), &src, &dest); } - { - SDL_Rect dest { -80, -80, mapBounds.w(), mapBounds.h() }; - SDL_RenderCopy(ren_.get(), renLay0_.get(), nullptr, &dest); - } + SDL_RenderCopy(ren_.get(), renLay0_.get(), nullptr, nullptr); + + SDL_Rect cameraField { + game.getCameraPosition().x(), + game.getCameraPosition().y(), + game.getFieldOfView().w(), + game.getFieldOfView().h() + }; SDL_SetRenderTarget(ren_.get(), nullptr); - SDL_RenderCopy(ren_.get(), canvas.get(), nullptr, nullptr); + SDL_RenderCopy(ren_.get(), canvas.get(), &cameraField, nullptr); SDL_RenderPresent(ren_.get()); } diff --git a/src/vector.h b/src/vector.h index d54fdbb..8019edf 100644 --- a/src/vector.h +++ b/src/vector.h @@ -1,6 +1,8 @@ #ifndef VECTOR_H_5458ED71 #define VECTOR_H_5458ED71 +#include + template class vec2 { public: -- cgit 1.4.1