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 --- res/scripts/common.lua | 9 +++++++++ src/renderer.cpp | 8 ++++++-- src/script_system.cpp | 3 ++- src/sprite.h | 1 + 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/res/scripts/common.lua b/res/scripts/common.lua index 1d4879f..e355a32 100644 --- a/res/scripts/common.lua +++ b/res/scripts/common.lua @@ -414,6 +414,15 @@ function SetAnimationSlowdown(spriteName, amount) sprite.animSlowdown = amount end +--- Sets the opacity of the sprite when it is rendered. +-- @param spriteName the alias of the sprite to modify +-- @param amount a value from 0.0 to 1.0 +function SetOpacity(spriteName, amount) + local spriteId = getSpriteByAlias(spriteName) + local sprite = getSprite(spriteId) + sprite.opacity = amount +end + --- Sets the enclosure zone for a sprite. -- The sprite will be prevented from exiting the area defined by that zone. function AddEnclosureZone(spriteName, zone) 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