/* orxonox - the future of 3D-vertical-scrollers Copyright (C) 2004 orx This program is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation; either version 2, or (at your option) any later version. ### File Specific: main-programmer: Benjamin Grauer co-programmer: ... */ /*! \file animation.h A Set of functions to animate some floats inside of an Object We apologize, that most part of the Function-Definitions are located inside this h-file, but this must be like this because it is a template function. */ #ifndef _ANIMATION_H #define _ANIMATION_H // FORWARD DEFINITION template class tList; typedef enum ANIM_FUNCTION {ANIM_CONSTANT, ANIM_LINEAR, ANIM_SINE, ANIM_RANDOM}; typedef enum ANIM_INFINITY {ANIM_INF_CONSTANT, ANIM_INF_LINEAR, ANIM_INF_PINGPONG, ANIM_INF_REWIND};//, ANIM_DELETE} struct AnimKeyFrame { float duration; float value; ANIM_FUNCTION animFunc; }; class Anim { public: void addKeyFrame(float value, float duration, ANIM_FUNCTION animFunc = ANIM_LINEAR); void setInfinity(ANIM_INFINITY preInfinity = ANIM_INF_CONSTANT, ANIM_INFINITY postInfinity = ANIM_INF_CONSTANT); void setAnimFunc(ANIM_FUNCTION animFunc); protected: Anim(void); virtual ~Anim(void); static tList* animatorList; virtual void tick(float time) = 0; // animation functions float random(float timePassed) const; float constant(float timePassed) const; float linear(float timePassed) const; float sine(float timePassed) const; // variables // ANIM_FUNCTION animFunc; float (Anim::*animFunc)(float) const; ANIM_INFINITY preInfinity; ANIM_INFINITY postInfinity; bool bHasKeys; bool bRunning; float localTime; AnimKeyFrame* currentKeyFrame; AnimKeyFrame* nextKeyFrame; tList* keyFrameList; }; //! A Class to handle some animation for single floated values. template class Animation : public Anim { public: Animation(T* object = NULL, void (T::*funcToAnim)(float) = NULL); virtual ~Animation(); void setFuncToAnim(T* object, void (T::*funcToAnim)(float)); virtual void tick(float time); private: T* object; void (T::*funcToAnim)(float); }; /** \brief standard constructor */ template Animation::Animation (T* object, void (T::*funcToAnim)(float)) { this->setFuncToAnim(object, funcToAnim); } /** \brief standard deconstructor */ template Animation::~Animation () { // delete what has to be deleted here } template void Animation::setFuncToAnim(T* object, void (T::*funcToAnim)(float)) { this->object = object; this->funcToAnim = funcToAnim; } template void Animation::tick(float time) { if (this->bRunning) { this->localTime += time; if (localTime >= this->currentKeyFrame->duration) { this->localTime = 0; if (this->currentKeyFrame == this->keyFrameList->lastElement()) switch (this->postInfinity) { case ANIM_INF_CONSTANT: this->bRunning = false; break; case ANIM_INF_REWIND: break; } this->currentKeyFrame = this->keyFrameList->nextElement(this->currentKeyFrame); this->nextKeyFrame = this->keyFrameList->nextElement(this->nextKeyFrame); printf("%p from:%f to:%f\n", this->currentKeyFrame,this->currentKeyFrame->value, this->nextKeyFrame->value); } (this->object->*(funcToAnim))((this->*animFunc)(this->localTime)); } } #endif /* _ANIMATION_H */