diff options
author | Kelly Rauchenberger <fefferburbia@gmail.com> | 2021-01-30 11:50:30 -0500 |
---|---|---|
committer | Kelly Rauchenberger <fefferburbia@gmail.com> | 2021-01-30 11:50:30 -0500 |
commit | 763bd80603dbace9f14c25309159ed07ec6e9a93 (patch) | |
tree | 8597c1ca9246ead9fae04e74e028665e3e406e9b /src | |
parent | de0ac2927230ce44fb92f362bfa7e4b4cb29bdd1 (diff) | |
download | tanetane-763bd80603dbace9f14c25309159ed07ec6e9a93.tar.gz tanetane-763bd80603dbace9f14c25309159ed07ec6e9a93.tar.bz2 tanetane-763bd80603dbace9f14c25309159ed07ec6e9a93.zip |
Added sprite Y sorting
Diffstat (limited to 'src')
-rw-r--r-- | src/game.cpp | 20 | ||||
-rw-r--r-- | src/game.h | 27 | ||||
-rw-r--r-- | src/party.cpp | 10 | ||||
-rw-r--r-- | src/renderer.cpp | 2 |
4 files changed, 46 insertions, 13 deletions
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 @@ | |||
1 | #include "game.h" | ||
2 | |||
3 | int Game::addSprite(Sprite sprite) { | ||
4 | int id = sprites_.size(); | ||
5 | sprites_.push_back(std::move(sprite)); | ||
6 | spritesByY_.emplace(sprite.loc().y(), id); | ||
7 | return id; | ||
8 | } | ||
9 | |||
10 | void Game::moveSprite(int id, vec2i newLoc) { | ||
11 | Sprite& sprite = sprites_.at(id); | ||
12 | bool changedY = (sprite.loc().y() != newLoc.y()); | ||
13 | if (changedY) { | ||
14 | spritesByY_.erase(std::make_tuple(sprite.loc().y(), id)); | ||
15 | } | ||
16 | sprite.loc() = newLoc; | ||
17 | if (changedY) { | ||
18 | spritesByY_.emplace(newLoc.y(), id); | ||
19 | } | ||
20 | } \ 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 @@ | |||
1 | #ifndef GAME_H_E6F1396E | 1 | #ifndef GAME_H_E6F1396E |
2 | #define GAME_H_E6F1396E | 2 | #define GAME_H_E6F1396E |
3 | 3 | ||
4 | #include <set> | ||
5 | #include <range/v3/all.hpp> | ||
4 | #include <vector> | 6 | #include <vector> |
5 | #include "sprite.h" | 7 | #include "sprite.h" |
6 | 8 | ||
@@ -14,23 +16,34 @@ struct Input { | |||
14 | class Game { | 16 | class Game { |
15 | public: | 17 | public: |
16 | 18 | ||
17 | int addSprite(Sprite sprite) { | 19 | int addSprite(Sprite sprite); |
18 | int id = sprites_.size(); | ||
19 | sprites_.push_back(std::move(sprite)); | ||
20 | return id; | ||
21 | } | ||
22 | 20 | ||
23 | Sprite& getSprite(int id) { | 21 | const Sprite& getSprite(int id) const { |
24 | return sprites_.at(id); | 22 | return sprites_.at(id); |
25 | } | 23 | } |
26 | 24 | ||
27 | std::vector<Sprite>& getSprites() { | 25 | const std::vector<Sprite>& getSprites() { |
28 | return sprites_; | 26 | return sprites_; |
29 | } | 27 | } |
30 | 28 | ||
29 | auto spriteView() const { | ||
30 | return ranges::views::transform([&] (int id) -> const Sprite& { | ||
31 | return sprites_.at(id); | ||
32 | }); | ||
33 | } | ||
34 | |||
35 | auto getSpritesByY() const { | ||
36 | return spritesByY_ | ranges::views::transform([] (const std::tuple<int, int>& val) { | ||
37 | return std::get<1>(val); | ||
38 | }); | ||
39 | } | ||
40 | |||
41 | void moveSprite(int id, vec2i newLoc); | ||
42 | |||
31 | private: | 43 | private: |
32 | 44 | ||
33 | std::vector<Sprite> sprites_; | 45 | std::vector<Sprite> sprites_; |
46 | std::set<std::tuple<int, int>> spritesByY_; | ||
34 | }; | 47 | }; |
35 | 48 | ||
36 | #endif /* end of include guard: GAME_H_E6F1396E */ | 49 | #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) { | |||
8 | newMember.spriteId = spriteId; | 8 | newMember.spriteId = spriteId; |
9 | 9 | ||
10 | if (index > 0) { | 10 | if (index > 0) { |
11 | Sprite& sprite = game.getSprite(spriteId); | 11 | const Sprite& sprite = game.getSprite(spriteId); |
12 | 12 | ||
13 | for (int i = 0; i < PARTY_FRAME_DELAY * index; i++) { | 13 | for (int i = 0; i < PARTY_FRAME_DELAY * index; i++) { |
14 | newMember.nextPosition.push_back(sprite.loc()); | 14 | newMember.nextPosition.push_back(sprite.loc()); |
@@ -23,7 +23,7 @@ void Party::move(Game& game, const Input& keystate) { | |||
23 | return; | 23 | return; |
24 | } | 24 | } |
25 | 25 | ||
26 | Sprite& p1 = game.getSprite(members_[0].spriteId); | 26 | const Sprite& p1 = game.getSprite(members_[0].spriteId); |
27 | vec2i pLoc = p1.loc(); | 27 | vec2i pLoc = p1.loc(); |
28 | 28 | ||
29 | if (keystate.up) | 29 | if (keystate.up) |
@@ -47,12 +47,12 @@ void Party::move(Game& game, const Input& keystate) { | |||
47 | } | 47 | } |
48 | 48 | ||
49 | if (keystate.up || keystate.down || keystate.left || keystate.right) { | 49 | if (keystate.up || keystate.down || keystate.left || keystate.right) { |
50 | p1.loc() = pLoc; | 50 | game.moveSprite(members_[0].spriteId, pLoc); |
51 | 51 | ||
52 | for (int i = 1; i < members_.size(); i++) { | 52 | for (int i = 1; i < members_.size(); i++) { |
53 | Sprite& pNext = game.getSprite(members_[i].spriteId); | 53 | const Sprite& pNext = game.getSprite(members_[i].spriteId); |
54 | members_[i].nextPosition.push_back(pLoc); | 54 | members_[i].nextPosition.push_back(pLoc); |
55 | pNext.loc() = members_[i].nextPosition.front(); | 55 | game.moveSprite(members_[i].spriteId, members_[i].nextPosition.front()); |
56 | members_[i].nextPosition.pop_front(); | 56 | members_[i].nextPosition.pop_front(); |
57 | } | 57 | } |
58 | } | 58 | } |
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) { | |||
48 | SDL_SetRenderDrawColor(ren_.get(), 255, 255, 255, 255); | 48 | SDL_SetRenderDrawColor(ren_.get(), 255, 255, 255, 255); |
49 | SDL_RenderClear(ren_.get()); | 49 | SDL_RenderClear(ren_.get()); |
50 | 50 | ||
51 | for (Sprite& sprite : game.getSprites()) { | 51 | for (const Sprite& sprite : game.getSpritesByY() | game.spriteView()) { |
52 | SDL_Rect dest { sprite.loc().x(), sprite.loc().y(), sprite.size().w(), sprite.size().h() }; | 52 | SDL_Rect dest { sprite.loc().x(), sprite.loc().y(), sprite.size().w(), sprite.size().h() }; |
53 | SDL_RenderCopy(ren_.get(), textures_.at(sprite.getTextureId()).get(), nullptr, &dest); | 53 | SDL_RenderCopy(ren_.get(), textures_.at(sprite.getTextureId()).get(), nullptr, &dest); |
54 | } | 54 | } |