From 763bd80603dbace9f14c25309159ed07ec6e9a93 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sat, 30 Jan 2021 11:50:30 -0500 Subject: Added sprite Y sorting --- CMakeLists.txt | 1 + src/game.cpp | 20 ++++++++++++++++++++ src/game.h | 27 ++++++++++++++++++++------- src/party.cpp | 10 +++++----- src/renderer.cpp | 2 +- 5 files changed, 47 insertions(+), 13 deletions(-) create mode 100644 src/game.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 3b46e02..4bbc297 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -30,6 +30,7 @@ add_executable(tanetane src/renderer.cpp src/sprite.cpp src/party.cpp + src/game.cpp ) set_property(TARGET tanetane PROPERTY CXX_STANDARD 17) diff --git a/src/game.cpp b/src/game.cpp new file mode 100644 index 0000000..ccca10f --- /dev/null +++ b/src/game.cpp @@ -0,0 +1,20 @@ +#include "game.h" + +int Game::addSprite(Sprite sprite) { + int id = sprites_.size(); + sprites_.push_back(std::move(sprite)); + spritesByY_.emplace(sprite.loc().y(), id); + return id; +} + +void Game::moveSprite(int id, vec2i newLoc) { + Sprite& sprite = sprites_.at(id); + bool changedY = (sprite.loc().y() != newLoc.y()); + if (changedY) { + spritesByY_.erase(std::make_tuple(sprite.loc().y(), id)); + } + sprite.loc() = newLoc; + if (changedY) { + spritesByY_.emplace(newLoc.y(), id); + } +} \ No newline at end of file diff --git a/src/game.h b/src/game.h index bb99543..abb70ce 100644 --- a/src/game.h +++ b/src/game.h @@ -1,6 +1,8 @@ #ifndef GAME_H_E6F1396E #define GAME_H_E6F1396E +#include +#include #include #include "sprite.h" @@ -14,23 +16,34 @@ struct Input { class Game { public: - int addSprite(Sprite sprite) { - int id = sprites_.size(); - sprites_.push_back(std::move(sprite)); - return id; - } + int addSprite(Sprite sprite); - Sprite& getSprite(int id) { + const Sprite& getSprite(int id) const { return sprites_.at(id); } - std::vector& getSprites() { + const std::vector& getSprites() { return sprites_; } + auto spriteView() const { + return ranges::views::transform([&] (int id) -> const Sprite& { + return sprites_.at(id); + }); + } + + auto getSpritesByY() const { + return spritesByY_ | ranges::views::transform([] (const std::tuple& val) { + return std::get<1>(val); + }); + } + + void moveSprite(int id, vec2i newLoc); + private: std::vector sprites_; + std::set> spritesByY_; }; #endif /* end of include guard: GAME_H_E6F1396E */ diff --git a/src/party.cpp b/src/party.cpp index 6c003b8..1ff4bb4 100644 --- a/src/party.cpp +++ b/src/party.cpp @@ -8,7 +8,7 @@ void Party::addMember(Game& game, int spriteId) { newMember.spriteId = spriteId; if (index > 0) { - Sprite& sprite = game.getSprite(spriteId); + const Sprite& sprite = game.getSprite(spriteId); for (int i = 0; i < PARTY_FRAME_DELAY * index; i++) { newMember.nextPosition.push_back(sprite.loc()); @@ -23,7 +23,7 @@ void Party::move(Game& game, const Input& keystate) { return; } - Sprite& p1 = game.getSprite(members_[0].spriteId); + const Sprite& p1 = game.getSprite(members_[0].spriteId); vec2i pLoc = p1.loc(); if (keystate.up) @@ -47,12 +47,12 @@ void Party::move(Game& game, const Input& keystate) { } if (keystate.up || keystate.down || keystate.left || keystate.right) { - p1.loc() = pLoc; + game.moveSprite(members_[0].spriteId, pLoc); for (int i = 1; i < members_.size(); i++) { - Sprite& pNext = game.getSprite(members_[i].spriteId); + const Sprite& pNext = game.getSprite(members_[i].spriteId); members_[i].nextPosition.push_back(pLoc); - pNext.loc() = members_[i].nextPosition.front(); + game.moveSprite(members_[i].spriteId, members_[i].nextPosition.front()); members_[i].nextPosition.pop_front(); } } diff --git a/src/renderer.cpp b/src/renderer.cpp index bcc3e95..8053de0 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -48,7 +48,7 @@ void Renderer::render(Game& game) { SDL_SetRenderDrawColor(ren_.get(), 255, 255, 255, 255); SDL_RenderClear(ren_.get()); - for (Sprite& sprite : game.getSprites()) { + for (const Sprite& sprite : game.getSpritesByY() | game.spriteView()) { SDL_Rect dest { sprite.loc().x(), sprite.loc().y(), sprite.size().w(), sprite.size().h() }; SDL_RenderCopy(ren_.get(), textures_.at(sprite.getTextureId()).get(), nullptr, &dest); } -- cgit 1.4.1