Search code examples
c++functiontween

Creating tweening functions using C++?


How should one create an interface for tweening something using C++? For instance, I want to fade a picture in over a duration of five seconds using a static function call like:

Graphics::FadeSurface( Surface mySurface, int FrameHeight, int NumOfFrames,
   int FadeDirection, double Duration )

I have a hard-coded setup that creates an object for each tween action that needs to be performed. I have been using a DeltaTime variable that keeps track of how much time has passed since the program has launched to control logic and such. I've included an example (much less refined) to show you kind of what I'm trying to do:

Example Logic Loop:


gameLoop( double DeltaTime ){

    // ...
    // logic
    // ...

    bool isItDone = otherClass.HaveFiveSecondsElapsed( double DeltaTime );

    if( isItDone == true )
        exit(1);

    // ...
    // logic
    // ... 

}

Example Tweening Class:


other_Class::other_Class(){

    InitialTime = 0;
    InitialTime_isSet = false;

}

bool other_class::HaveFiveSecondsElapsed( double DeltaTime ){

    // Setting InitialTime if it hasn't already been set
    if( otherClass.InitialTime_isSet == false ){

        otherClass.InitialTime = DeltaTime;
        otherClass.InitialTime_isSet = true;

    }

    bool toReturn = false;

    if( DeltaTime - InitialTime > 5 )
        toReturn = true;

    return toReturn;

}

Any help is greatly appreciated. Thanks!


Solution

  • I built a Tween engine for java that is generic enough to be used to tween any attribute of any object. The generic part is done through the definition of a "Tweenable" interface that users need to implement to tween their objects.

    I greatly encourage you to use it as inspiration to build your engine, or directly port it. I can also plan a home-made port to C++, but it would be quite a lot of work to maintain it up-to-date with the current java version (which grows very fast).

    http://code.google.com/p/java-universal-tween-engine/

    NB: I made a more elaborated answer about this engine in this question:
    Android: tween animation of a bitmap