#include "Animation.h" Animation::Animation(float _d, std::function _cb, bool _l) :duration(_d), callback(_cb), loop(_l), elapsed(0.0f) {} // linear interpolation constructor template LerpAnimation::LerpAnimation(float _d, T _ev, T _sv, std::function _cb, std::function _w, bool _l) :Animation(_d, _cb, _l), //duration(_d), target(_t), callback(_cb), loop(_l),elapsed(0.0f), startvalue(_sv), endvalue(_ev), write(_w) {} // discrete values constructor template DiscreteAnimation::DiscreteAnimation(float _d, std::vector _v, std::function _cb, std::function _w, bool _l) :Animation(_d, _cb, _l), //duration(_d), target(_t), callback(_cb), loop(_l), elapsed(0.0f), index(0), nonelapsed(0.0f), values(_v), write(_w) { timestep = _v.size() / _d; } /* // don't call virtual functions (like cancel()) from base class destructor * // child classes destructors' are called first anyway Animation::~Animation() { // deconstructor sets target to desired end state (no partial values) cancel(); } */ template<> void LerpAnimation::lerp() { //*(std::string*)target = ; write(endvalue.substr(0, endvalue.length() * (elapsed / duration))); } template<> void LerpAnimation::lerp() { int delta = endvalue - startvalue; //*(int*)target = ; write(startvalue + (elapsed / duration * delta)); } template<> void LerpAnimation::lerp() { int delta = endvalue - startvalue; //*(float*)target = ; write(startvalue + (elapsed / duration * delta)); } template<> void LerpAnimation::lerp() { std::cout << "sf::Vector2f implementation of lerp." << std::endl; int delta_x = endvalue.x - startvalue.x; int delta_y = endvalue.y - startvalue.y; std::cout << "Start: " << startvalue.x << ", " << startvalue.y << "; End: " << endvalue.x << ", " << endvalue.y << std::endl; std::cout << "Delta: " << delta_x << ", " << delta_y << std::endl; //((sf::Vector2f*)target)->x = startvalue.x + (elapsed / duration * delta_x); //((sf::Vector2f*)target)->y = startvalue.y + (elapsed / duration * delta_y); write(sf::Vector2f(startvalue.x + (elapsed / duration * delta_x), startvalue.y + (elapsed / duration * delta_y))); } template<> void LerpAnimation::lerp() { int delta_x = endvalue.x - startvalue.y; int delta_y = endvalue.y - startvalue.y; //((sf::Vector2i*)target)->x = startvalue.x + (elapsed / duration * delta_x); //((sf::Vector2i*)target)->y = startvalue.y + (elapsed / duration * delta_y); write(sf::Vector2i(startvalue.x + (elapsed / duration * delta_x), startvalue.y + (elapsed / duration * delta_y))); } template void LerpAnimation::step(float delta) { elapsed += delta; std::cout << "LerpAnimation step function. Elapsed: " << elapsed < void DiscreteAnimation::step(float delta) { 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++; //*(T*)target = values[index]; write(values[index]); if (isDone()) cancel(); //use the exact value, not my math } template void LerpAnimation::cancel() { //*(T*)target = endvalue; write(endvalue); } template void DiscreteAnimation::cancel() { //*(T*)target = values[values.size() - 1]; write(values[values.size() - 1]); } bool Animation::isDone() { return elapsed + Animation::EPSILON >= duration; } namespace animation_template_implementations { // instantiate to compile concrete templates //LerpAnimation implement_vector2f; auto implement_v2f_const = LerpAnimation>(4.0, sf::Vector2(), sf::Vector2f(1,1), [](){}, [](sf::Vector2f v){}, false); //LerpAnimation implement_vector2i; //LerpAnimation implment_int; //LerpAnimation implment_string; //LerpAnimation implement_float; //DiscreteAnimation implement_int_discrete; }