summary refs log tree commit diff stats
path: root/src/renderer.cpp
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2021-02-13 20:50:21 -0500
committerKelly Rauchenberger <fefferburbia@gmail.com>2021-02-13 20:50:21 -0500
commit470b1d43fb6f8e17624ee90f87270de5bd6ff77e (patch)
tree966f1e18d496f12c3273684be299573e4bf443ec /src/renderer.cpp
parented933607765a6e010689aaaf85184053ff6e8a2b (diff)
downloadtanetane-470b1d43fb6f8e17624ee90f87270de5bd6ff77e.tar.gz
tanetane-470b1d43fb6f8e17624ee90f87270de5bd6ff77e.tar.bz2
tanetane-470b1d43fb6f8e17624ee90f87270de5bd6ff77e.zip
Added lightning sprite to mailbox event
Sprites can be destroyed now, which really just means that their index is removed from the list of active sprites. The actual memory is not freed until all sprites are deleted on map change.

Sprite layers are introduced. All sprites by default are on layer 0 (Normal) which renders them in between the lower and upper map layers. There is also a layer 1 (Above) that renders above the upper map layer, and the sprite for the lightning strike uses this layer in order to not be hidden by the trees.

Fixed a bug where waiting for the end of a non-looping animation would trip the flag immediately upon the final frame activating, instead of waiting the length of the last frame.
Diffstat (limited to 'src/renderer.cpp')
-rw-r--r--src/renderer.cpp34
1 files changed, 21 insertions, 13 deletions
diff --git a/src/renderer.cpp b/src/renderer.cpp index a7169e9..0e5f8f1 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp
@@ -94,6 +94,21 @@ texture_ptr Renderer::renderMapLayer(const Map& map, int layer) {
94 return canvas; 94 return canvas;
95} 95}
96 96
97void Renderer::renderSprite(const Sprite& sprite) {
98 if (sprite.isAnimated) {
99 if (sprite.hasShadow) {
100 int shadowTexId = loadImageFromFile("../res/shadow.png");
101 const SDL_Rect shadowDest { sprite.loc.x() - 8, sprite.loc.y() - 8, 16, 8 };
102 SDL_RenderCopy(ren_.get(), textures_.at(shadowTexId).get(), nullptr, &shadowDest);
103 }
104
105 const SpriteFrame& frame = sprite.frames.at(sprite.animations.at(sprite.animationId).frameIndices.at(sprite.animationFrame));
106 const SDL_Rect& src = frame.srcRect;
107 SDL_Rect dest { sprite.loc.x() - frame.center.x(), sprite.loc.y() - frame.center.y(), frame.size.w(), frame.size.h() };
108 SDL_RenderCopy(ren_.get(), textures_.at(loadImageFromFile(sprite.spritesheet)).get(), &src, &dest);
109 }
110}
111
97void Renderer::render(Game& game) { 112void Renderer::render(Game& game) {
98 if (cachedMapName_ != game.getMap().getName()) { 113 if (cachedMapName_ != game.getMap().getName()) {
99 cachedMapName_ = game.getMap().getName(); 114 cachedMapName_ = game.getMap().getName();
@@ -122,23 +137,16 @@ void Renderer::render(Game& game) {
122 137
123 SDL_RenderCopy(ren_.get(), renLay1_.get(), nullptr, nullptr); 138 SDL_RenderCopy(ren_.get(), renLay1_.get(), nullptr, nullptr);
124 139
125 int shadowTexId = loadImageFromFile("../res/shadow.png"); 140 for (const Sprite& sprite : game.getSystem<TransformSystem>().getSpritesByY(SpriteLayer::Normal) | game.spriteView()) {
126 for (const Sprite& sprite : game.getSystem<TransformSystem>().getSpritesByY() | game.spriteView()) { 141 renderSprite(sprite);
127 if (sprite.isAnimated) {
128 if (sprite.hasShadow) {
129 const SDL_Rect shadowDest { sprite.loc.x() - 8, sprite.loc.y() - 8, 16, 8 };
130 SDL_RenderCopy(ren_.get(), textures_.at(shadowTexId).get(), nullptr, &shadowDest);
131 }
132
133 const SpriteFrame& frame = sprite.frames.at(sprite.animations.at(sprite.animationId).frameIndices.at(sprite.animationFrame));
134 const SDL_Rect& src = frame.srcRect;
135 SDL_Rect dest { sprite.loc.x() - frame.center.x(), sprite.loc.y() - frame.center.y(), frame.size.w(), frame.size.h() };
136 SDL_RenderCopy(ren_.get(), textures_.at(loadImageFromFile(sprite.spritesheet)).get(), &src, &dest);
137 }
138 } 142 }
139 143
140 SDL_RenderCopy(ren_.get(), renLay0_.get(), nullptr, nullptr); 144 SDL_RenderCopy(ren_.get(), renLay0_.get(), nullptr, nullptr);
141 145
146 for (const Sprite& sprite : game.getSystem<TransformSystem>().getSpritesByY(SpriteLayer::Above) | game.spriteView()) {
147 renderSprite(sprite);
148 }
149
142 SDL_Rect cameraField { 150 SDL_Rect cameraField {
143 game.getSystem<CameraSystem>().getCameraPosition().x(), 151 game.getSystem<CameraSystem>().getCameraPosition().x(),
144 game.getSystem<CameraSystem>().getCameraPosition().y(), 152 game.getSystem<CameraSystem>().getCameraPosition().y(),