Skip to content

Added fade helpers to help clean code in game objects #1641

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 17, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
74 changes: 74 additions & 0 deletions src/util/fade_helper.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// SuperTux
// Copyright (C) 2021 A. Semphris <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#include "util/fade_helper.hpp"

FadeHelper::FadeHelper(float time, float target_value,
float start_value, easing ease) :
m_value(nullptr),
m_start(start_value),
m_target(target_value),
m_time(0.f),
m_total_time(time),
m_ease(ease)
{
}

FadeHelper::FadeHelper(float* value, float time,
float target_value, easing ease) :
m_value(value),
m_start(*value),
m_target(target_value),
m_time(0.f),
m_total_time(time),
m_ease(ease)
{
}

float
FadeHelper::update(float dt_sec)
{
m_time += dt_sec;

if (completed())
{
m_time = m_total_time;
if (m_value)
*m_value = m_target;
return m_target;
}
else
{
// FLOAT/DOUBLE CONVERSION
// If at some point in development, floats are changed to doubles, or the
// ease funciton return type change from double to float, remove the casts
// here (it takes a lot of space). ~ Semphris
float progress = static_cast<float>(m_ease(static_cast<double>(
m_start + (m_target - m_start) * (m_time / m_total_time)
)));
if (m_value)
*m_value = progress;
return progress;
}
}

bool
FadeHelper::completed() const
{
return m_time >= m_total_time;
}

/* EOF */
58 changes: 58 additions & 0 deletions src/util/fade_helper.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
// SuperTux
// Copyright (C) 2021 A. Semphris <[email protected]>
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#ifndef HEADER_SUPERTUX_UTIL_FADE_HELPER_HPP
#define HEADER_SUPERTUX_UTIL_FADE_HELPER_HPP

#include "math/easing.hpp"

class FadeHelper
{
public:
/** Initialize FadeHelper without binding to a value */
FadeHelper(float time, float target_value,
float start_value = 0.f, easing ease = LinearInterpolation);

/** Initialize FadeHelper, binding to @c value */
FadeHelper(float* value, float time,
float target_value, easing ease = LinearInterpolation);


/**
* Increases the internal timer of the FadeHelper.
*
* @param dt_sec The increase in time since last call to update().
* @returns The new value for the internal value.
*/
float update(float dt_sec);

/** @returns true if the FadeHelper has completed fading. */
bool completed() const;

private:
float* m_value;
float m_start, m_target,
m_time, m_total_time;
easing m_ease;

private:
FadeHelper(const FadeHelper&) = delete;
FadeHelper& operator=(const FadeHelper&) = delete;
};

#endif

/* EOF */