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
This commit is contained in:
parent
d74635ee4e
commit
c551c721ce
|
@ -1,19 +1,19 @@
|
||||||
#include "Animation.h"
|
#include "Animation.h"
|
||||||
|
|
||||||
Animation::Animation(float _d, void* _t, std::function<void()> _cb, bool _l)
|
Animation::Animation(float _d, std::function<void()> _cb, bool _l)
|
||||||
:duration(_d), target(_t), callback(_cb), loop(_l), elapsed(0.0f) {}
|
:duration(_d), callback(_cb), loop(_l), elapsed(0.0f) {}
|
||||||
|
|
||||||
// linear interpolation constructor
|
// linear interpolation constructor
|
||||||
template<typename T>
|
template<typename T>
|
||||||
LerpAnimation<T>::LerpAnimation(float _d, T _ev, T* _t, std::function<void()> _cb, bool _l)
|
LerpAnimation<T>::LerpAnimation(float _d, T _ev, T _sv, std::function<void()> _cb, std::function<void(T)> _w, bool _l)
|
||||||
:Animation(_d, _t, _cb, _l), //duration(_d), target(_t), callback(_cb), loop(_l),elapsed(0.0f),
|
:Animation(_d, _cb, _l), //duration(_d), target(_t), callback(_cb), loop(_l),elapsed(0.0f),
|
||||||
startvalue(*_t), endvalue(_ev) {}
|
startvalue(_sv), endvalue(_ev), write(_w) {}
|
||||||
|
|
||||||
// discrete values constructor
|
// discrete values constructor
|
||||||
template<typename T>
|
template<typename T>
|
||||||
DiscreteAnimation<T>::DiscreteAnimation(float _d, std::vector<T> _v, T* _t, std::function<void()> _cb, bool _l)
|
DiscreteAnimation<T>::DiscreteAnimation(float _d, std::vector<T> _v, std::function<void()> _cb, std::function<void(T)> _w, bool _l)
|
||||||
:Animation(_d, _t, _cb, _l), //duration(_d), target(_t), callback(_cb), loop(_l), elapsed(0.0f),
|
:Animation(_d, _cb, _l), //duration(_d), target(_t), callback(_cb), loop(_l), elapsed(0.0f),
|
||||||
index(0), nonelapsed(0.0f), values(_v) {
|
index(0), nonelapsed(0.0f), values(_v), write(_w) {
|
||||||
timestep = _v.size() / _d;
|
timestep = _v.size() / _d;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -27,40 +27,51 @@ Animation::~Animation() {
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
void LerpAnimation<std::string>::lerp() {
|
void LerpAnimation<std::string>::lerp() {
|
||||||
*(std::string*)target = endvalue.substr(0, endvalue.length() * (elapsed / duration));
|
//*(std::string*)target = ;
|
||||||
|
write(endvalue.substr(0, endvalue.length() * (elapsed / duration)));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
void LerpAnimation<int>::lerp() {
|
void LerpAnimation<int>::lerp() {
|
||||||
int delta = endvalue - startvalue;
|
int delta = endvalue - startvalue;
|
||||||
*(int*)target = startvalue + (elapsed / duration * delta);
|
//*(int*)target = ;
|
||||||
|
write(startvalue + (elapsed / duration * delta));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
void LerpAnimation<float>::lerp() {
|
void LerpAnimation<float>::lerp() {
|
||||||
int delta = endvalue - startvalue;
|
int delta = endvalue - startvalue;
|
||||||
*(float*)target = startvalue + (elapsed / duration * delta);
|
//*(float*)target = ;
|
||||||
|
write(startvalue + (elapsed / duration * delta));
|
||||||
}
|
}
|
||||||
|
|
||||||
template<>
|
template<>
|
||||||
void LerpAnimation<sf::Vector2f>::lerp() {
|
void LerpAnimation<sf::Vector2f>::lerp() {
|
||||||
|
std::cout << "sf::Vector2f implementation of lerp." << std::endl;
|
||||||
int delta_x = endvalue.x - startvalue.x;
|
int delta_x = endvalue.x - startvalue.x;
|
||||||
int delta_y = endvalue.y - startvalue.y;
|
int delta_y = endvalue.y - startvalue.y;
|
||||||
((sf::Vector2f*)target)->x = startvalue.x + (elapsed / duration * delta_x);
|
std::cout << "Start: " << startvalue.x << ", " << startvalue.y << "; End: " << endvalue.x << ", " << endvalue.y << std::endl;
|
||||||
((sf::Vector2f*)target)->y = startvalue.y + (elapsed / duration * delta_y);
|
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<>
|
template<>
|
||||||
void LerpAnimation<sf::Vector2i>::lerp() {
|
void LerpAnimation<sf::Vector2i>::lerp() {
|
||||||
int delta_x = endvalue.x - startvalue.y;
|
int delta_x = endvalue.x - startvalue.y;
|
||||||
int delta_y = endvalue.y - startvalue.y;
|
int delta_y = endvalue.y - startvalue.y;
|
||||||
((sf::Vector2i*)target)->x = startvalue.x + (elapsed / duration * delta_x);
|
//((sf::Vector2i*)target)->x = startvalue.x + (elapsed / duration * delta_x);
|
||||||
((sf::Vector2i*)target)->y = startvalue.y + (elapsed / duration * delta_y);
|
//((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<typename T>
|
template<typename T>
|
||||||
void LerpAnimation<T>::step(float delta) {
|
void LerpAnimation<T>::step(float delta) {
|
||||||
elapsed += delta;
|
elapsed += delta;
|
||||||
|
std::cout << "LerpAnimation step function. Elapsed: " << elapsed <<std::endl;
|
||||||
lerp();
|
lerp();
|
||||||
if (isDone()) cancel(); //use the exact value, not my math
|
if (isDone()) cancel(); //use the exact value, not my math
|
||||||
}
|
}
|
||||||
|
@ -74,20 +85,35 @@ void DiscreteAnimation<T>::step(float delta)
|
||||||
elapsed += nonelapsed; // or should it be += timestep?
|
elapsed += nonelapsed; // or should it be += timestep?
|
||||||
nonelapsed = 0; // or should it -= timestep?
|
nonelapsed = 0; // or should it -= timestep?
|
||||||
index++;
|
index++;
|
||||||
*target = values[index];
|
//*(T*)target = values[index];
|
||||||
|
write(values[index]);
|
||||||
if (isDone()) cancel(); //use the exact value, not my math
|
if (isDone()) cancel(); //use the exact value, not my math
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void LerpAnimation<T>::cancel() {
|
void LerpAnimation<T>::cancel() {
|
||||||
*target = endvalue;
|
//*(T*)target = endvalue;
|
||||||
|
write(endvalue);
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
void DiscreteAnimation<T>::cancel() {
|
void DiscreteAnimation<T>::cancel() {
|
||||||
*target = values[values.size() - 1];
|
//*(T*)target = values[values.size() - 1];
|
||||||
|
write(values[values.size() - 1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool Animation::isDone() {
|
bool Animation::isDone() {
|
||||||
return elapsed + Animation::EPSILON >= duration;
|
return elapsed + Animation::EPSILON >= duration;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
namespace animation_template_implementations {
|
||||||
|
// instantiate to compile concrete templates
|
||||||
|
LerpAnimation<sf::Vector2f> implement_vector2f;
|
||||||
|
|
||||||
|
auto implement_v2f_const = LerpAnimation<sf::Vector2<float>>(4.0, sf::Vector2<float>(), sf::Vector2f(1,1), [](){}, [](sf::Vector2f v){}, false);
|
||||||
|
LerpAnimation<sf::Vector2i> implement_vector2i;
|
||||||
|
LerpAnimation<int> implment_int;
|
||||||
|
LerpAnimation<std::string> implment_string;
|
||||||
|
LerpAnimation<float> implement_float;
|
||||||
|
DiscreteAnimation<int> implement_int_discrete;
|
||||||
|
}
|
||||||
|
|
|
@ -6,13 +6,13 @@ class Animation
|
||||||
protected:
|
protected:
|
||||||
static constexpr float EPSILON = 0.05;
|
static constexpr float EPSILON = 0.05;
|
||||||
float duration, elapsed;
|
float duration, elapsed;
|
||||||
void* target;
|
|
||||||
std::function<void()> callback;
|
std::function<void()> callback;
|
||||||
bool loop;
|
bool loop;
|
||||||
public:
|
public:
|
||||||
//Animation(float, T, T*, std::function<void()>, bool); // lerp
|
//Animation(float, T, T*, std::function<void()>, bool); // lerp
|
||||||
//Animation(float, std::vector<T>, T*, std::function<void()>, bool); // discrete
|
//Animation(float, std::vector<T>, T*, std::function<void()>, bool); // discrete
|
||||||
Animation(float, void*, std::function<void()>, bool);
|
Animation(float, std::function<void()>, bool);
|
||||||
|
Animation() {};
|
||||||
virtual void step(float) = 0;
|
virtual void step(float) = 0;
|
||||||
virtual void cancel() = 0;
|
virtual void cancel() = 0;
|
||||||
bool isDone();
|
bool isDone();
|
||||||
|
@ -22,10 +22,12 @@ template<typename T>
|
||||||
class LerpAnimation: public Animation
|
class LerpAnimation: public Animation
|
||||||
{
|
{
|
||||||
T startvalue, endvalue;
|
T startvalue, endvalue;
|
||||||
|
std::function<void(T)> write;
|
||||||
void lerp();
|
void lerp();
|
||||||
public:
|
public:
|
||||||
~LerpAnimation() { cancel(); }
|
~LerpAnimation() { cancel(); }
|
||||||
LerpAnimation(float, T, T*, std::function<void()>, bool);
|
LerpAnimation(float, T, T, std::function<void()>, std::function<void(T)>, bool);
|
||||||
|
LerpAnimation() {};
|
||||||
void step(float) override final;
|
void step(float) override final;
|
||||||
void cancel() override final;
|
void cancel() override final;
|
||||||
};
|
};
|
||||||
|
@ -34,10 +36,12 @@ template<typename T>
|
||||||
class DiscreteAnimation: public Animation
|
class DiscreteAnimation: public Animation
|
||||||
{
|
{
|
||||||
std::vector<T> values;
|
std::vector<T> values;
|
||||||
|
std::function<void(T)> write;
|
||||||
float nonelapsed, timestep;
|
float nonelapsed, timestep;
|
||||||
int index;
|
int index;
|
||||||
public:
|
public:
|
||||||
DiscreteAnimation(float, std::vector<T>, T*, std::function<void()>, bool);
|
DiscreteAnimation(float, std::vector<T>, std::function<void()>, std::function<void(T)>, bool);
|
||||||
|
DiscreteAnimation() {};
|
||||||
~DiscreteAnimation() { cancel(); }
|
~DiscreteAnimation() { cancel(); }
|
||||||
void step(float) override final;
|
void step(float) override final;
|
||||||
void cancel() override final;
|
void cancel() override final;
|
||||||
|
|
|
@ -629,3 +629,20 @@ PyObject* McRFPy_API::_modGrid(PyObject* self, PyObject* args) {
|
||||||
Py_INCREF(Py_None);
|
Py_INCREF(Py_None);
|
||||||
return Py_None;
|
return Py_None;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
PyObject* McRFPy_API::_createAnimation(PyObject *self, PyObject *args) {
|
||||||
|
//LerpAnimation<T>::LerpAnimation(float _d, T _ev, T* _t, std::function<void()> _cb, std::function<void(T)> _w, bool _l)
|
||||||
|
std::string menu_key = "demobox1";
|
||||||
|
McRFPy_API::animations.push_back(
|
||||||
|
new LerpAnimation<sf::Vector2f>(
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
|
@ -10,6 +10,10 @@
|
||||||
#include "Grid.h"
|
#include "Grid.h"
|
||||||
#include "IndexSprite.h"
|
#include "IndexSprite.h"
|
||||||
#include "EntityManager.h"
|
#include "EntityManager.h"
|
||||||
|
#include <list>
|
||||||
|
|
||||||
|
// implementation required to link templates
|
||||||
|
#include "Animation.h"
|
||||||
|
|
||||||
class GameEngine; // forward declared (circular members)
|
class GameEngine; // forward declared (circular members)
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
#include "PythonScene.h"
|
#include "PythonScene.h"
|
||||||
#include "ActionCode.h"
|
#include "ActionCode.h"
|
||||||
#include "McRFPy_API.h"
|
#include "McRFPy_API.h"
|
||||||
#include "Animation.h"
|
//#include "Animation.h"
|
||||||
|
|
||||||
PythonScene::PythonScene(GameEngine* g, std::string pymodule)
|
PythonScene::PythonScene(GameEngine* g, std::string pymodule)
|
||||||
: Scene(g) {
|
: Scene(g) {
|
||||||
|
@ -44,14 +44,18 @@ PythonScene::PythonScene(GameEngine* g, std::string pymodule)
|
||||||
}
|
}
|
||||||
|
|
||||||
void PythonScene::animate() {
|
void PythonScene::animate() {
|
||||||
|
std::cout << "Number of animations: " << McRFPy_API::animations.size() << std::endl;
|
||||||
auto frametime = game->getFrameTime();
|
auto frametime = game->getFrameTime();
|
||||||
auto it = McRFPy_API::animations.begin();
|
auto it = McRFPy_API::animations.begin();
|
||||||
while (it != animations.end()) {
|
while (it != McRFPy_API::animations.end()) {
|
||||||
|
std::cout << "Iterating" << std::endl;
|
||||||
(*it)->step(frametime);
|
(*it)->step(frametime);
|
||||||
|
std::cout << "Step complete" << std::endl;
|
||||||
if ((*it)->isDone()) {
|
if ((*it)->isDone()) {
|
||||||
|
std::cout << "Cleaning up" << std::endl;
|
||||||
auto prev = it;
|
auto prev = it;
|
||||||
it++;
|
it++;
|
||||||
animations.erase(prev);
|
McRFPy_API::animations.erase(prev);
|
||||||
} else it++;
|
} else it++;
|
||||||
}
|
}
|
||||||
/* // workin on it
|
/* // workin on it
|
||||||
|
|
Loading…
Reference in New Issue