From c551c721ce530f337a47c5c39673d8c6fafeb956 Mon Sep 17 00:00:00 2001 From: John McCardle Date: Tue, 7 Mar 2023 07:39:41 -0500 Subject: [PATCH] Animation work: removing pointers from the entire class in favor of std::function/lambdas to write values. This actually works with SFML classes, because I can wrap the setter class --- src/Animation.cpp | 62 ++++++++++++++++++++++++++++++++------------- src/Animation.h | 12 ++++++--- src/McRFPy_API.cpp | 17 +++++++++++++ src/McRFPy_API.h | 4 +++ src/PythonScene.cpp | 10 +++++--- 5 files changed, 80 insertions(+), 25 deletions(-) diff --git a/src/Animation.cpp b/src/Animation.cpp index 7a8ccbc..cc9d0b2 100644 --- a/src/Animation.cpp +++ b/src/Animation.cpp @@ -1,19 +1,19 @@ #include "Animation.h" -Animation::Animation(float _d, void* _t, std::function _cb, bool _l) -:duration(_d), target(_t), callback(_cb), loop(_l), elapsed(0.0f) {} +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* _t, std::function _cb, bool _l) -:Animation(_d, _t, _cb, _l), //duration(_d), target(_t), callback(_cb), loop(_l),elapsed(0.0f), -startvalue(*_t), endvalue(_ev) {} +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, T* _t, std::function _cb, bool _l) -:Animation(_d, _t, _cb, _l), //duration(_d), target(_t), callback(_cb), loop(_l), elapsed(0.0f), -index(0), nonelapsed(0.0f), values(_v) { +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; } @@ -27,40 +27,51 @@ Animation::~Animation() { template<> void LerpAnimation::lerp() { - *(std::string*)target = endvalue.substr(0, endvalue.length() * (elapsed / duration)); + //*(std::string*)target = ; + write(endvalue.substr(0, endvalue.length() * (elapsed / duration))); } template<> void LerpAnimation::lerp() { int delta = endvalue - startvalue; - *(int*)target = startvalue + (elapsed / duration * delta); + //*(int*)target = ; + write(startvalue + (elapsed / duration * delta)); } template<> void LerpAnimation::lerp() { int delta = endvalue - startvalue; - *(float*)target = startvalue + (elapsed / duration * delta); + //*(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; - ((sf::Vector2f*)target)->x = startvalue.x + (elapsed / duration * delta_x); - ((sf::Vector2f*)target)->y = startvalue.y + (elapsed / duration * delta_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); + //((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 <::step(float delta) elapsed += nonelapsed; // or should it be += timestep? nonelapsed = 0; // or should it -= timestep? index++; - *target = values[index]; + //*(T*)target = values[index]; + write(values[index]); if (isDone()) cancel(); //use the exact value, not my math } template void LerpAnimation::cancel() { - *target = endvalue; + //*(T*)target = endvalue; + write(endvalue); } template void DiscreteAnimation::cancel() { - *target = values[values.size() - 1]; + //*(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; +} diff --git a/src/Animation.h b/src/Animation.h index 39aac87..e8a80e6 100644 --- a/src/Animation.h +++ b/src/Animation.h @@ -6,13 +6,13 @@ class Animation protected: static constexpr float EPSILON = 0.05; float duration, elapsed; - void* target; std::function callback; bool loop; public: //Animation(float, T, T*, std::function, bool); // lerp //Animation(float, std::vector, T*, std::function, bool); // discrete - Animation(float, void*, std::function, bool); + Animation(float, std::function, bool); + Animation() {}; virtual void step(float) = 0; virtual void cancel() = 0; bool isDone(); @@ -22,10 +22,12 @@ template class LerpAnimation: public Animation { T startvalue, endvalue; + std::function write; void lerp(); public: ~LerpAnimation() { cancel(); } - LerpAnimation(float, T, T*, std::function, bool); + LerpAnimation(float, T, T, std::function, std::function, bool); + LerpAnimation() {}; void step(float) override final; void cancel() override final; }; @@ -34,10 +36,12 @@ template class DiscreteAnimation: public Animation { std::vector values; + std::function write; float nonelapsed, timestep; int index; public: - DiscreteAnimation(float, std::vector, T*, std::function, bool); + DiscreteAnimation(float, std::vector, std::function, std::function, bool); + DiscreteAnimation() {}; ~DiscreteAnimation() { cancel(); } void step(float) override final; void cancel() override final; diff --git a/src/McRFPy_API.cpp b/src/McRFPy_API.cpp index 49d62df..0382aa1 100644 --- a/src/McRFPy_API.cpp +++ b/src/McRFPy_API.cpp @@ -629,3 +629,20 @@ PyObject* McRFPy_API::_modGrid(PyObject* self, PyObject* args) { Py_INCREF(Py_None); return Py_None; } + +PyObject* McRFPy_API::_createAnimation(PyObject *self, PyObject *args) { + //LerpAnimation::LerpAnimation(float _d, T _ev, T* _t, std::function _cb, std::function _w, bool _l) + std::string menu_key = "demobox1"; + McRFPy_API::animations.push_back( + new LerpAnimation( + 3.0, + sf::Vector2f(100, 100), + McRFPy_API::menus[menu_key]->box.getPosition(), + [](){McRFPy_API::executePyString("print('animation callback')");}, + [=](sf::Vector2f v) {std::cout << "write lambda!" << std::endl; McRFPy_API::menus[menu_key]->box.setPosition(v); std::cout << "Position set to" << McRFPy_API::menus[menu_key]->box.getPosition().x << ", " << McRFPy_API::menus[menu_key]->box.getPosition().y << std::endl;}, + false) + ); + + Py_INCREF(Py_None); + return Py_None; +} diff --git a/src/McRFPy_API.h b/src/McRFPy_API.h index 977855f..cb356fa 100644 --- a/src/McRFPy_API.h +++ b/src/McRFPy_API.h @@ -10,6 +10,10 @@ #include "Grid.h" #include "IndexSprite.h" #include "EntityManager.h" +#include + +// implementation required to link templates +#include "Animation.h" class GameEngine; // forward declared (circular members) diff --git a/src/PythonScene.cpp b/src/PythonScene.cpp index c998db5..3245ac6 100644 --- a/src/PythonScene.cpp +++ b/src/PythonScene.cpp @@ -1,7 +1,7 @@ #include "PythonScene.h" #include "ActionCode.h" #include "McRFPy_API.h" -#include "Animation.h" +//#include "Animation.h" PythonScene::PythonScene(GameEngine* g, std::string pymodule) : Scene(g) { @@ -44,14 +44,18 @@ PythonScene::PythonScene(GameEngine* g, std::string pymodule) } void PythonScene::animate() { + std::cout << "Number of animations: " << McRFPy_API::animations.size() << std::endl; auto frametime = game->getFrameTime(); auto it = McRFPy_API::animations.begin(); - while (it != animations.end()) { + while (it != McRFPy_API::animations.end()) { + std::cout << "Iterating" << std::endl; (*it)->step(frametime); + std::cout << "Step complete" << std::endl; if ((*it)->isDone()) { + std::cout << "Cleaning up" << std::endl; auto prev = it; it++; - animations.erase(prev); + McRFPy_API::animations.erase(prev); } else it++; } /* // workin on it