I'm trying to implement a tweening library in C++. This is mostly for educational purposes because there are probably other libraries that do this better than I can (like: https://github.com/mobius3/tweeny, but is over my head in terms of template-fu).
The primary goal was to do as much as possible at compile time. Otherwise I could simplify things with some function pointers.
Here are a couple of the (many) easing functions:
struct easing
{
struct linear {
template<typename T>
static T run(float t, T start, T end) {
return static_cast<T>((end - start) * t + start);
}
};
struct quadratic_in {
template<typename T>
static T run(float t, T start, T end) {
return static_cast<T>((end - start) * t * t + start);
}
};
struct quadratic_out {
template<typename T>
static T run(float t, T start, T end) {
return static_cast<T>((-(end - start)) * t * (t - 2) + start);
}
};
};
And here's the relevant bits of the library code:
namespace detail {
template <typename TEase, typename T>
struct tween
{
T _from;
T _to;
float _running_time;
float _duration;
T* _value;
};
template <typename TEase, typename T>
bool update(tween<TEase, T>& tween, float dt)
{
float t = tween._running_time / tween._duration;
if (t > 1.0f)
{
*tween._value = tween._to;
return false;
}
*tween._value = TEase::run(t, tween._from, tween._to);
tween._running_time += dt;
return true;
}
std::tuple<
std::vector<tween<easing::linear, float>>,
std::vector<tween<easing::quadratic_in, float>>,
std::vector<tween<easing::quadratic_out, float>>,
std::vector<tween<easing::linear, my_vec2_type>>,
std::vector<tween<easing::quadratic_in, my_vec2_type>>,
std::vector<tween<easing::quadratic_out, my_vec2_type>>,
std::vector<tween<easing::linear, my_vec3_type>>,
std::vector<tween<easing::quadratic_in, my_vec3_type>>,
std::vector<tween<easing::quadratic_out, my_vec3_type>>,
// Getting silly...
> g_tweens;
}
void update(float dt)
{
// Can use std::apply with C++17
my_apply([dt](auto& tweens) {
for (auto it = begin(tweens); it != end(tweens);)
{
if (update(*it, dt))
{
++it;
}
else
{
it = tweens.erase(it);
}
}
}, detail::g_tweens);
}
template <typename TEasing, typename T>
void create(T* value, T to, float duration)
{
std::get<std::vector<detail::tween<TEasing, T>>>(detail::g_tweens).push_back({ *value, to, 0.0f, duration, value });
}
And an example:
my_vec3_type color(1.0f, 0.0f, 0.0f);
tween::create<easing::linear>(&color, my_vec3_type(0.0f, 1.0f, 0.0f), 1.0f);
for (int i = 0; i < 100; ++i)
{
tween::update(0.01f);
std::cout << "color = (" << color.r << ", " << color.g << ", " << color.b << ")" << std::endl;
}
This actually works mostly how I wanted. Sure, I needed to add all the easing types to the tuple - and there are a lot - but I thought that might be reasonable considering they're not likely to change much.
But originally I only needed floats. And now I want to be able to ease other types like colors and maybe a few others. So there are potentially hundreds of combinations. And anyone else who wants to use this would need to edit the g_tween signature to add their own types.
The biggest problem - as I see it - is that second template argument to the tween type to allow me to use any scalar type. I just want it to work for anything that has the right operators defined. Like:
template <typename TEase>
struct tween
{
std::any _from;
std::any _to;
float _running_time;
float _duration;
std::any* _value;
};
That is, if there were some way to recover the type so I could call the correct easing function. Any suggestions or alternatives welcome!