2

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!

Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524
Chad Layton
  • 127
  • 6

1 Answers1

1

In short, you want to type erase.

So here is a full type erasure library implemented as pseudo-method pointers.

You can do something fancy, splitting dispatch from storage. Or you can use it directly.

Stuff all of the from/to/pointer into a single any. They are tied, after all.

Then use a super_any containing that with a pseudo-method.

template<class T>
struct Tparams {
  T from; T to; T* value;
};

template<class T>
Tparams<T> make_params( T from, T to, T* value ) {
  return {from, to, value};
}

template<class TEase>
auto tweener() {
  return [](auto&& Tparam, float t){
    *Tparam.value = TEase::run(t, Tparam.from, Tparam.to);
  };
}
template<class TEase>
const auto do_tween = make_any_method<void(float)>(tweener<TEase>());

template <typename TEase>
struct tween
{
  super_any<decltype(do_tween<TEase>)> Tparams;
  float _running_time;
  float _duration;
};

and then bob is your uncle. live example.

template <typename TEase>
bool update(tween<TEase>& tween, float dt)
{
  float t = tween._running_time / tween._duration;
  t = (std::min)(t, 1.0f);

  (tween.Tparams->*do_tween<TEase>)(t);

  tween._running_time += dt;

  return true;
}

float value = 0;
auto test = tween<easing::linear>{make_params(0.0f, 10.0f, &value), 0.0f, 100.0f};

for (int i = 0; i < 100; ++i)
{
  update( test, 1.0 );

  std::cout << value << '\n';
}

Now the library I linked to ties the type erased pseudo-method to the any storage. The design does not require this, nor does type erasure in general require the pseudo-method technique.

We could split the any_methods from the any storage, and manually switch which was active.

But the fact you have 3 independent type erasures to any is actually a design flaw.

Community
  • 1
  • 1
Yakk - Adam Nevraumont
  • 262,606
  • 27
  • 330
  • 524