Skip to content

Commit

Permalink
Merge pull request #1265 from lairworks/scaleLinearFade
Browse files Browse the repository at this point in the history
Use `scaleLinear` to implement `Fade::update()`
  • Loading branch information
DanRStevens authored Mar 6, 2025
2 parents 5f45368 + 8c30c08 commit fecc8eb
Showing 1 changed file with 6 additions and 3 deletions.
9 changes: 6 additions & 3 deletions NAS2D/Renderer/Fade.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#include "Fade.h"
#include "Renderer.h"
#include "../Math/Rectangle.h"
#include "../Math/MathUtils.h"

#include <algorithm>
#include <stdexcept>
Expand Down Expand Up @@ -78,10 +79,12 @@ void Fade::update()
return;
}

const auto step = static_cast<uint8_t>(std::clamp<unsigned int>(mFadeTimer.elapsedTicks() * 255u / mDuration.milliseconds, 0u, 255u));
mFadeColor.alpha = (mDirection == FadeDirection::In) ? 255 - step : step;
const auto currentMilliseconds = std::min(mFadeTimer.elapsedTicks(), mDuration.milliseconds);
mFadeColor.alpha = (mDirection == FadeDirection::In) ?
scaleLinear(currentMilliseconds, uint32_t{0}, mDuration.milliseconds, alphaOpaque, alphaTransparent) :
scaleLinear(currentMilliseconds, uint32_t{0}, mDuration.milliseconds, alphaTransparent, alphaOpaque);

if (step == 255)
if (currentMilliseconds >= mDuration.milliseconds)
{
mDirection = FadeDirection::None;
if (!mOnFadeComplete.empty())
Expand Down

0 comments on commit fecc8eb

Please sign in to comment.