From 3b6b94656003a434d48f7d2606ee6cf82714d4e8 Mon Sep 17 00:00:00 2001 From: frank <420@shampoo.ooo> Date: Wed, 8 Sep 2021 20:07:11 -0400 Subject: [PATCH] add default constructor to Animation, set private members in Timer --- src/Animation.cpp | 17 +++++++++------- src/Animation.hpp | 33 +++++++++++++++++++++++-------- src/Recorder.hpp | 6 ++---- src/Timer.cpp | 50 ++++++++++++++++++++++++++++++++++++++++------- src/Timer.hpp | 25 +++++++++++++++++++----- 5 files changed, 100 insertions(+), 31 deletions(-) diff --git a/src/Animation.cpp b/src/Animation.cpp index 8eb93b1..23e87c0 100644 --- a/src/Animation.cpp +++ b/src/Animation.cpp @@ -1,13 +1,17 @@ #include "Animation.hpp" +Animation::Animation() +{ + timer.toggle(Timer::OFF); +} + void Animation::play(float delay, bool play_once) { this->delay = delay; playing = true; paused = false; - previous_step_time = timer.elapsed; + previous_step_time = timer.ms_elapsed(); overflow = 0; - count = 0; ending = play_once; if (delay <= 0) { @@ -56,7 +60,7 @@ void Animation::update() { if (delay > 0) { - delay -= timer.frame_duration; + delay -= timer.ms_last_frame(); if (delay <= 0) { timer.toggle(true); @@ -64,12 +68,11 @@ void Animation::update() } if (delay <= 0) { - if (timer.elapsed - previous_step_time + overflow > frame_length) + if (timer.ms_elapsed() - previous_step_time + overflow > frame_length) { - overflow = timer.elapsed - previous_step_time + overflow - frame_length; - previous_step_time = timer.elapsed; + overflow = timer.ms_elapsed() - previous_step_time + overflow - frame_length; + previous_step_time = timer.ms_elapsed(); step(); - count++; if (ending) { reset(); diff --git a/src/Animation.hpp b/src/Animation.hpp index a6e4914..2168bf6 100644 --- a/src/Animation.hpp +++ b/src/Animation.hpp @@ -4,28 +4,45 @@ #include #include #include - #include "Timer.hpp" typedef std::function callback; class Node; -struct Animation +class Animation { +private: + bool playing = false, ending = false, paused = false; - int previous_step_time = 0, count = 0; - float delay = 0, overflow = 0, frame_length; - callback step; - Node* containing_object; + int previous_step_time = 0; + float delay = 0, overflow = 0, frame_length = 0; + callback step = nullptr; + Node* containing_object = nullptr; Timer 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 milliseconds, representing how often the animation function will run. If omitted, the + * animation function will run every time the Animation object is updated (generally, once per + * frame of the application). */ template - Animation(void(T::*f)(), T* o, float frame_length = 0) : frame_length(frame_length) + Animation(void(T::*member_function)(), T* object, float frame_length = 0) : frame_length(frame_length) + { + bind(member_function, object); + timer.toggle(Timer::OFF); + } + + /* Set the animation function by supplying an object and one of its member functions */ + template + void bind(void(T::*f)(), T* o) { step = std::bind(f, o); - timer.toggle(false); containing_object = static_cast(o); } diff --git a/src/Recorder.hpp b/src/Recorder.hpp index 1854154..0d83b85 100644 --- a/src/Recorder.hpp +++ b/src/Recorder.hpp @@ -1,6 +1,8 @@ #ifndef Recorder_h_ #define Recorder_h_ +#define GLM_ENABLE_EXPERIMENTAL + #include #include #include @@ -9,14 +11,10 @@ #include #include #include - #include "SDL.h" #include "SDL_mixer.h" - -#define GLM_ENABLE_EXPERIMENTAL #include "glm/ext.hpp" #include "json/json.hpp" - #include "filesystem.hpp" #include "Node.hpp" #include "Animation.hpp" diff --git a/src/Timer.cpp b/src/Timer.cpp index 30e2a44..6b3346f 100644 --- a/src/Timer.cpp +++ b/src/Timer.cpp @@ -1,41 +1,77 @@ #include "Timer.hpp" +/* Initialize a Timer object for keeping a general time count in milliseconds or seconds which can be paused + * arbitrarily. The time initializes to zero. By default, the timer begins timing at initialization (this + * may change in the future). The ticks and previous_ticks parameters are initially synchronized with the SDL + * ticks function, which returns the number of milliseconds since the program began, and the SDL ticks function + * is used to keep the time updated. */ Timer::Timer() { ticks = SDL_GetTicks(); ticks_previous = ticks; } +/* Check whether Timer is keeping time or not */ +bool Timer::state() +{ + return timing; +} + +/* Toggle timing on/off */ void Timer::toggle() { - toggle(!is_timing); + toggle(!state()); } +/* Set state explicitly to Timer::ON (true) or Timer::OFF (false) */ void Timer::toggle(bool state) { - is_timing = state; + timing = state; } +/* Reset time elapsed to zero */ void Timer::reset() { - elapsed = 0; + ticks_elapsed = 0; ticks = SDL_GetTicks(); ticks_previous = ticks; frame_duration = 0; } -float Timer::get_seconds_elapsed() +/* Return milliseconds elapsed on timer */ +float Timer::ms_elapsed() { - return elapsed / 1000; + return ticks_elapsed; } +/* Return seconds elapsed on timer */ +float Timer::seconds_elapsed() +{ + return ms_elapsed() / 1000.0f; +} + +/* Return the length of the previous frame in milliseconds (the time between the last two calls to + * the timer update function) */ +float Timer::ms_last_frame() +{ + return frame_duration; +} + +/* Return the length of the previous frame in seconds (the time between the last two calls to the + * timer update function) */ +float Timer::seconds_last_frame() +{ + return ms_last_frame() / 1000.0f; +} + +/* Add time to the timer by measuring the time between this call and the previous call to update */ void Timer::update() { ticks = SDL_GetTicks(); frame_duration = ticks - ticks_previous; - if (is_timing) + if (state()) { - elapsed += frame_duration; + ticks_elapsed += frame_duration; } ticks_previous = ticks; } diff --git a/src/Timer.hpp b/src/Timer.hpp index b16ec51..77e00c0 100644 --- a/src/Timer.hpp +++ b/src/Timer.hpp @@ -3,18 +3,33 @@ #include "SDL.h" -struct Timer +class Timer { - int ticks, ticks_previous, frame_duration = 0, elapsed = 0; - bool is_timing = true; +public: + + enum State : bool + { + OFF, + ON + }; + +private: + + bool timing = Timer::ON; + int ticks = 0, ticks_previous = 0, frame_duration = 0, ticks_elapsed = 0; + +public: Timer(); - + bool state(); void toggle(); void toggle(bool); void reset(); - float get_seconds_elapsed(); + float ms_elapsed(); + float seconds_elapsed(); + float ms_last_frame(); + float seconds_last_frame(); void update(); };