track timestamp with Timer class

This commit is contained in:
ohsqueezy 2023-06-24 13:05:40 -04:00
parent 635562263b
commit c0b55752e1
7 changed files with 49 additions and 31 deletions

View File

@ -19,9 +19,9 @@ void Animation::play(float delay, bool play_once)
}
}
void Animation::set_frame_length(float length)
void Animation::frame_length(float length)
{
frame_length = length;
_frame_length = length;
}
void Animation::play_once(float delay)
@ -68,16 +68,16 @@ void Animation::update(float timestamp)
}
if (delay <= 0)
{
if (timer.elapsed() - previous_step_time + overflow > frame_length)
if (timer.elapsed() - previous_step_time + overflow > _frame_length)
{
overflow = timer.elapsed() - previous_step_time + overflow - frame_length;
overflow = timer.elapsed() - previous_step_time + overflow - _frame_length;
previous_step_time = timer.elapsed();
step();
if (ending)
{
reset();
}
if (overflow > frame_length)
if (overflow > _frame_length)
{
overflow = 0;
}

View File

@ -15,37 +15,44 @@
#include <algorithm>
#include "Timer.hpp"
typedef std::function<void()> Callback;
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;
float delay = 0.0f, overflow = 0.0f, _frame_length = 0.0f, previous_step_time = 0.0f;
Callback step = nullptr;
// Node* containing_object = nullptr;
sb::Timer timer = sb::Timer();
sb::Timer timer;
public:
Animation();
/*!
* Constructor that allows passing a pointer to an object and a pointer to one of its
* member functions that will be used as the animation function. Frame length can be supplied
* in seconds, representing how often the animation function will run. If omitted, the
* animation function will run every time the Animation object is updated.
* 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) : frame_length(frame_length), step(step)
Animation(Callback step, float frame_length = 0.0f) : _frame_length(frame_length), step(step)
{
timer.off();
}
void set_frame_length(float);
void play(float = 0, bool = false);
void play_once(float = 0);
/*!
* Set the duration in seconds of each frame of the animation.
*
* @param length frame length in seconds
*/
void frame_length(float length);
void play(float = 0.0f, bool = false);
void play_once(float = 0.0f);
void pause();
void unpause();
void reset();

View File

@ -37,11 +37,9 @@ struct BGM : Node
};
class SoundEffect : public Node
struct SoundEffect : Node
{
public:
Mix_Chunk* chunk = nullptr;
int channel, loops = 0;
float volume = 1.0f;

View File

@ -189,7 +189,7 @@ void Configuration::enable_auto_refresh(const fs::path& file_to_refresh, float i
}
#endif
this->file_to_refresh = file_to_refresh;
auto_refresher.set_frame_length(interval);
auto_refresher.frame_length(interval);
auto_refresher.play();
}

View File

@ -360,7 +360,7 @@ void Recorder::update(float timestamp)
{
end_recording();
}
animation.set_frame_length(frame_length());
animation.frame_length(frame_length());
animation.update(timestamp);
}

View File

@ -40,16 +40,22 @@ float sb::Timer::frame() const
return frame_duration;
}
void sb::Timer::update(float timestamp)
float sb::Timer::stamp() const
{
return _stamp;
}
void sb::Timer::update(float stamp)
{
_stamp = stamp;
if (previous_is_recorded)
{
frame_duration = timestamp - timestamp_previous;
frame_duration = stamp - stamp_previous;
if (*this)
{
_elapsed += frame();
}
}
timestamp_previous = timestamp;
stamp_previous = stamp;
previous_is_recorded = true;
}

View File

@ -23,7 +23,7 @@ namespace sb
private:
bool timing = false, previous_is_recorded = false;
float timestamp_previous = 0.0f, frame_duration = 0.0f, _elapsed = 0.0f;
float stamp_previous = 0.0f, frame_duration = 0.0f, _elapsed = 0.0f, _stamp = 0.0f;
public:
@ -75,16 +75,23 @@ namespace sb
float frame() const;
/*!
* Update the timer's elapsed time, using the amount of seconds elapsed in the program at the start of the frame to keep track.
* @return timestamp at the last update
*/
float stamp() const;
/*!
* Update the timer's elapsed time using the given timestamp to determine the amount of time passed since the previous update.
*
* The timer should be updated every frame to keep track of the timestamp, regardless of whether it is currently timing or not.
* The timestamp will be saved and can be retrieved with Timer::stamp().
*
* The timer can be updated even if it is not currently timing.
*
* The timestamp should be forwarded from the value passed to Game::update, so that it is consistently the timestamp at the
* beginning of the frame.
*
* @param timestamp seconds elapsed since the start of the program
* @param stamp seconds elapsed since the start of the program
*/
void update(float timestamp);
void update(float stamp);
/*!
* Applies delta timing to a value: returns the value as weighted by the amount of time passed since the