spacebox/src/Animation.hpp

85 lines
2.8 KiB
C++

/* +------------------------------------------------------+
____/ \____ /| - Open source game framework licensed to freely use, |
\ / / | copy, modify and sell without restriction |
+--\ ^__^ /--+ | |
| ~/ \~ | | - created for <https://foam.shampoo.ooo> |
| ~~~~~~~~~~~~ | +------------------------------------------------------+
| SPACE ~~~~~ | /
| ~~~~~~~ BOX |/
+-------------*/
#pragma once
#include <vector>
#include <functional>
#include <algorithm>
#include "Timer.hpp"
class Animation
{
private:
typedef std::function<void()> Callback;
bool play_state = false, ending = false, paused = false;
float delay = 0.0f, overflow = 0.0f, _frame_length = 0.0f, previous_step_time = 0.0f;
Callback step = nullptr;
sb::Timer timer;
public:
Animation();
/*!
* Create an Animation object by supplying a function and interval at which the function should run. Run Animation::update(float)
* regularly with an updated timestamp from Game::update(float), and the function will be launched automatically at the given
* interval. If the interval is omitted, the function will run every time Animation::update(float) is run.
*
* @param step function to be run every given amount of seconds, or zero to run every update
* @param frame_length seconds between each run of the function
*/
Animation(Callback step, float frame_length = 0.0f) : _frame_length(frame_length), step(step)
{
timer.off();
}
/*!
* Set the duration in seconds of each frame of the animation.
*
* @param length frame length in seconds
*/
void frame_length(float length);
/*!
* Turn the play state to on, causing the animation's callback to run once every frame length. If a delay is given, wait before running. If
* the play_once flag is set to true, only play the callback once after the delay.
*
* @param delay Amount of seconds to delay before running
* @param play_once If true, only run the callback once instead of once every frame length
*/
void play(float delay = 0.0f, bool play_once = false);
/*!
* Run the animation's callback only once, optionally after a specified delay. If no delay is specified, it will run immediately.
*
* @param delay Amount of seconds to delay before running
*/
void play_once(float delay = 0.0f);
void pause();
void unpause();
void reset();
bool playing(bool = true) const;
void update(float timestamp);
};
/* Add Animation class to the sb namespace. This should be the default location, but Animation is left in the global namespace
* for backward compatibility.
*/
namespace sb
{
using ::Animation;
}