From 470b1d43fb6f8e17624ee90f87270de5bd6ff77e Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sat, 13 Feb 2021 20:50:21 -0500 Subject: 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. --- src/renderer.cpp | 34 +++++++++++++++++++++------------- 1 file changed, 21 insertions(+), 13 deletions(-) (limited to 'src/renderer.cpp') 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) { return canvas; } +void Renderer::renderSprite(const Sprite& sprite) { + if (sprite.isAnimated) { + if (sprite.hasShadow) { + int shadowTexId = loadImageFromFile("../res/shadow.png"); + const SDL_Rect shadowDest { sprite.loc.x() - 8, sprite.loc.y() - 8, 16, 8 }; + SDL_RenderCopy(ren_.get(), textures_.at(shadowTexId).get(), nullptr, &shadowDest); + } + + const SpriteFrame& frame = sprite.frames.at(sprite.animations.at(sprite.animationId).frameIndices.at(sprite.animationFrame)); + const SDL_Rect& src = frame.srcRect; + SDL_Rect dest { sprite.loc.x() - frame.center.x(), sprite.loc.y() - frame.center.y(), frame.size.w(), frame.size.h() }; + SDL_RenderCopy(ren_.get(), textures_.at(loadImageFromFile(sprite.spritesheet)).get(), &src, &dest); + } +} + void Renderer::render(Game& game) { if (cachedMapName_ != game.getMap().getName()) { cachedMapName_ = game.getMap().getName(); @@ -122,23 +137,16 @@ void Renderer::render(Game& game) { SDL_RenderCopy(ren_.get(), renLay1_.get(), nullptr, nullptr); - int shadowTexId = loadImageFromFile("../res/shadow.png"); - for (const Sprite& sprite : game.getSystem().getSpritesByY() | game.spriteView()) { - if (sprite.isAnimated) { - if (sprite.hasShadow) { - const SDL_Rect shadowDest { sprite.loc.x() - 8, sprite.loc.y() - 8, 16, 8 }; - SDL_RenderCopy(ren_.get(), textures_.at(shadowTexId).get(), nullptr, &shadowDest); - } - - const SpriteFrame& frame = sprite.frames.at(sprite.animations.at(sprite.animationId).frameIndices.at(sprite.animationFrame)); - const SDL_Rect& src = frame.srcRect; - SDL_Rect dest { sprite.loc.x() - frame.center.x(), sprite.loc.y() - frame.center.y(), frame.size.w(), frame.size.h() }; - SDL_RenderCopy(ren_.get(), textures_.at(loadImageFromFile(sprite.spritesheet)).get(), &src, &dest); - } + for (const Sprite& sprite : game.getSystem().getSpritesByY(SpriteLayer::Normal) | game.spriteView()) { + renderSprite(sprite); } SDL_RenderCopy(ren_.get(), renLay0_.get(), nullptr, nullptr); + for (const Sprite& sprite : game.getSystem().getSpritesByY(SpriteLayer::Above) | game.spriteView()) { + renderSprite(sprite); + } + SDL_Rect cameraField { game.getSystem().getCameraPosition().x(), game.getSystem().getCameraPosition().y(), -- cgit 1.4.1