add default constructor to Animation, set private members in Timer

This commit is contained in:
frank 2021-09-08 20:07:11 -04:00
parent 18f83968f3
commit 3b6b946560
5 changed files with 100 additions and 31 deletions

View File

@ -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();

View File

@ -4,28 +4,45 @@
#include <vector>
#include <functional>
#include <algorithm>
#include "Timer.hpp"
typedef std::function<void()> 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<typename T>
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<typename T>
void bind(void(T::*f)(), T* o)
{
step = std::bind(f, o);
timer.toggle(false);
containing_object = static_cast<Node*>(o);
}

View File

@ -1,6 +1,8 @@
#ifndef Recorder_h_
#define Recorder_h_
#define GLM_ENABLE_EXPERIMENTAL
#include <list>
#include <vector>
#include <sstream>
@ -9,14 +11,10 @@
#include <functional>
#include <cstdlib>
#include <fstream>
#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"

View File

@ -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;
}

View File

@ -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();
};