From 47e823d5b97294097e988d69f01028456e9ecbc8 Mon Sep 17 00:00:00 2001 From: John McCardle Date: Sun, 5 Mar 2023 22:44:39 -0500 Subject: [PATCH] Animation class (not tested) --- src/Animation.cpp | 81 +++++++++++++++++++++++++++++++++++++++++++++++ src/Animation.h | 23 ++++++++++++++ 2 files changed, 104 insertions(+) create mode 100644 src/Animation.cpp create mode 100644 src/Animation.h diff --git a/src/Animation.cpp b/src/Animation.cpp new file mode 100644 index 0000000..a50c047 --- /dev/null +++ b/src/Animation.cpp @@ -0,0 +1,81 @@ +#include "Animation.h" + +// linear interpolation constructor +template +Animation::Animation(float _d, T _ev, T* _t, std::function _cb, bool _l) +:duration(_d), endvalue(_ev), target(_t), callback(_cb), loop(_l), +index(-1), startvalue(*_t), elapsed(0.0f) {} + +// discrete values constructor +template +Animation::Animation(float _d, std::vector _v, T* _t, std::function _cb, bool _l) +:duration(_d), target(_t), callback(_cb), values(_v), loop(_l), +index(0), startvalue(*_t), elapsed(0.0f), nonelapsed(0.0f) { + timestep = _v.size() / _d; +} + +template +Animation::~Animation() { + // deconstructor sets target to desired end state (no partial values) + cancel(); +} + +template<> +void Animation::lerp() { + *target = endvalue.substr(0, endvalue.length() * (elapsed / duration)); +} + +template<> +void Animation::lerp() { + int delta = endvalue - startvalue; + *target = startvalue + (elapsed / duration * delta); +} + +template<> +void Animation::lerp() { + int delta = endvalue - startvalue; + *target = startvalue + (elapsed / duration * delta); +} + +template<> +void Animation::lerp() { + int delta_x = endvalue.x - startvalue.x; + int delta_y = endvalue.y - startvalue.y; + target->x = startvalue.x + (elapsed / duration * delta_x); + target->y = startvalue.y + (elapsed / duration * delta_y); +} + +template<> +void Animation::lerp() { + int delta_x = endvalue.x - startvalue.y; + int delta_y = endvalue.y - startvalue.y; + target->x = startvalue.x + (elapsed / duration * delta_x); + target->y = startvalue.y + (elapsed / duration * delta_y); +} + +template +void Animation::step(float delta) +{ + if (index == -1) { + // lerp function + elapsed += delta; + lerp(); + } + else { + nonelapsed += delta; + if (nonelapsed < timestep) return; + if (index == values.size() - 1) return; + elapsed += nonelapsed; // or should it be += timestep? + nonelapsed = 0; // or should it -= timestep? + index++; + *target = values[index]; + } +} + +template +void Animation::cancel() { + if (index == -1) + *target = endvalue; + else + *target = values[values.size() - 1]; +} diff --git a/src/Animation.h b/src/Animation.h new file mode 100644 index 0000000..9509876 --- /dev/null +++ b/src/Animation.h @@ -0,0 +1,23 @@ +#pragma once +#include "Common.h" + +template +class Animation +{ + static constexpr float EPSILON = 0.05; + T startvalue, endvalue; + int index; + float duration, elapsed, nonelapsed, timestep; + T* target; + std::vector values; + std::function callback; + bool loop; +public: + Animation(float, T, T*, std::function, bool); // lerp + Animation(float, std::vector, T*, std::function, bool); // discrete + ~Animation(); + void lerp(); + void step(float); + void cancel(); + bool isDone(); +};