From 132899d222f2c7d3542655b2332ed3574ffa9737 Mon Sep 17 00:00:00 2001 From: Star Rauchenberger Date: Mon, 5 Jul 2021 10:07:32 -0400 Subject: Added sprite opacity --- src/renderer.cpp | 8 ++++++-- src/script_system.cpp | 3 ++- src/sprite.h | 1 + 3 files changed, 9 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/renderer.cpp b/src/renderer.cpp index b28e3cb..25c5669 100644 --- a/src/renderer.cpp +++ b/src/renderer.cpp @@ -112,7 +112,9 @@ void Renderer::renderSprite(const Sprite& sprite) { 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); + SDL_Texture* shadowTex = textures_.at(shadowTexId).get(); + SDL_SetTextureAlphaMod(shadowTex, sprite.opacity * 255); + SDL_RenderCopy(ren_.get(), shadowTex, nullptr, &shadowDest); } const SpriteFrame& frame = sprite.frames.at(sprite.animations.at(sprite.animationId).frameIndices.at(sprite.animationFrame)); @@ -121,7 +123,9 @@ void Renderer::renderSprite(const Sprite& sprite) { if (sprite.bobbing) { dest.y -= sprite.bobAmount; } - SDL_RenderCopy(ren_.get(), textures_.at(loadImageFromFile(sprite.spritesheet)).get(), &src, &dest); + SDL_Texture* textureToRender = textures_.at(loadImageFromFile(sprite.spritesheet)).get(); + SDL_SetTextureAlphaMod(textureToRender, sprite.opacity * 255); + SDL_RenderCopy(ren_.get(), textureToRender, &src, &dest); } } diff --git a/src/script_system.cpp b/src/script_system.cpp index 7889148..56a6012 100644 --- a/src/script_system.cpp +++ b/src/script_system.cpp @@ -55,7 +55,8 @@ ScriptSystem::ScriptSystem(Game& game) : game_(game) { "solid", &Sprite::solid, "behaviourType", &Sprite::behaviourType, "followSpriteId", &Sprite::followSpriteId, - "interactionScript", &Sprite::interactionScript); + "interactionScript", &Sprite::interactionScript, + "opacity", &Sprite::opacity); engine_.new_usertype( "message", diff --git a/src/sprite.h b/src/sprite.h index 413c20d..782ae14 100644 --- a/src/sprite.h +++ b/src/sprite.h @@ -100,6 +100,7 @@ public: int bobAmount = 0; bool bobbingDown = false; int animSlowdownProgress = 0; + double opacity = 1.0; // Animation (controls) bool normallyHasShadow = false; -- cgit 1.4.1