From c6b441cc1f430e2d9fd4861788741f1680efc8d3 Mon Sep 17 00:00:00 2001 From: Daniel Stevens Date: Wed, 5 Mar 2025 22:26:56 -0700 Subject: [PATCH 1/3] Use elapsed time to determine fade complete Using the time values is perhaps easier to read and understand. Additionally, the change makes it easier to then use `scaleLinear` for the main calculation. There is maybe a slight change in behavior here, regarding rounding errors in determining when a fade is complete. With the change we wait the full elapsed time before declaring the fade complete. Previously, once the output was rounded to max intensity, the fade was considered complete, even if it hadn't quite yet reached the full fade time. Arguably it was more efficient to stop early, once there was no more calculation to do, though if something else was waiting on the fade to complete, it could have triggered early if the calculation finished early. With the update, everything should finish at the specified time. --- NAS2D/Renderer/Fade.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/NAS2D/Renderer/Fade.cpp b/NAS2D/Renderer/Fade.cpp index f455b088..b3f2902d 100644 --- a/NAS2D/Renderer/Fade.cpp +++ b/NAS2D/Renderer/Fade.cpp @@ -78,10 +78,11 @@ void Fade::update() return; } - const auto step = static_cast(std::clamp(mFadeTimer.elapsedTicks() * 255u / mDuration.milliseconds, 0u, 255u)); + const auto currentMilliseconds = mFadeTimer.elapsedTicks(); + const auto step = static_cast(std::clamp(currentMilliseconds * 255u / mDuration.milliseconds, 0u, 255u)); mFadeColor.alpha = (mDirection == FadeDirection::In) ? 255 - step : step; - if (step == 255) + if (currentMilliseconds >= mDuration.milliseconds) { mDirection = FadeDirection::None; if (!mOnFadeComplete.empty()) From aa5366af5e3ff597480f53289d95af9760edeb1b Mon Sep 17 00:00:00 2001 From: Daniel Stevens Date: Wed, 5 Mar 2025 22:39:12 -0700 Subject: [PATCH 2/3] Clamp input time duration rather than output alpha value --- NAS2D/Renderer/Fade.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/NAS2D/Renderer/Fade.cpp b/NAS2D/Renderer/Fade.cpp index b3f2902d..bcb4c819 100644 --- a/NAS2D/Renderer/Fade.cpp +++ b/NAS2D/Renderer/Fade.cpp @@ -78,8 +78,8 @@ void Fade::update() return; } - const auto currentMilliseconds = mFadeTimer.elapsedTicks(); - const auto step = static_cast(std::clamp(currentMilliseconds * 255u / mDuration.milliseconds, 0u, 255u)); + const auto currentMilliseconds = std::min(mFadeTimer.elapsedTicks(), mDuration.milliseconds); + const auto step = static_cast(currentMilliseconds * 255u / mDuration.milliseconds); mFadeColor.alpha = (mDirection == FadeDirection::In) ? 255 - step : step; if (currentMilliseconds >= mDuration.milliseconds) From 8c30c08e641038f5ed44fe4f98e16e963930f746 Mon Sep 17 00:00:00 2001 From: Daniel Stevens Date: Wed, 5 Mar 2025 22:40:41 -0700 Subject: [PATCH 3/3] Use `scaleLinear` to implement `Fade::update()` --- NAS2D/Renderer/Fade.cpp | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/NAS2D/Renderer/Fade.cpp b/NAS2D/Renderer/Fade.cpp index bcb4c819..7497e166 100644 --- a/NAS2D/Renderer/Fade.cpp +++ b/NAS2D/Renderer/Fade.cpp @@ -12,6 +12,7 @@ #include "Fade.h" #include "Renderer.h" #include "../Math/Rectangle.h" +#include "../Math/MathUtils.h" #include #include @@ -79,8 +80,9 @@ void Fade::update() } const auto currentMilliseconds = std::min(mFadeTimer.elapsedTicks(), mDuration.milliseconds); - const auto step = static_cast(currentMilliseconds * 255u / mDuration.milliseconds); - mFadeColor.alpha = (mDirection == FadeDirection::In) ? 255 - step : step; + mFadeColor.alpha = (mDirection == FadeDirection::In) ? + scaleLinear(currentMilliseconds, uint32_t{0}, mDuration.milliseconds, alphaOpaque, alphaTransparent) : + scaleLinear(currentMilliseconds, uint32_t{0}, mDuration.milliseconds, alphaTransparent, alphaOpaque); if (currentMilliseconds >= mDuration.milliseconds) {