//------------------------------------------------------------------------------------- // // JGE++ is a hardware accelerated 2D game SDK for PSP/Windows. // // Licensed under the BSD license, see LICENSE in JGE root for details. // // Copyright (c) 2007 James Hui (a.k.a. Dr.Watson) // //------------------------------------------------------------------------------------- #ifndef _JANIMATOR_H_ #define _JANIMATOR_H_ #include #include "JRenderer.h" using namespace std; class JResourceManager; class JQuad; class JAnimatorFrame; class JAnimatorObject; ////////////////////////////////////////////////////////////////////////// /// A frame based animation system. The animation frames and play sequence /// are loaded from a XML file. /// /// A sample animation script: /// /// @code /// /// /// /// @endcode /// /// A frame can also overide the global frame rate by using the "time" parameter (in second) /// as shown above. /// ////////////////////////////////////////////////////////////////////////// class JAnimator { public: ////////////////////////////////////////////////////////////////////////// /// Constructor. /// /// @param resourceMgr - ResourceManager to look for images (JQuads) /// ////////////////////////////////////////////////////////////////////////// JAnimator(JResourceManager* resourceMgr); ////////////////////////////////////////////////////////////////////////// /// Destructor. /// ////////////////////////////////////////////////////////////////////////// ~JAnimator(); ////////////////////////////////////////////////////////////////////////// /// Load animation sequence from a script file. /// /// @param scriptFile - Animation script. /// /// @return True if no problem during loading. False otherwise. /// ////////////////////////////////////////////////////////////////////////// bool Load(const char* scriptFile); ////////////////////////////////////////////////////////////////////////// /// Start animation. /// ////////////////////////////////////////////////////////////////////////// void Start(); ////////////////////////////////////////////////////////////////////////// /// Stop animation. /// ////////////////////////////////////////////////////////////////////////// void Stop(); ////////////////////////////////////////////////////////////////////////// /// Pause animation. /// ////////////////////////////////////////////////////////////////////////// void Pause(); ////////////////////////////////////////////////////////////////////////// /// Resume animation. /// ////////////////////////////////////////////////////////////////////////// void Resume(); ////////////////////////////////////////////////////////////////////////// /// Update animation. /// /// @param dt - Time elapsed since last update (in second). /// ////////////////////////////////////////////////////////////////////////// void Update(float dt); ////////////////////////////////////////////////////////////////////////// /// Render animation. /// ////////////////////////////////////////////////////////////////////////// void Render(); ////////////////////////////////////////////////////////////////////////// /// Check if animation is playing or not. /// /// @return True if playing animation. /// ////////////////////////////////////////////////////////////////////////// bool IsAnimating(); ////////////////////////////////////////////////////////////////////////// /// Check if the animation is active. /// /// @return True if active. /// ////////////////////////////////////////////////////////////////////////// bool IsActive(); JResourceManager* GetResourceManager(); ////////////////////////////////////////////////////////////////////////// /// Set current frame to a particular index. /// /// @param index - The new index of current frame. /// ////////////////////////////////////////////////////////////////////////// void SetCurrentFrameIndex(int index); ////////////////////////////////////////////////////////////////////////// /// Get index of current frame. /// /// @return Index of current frame. /// ////////////////////////////////////////////////////////////////////////// int GetCurrentFrameIndex(); ////////////////////////////////////////////////////////////////////////// /// Set animation type. /// /// @param type - Animation type. /// /// @code /// JSprite::ANIMATION_TYPE_LOOPING - Default /// JSprite::ANIMATION_TYPE_ONCE_AND_GONE /// JSprite::ANIMATION_TYPE_ONCE_AND_STAY /// JSprite::ANIMATION_TYPE_ONCE_AND_BACK /// JSprite::ANIMATION_TYPE_PINGPONG /// @endcode /// ////////////////////////////////////////////////////////////////////////// void SetAnimationType(int type); ////////////////////////////////////////////////////////////////////////// /// Set position of the sprite. /// /// @param x - X position. /// @param y - Y position. /// ////////////////////////////////////////////////////////////////////////// void SetPosition(float x, float y); ////////////////////////////////////////////////////////////////////////// /// Set anchor point of the animation. All rendering operations will be /// based on this anchor point. /// /// @param x - X position of the anchor point. /// @param y - Y position of the anchor point. /// ////////////////////////////////////////////////////////////////////////// void SetHotSpot(float x, float y); private: JResourceManager* mResource; vector mFrames; bool mAnimating; bool mActive; int mCurrentFrame; int mAnimationType; int mFrameDelta; float mX; float mY; float mHotSpotX; float mHotSpotY; }; ////////////////////////////////////////////////////////////////////////// /// A single frame of an animation. /// ////////////////////////////////////////////////////////////////////////// class JAnimatorFrame { public: ////////////////////////////////////////////////////////////////////////// /// Constructor. /// /// @param parent - Parent of the frame. /// ////////////////////////////////////////////////////////////////////////// JAnimatorFrame(JAnimator* parent); ////////////////////////////////////////////////////////////////////////// /// Destructor. /// ////////////////////////////////////////////////////////////////////////// ~JAnimatorFrame(); ////////////////////////////////////////////////////////////////////////// /// Add a new object into the frame. /// /// @param obj - New animation object. /// ////////////////////////////////////////////////////////////////////////// void AddObject(JAnimatorObject *obj); ////////////////////////////////////////////////////////////////////////// /// Set play time of the frame. /// /// @param duration - Time to play (in second). /// ////////////////////////////////////////////////////////////////////////// void SetFrameTime(float duration); ////////////////////////////////////////////////////////////////////////// /// Frame update. /// /// @param dt - Time elapsed since last update (in second). /// /// @return True if the frame is done. /// ////////////////////////////////////////////////////////////////////////// bool Update(float dt); ////////////////////////////////////////////////////////////////////////// /// Render frame. /// /// @param x - X position for rendering. /// @param y - Y position for rendering. /// ////////////////////////////////////////////////////////////////////////// void Render(float x, float y); ////////////////////////////////////////////////////////////////////////// /// Start playing the frame. /// ////////////////////////////////////////////////////////////////////////// void Start(); private: float mTimer; float mFrameTime; JAnimator* mAnimator; vector mObjects; }; ////////////////////////////////////////////////////////////////////////// /// Animation object (image quad) in a frame. /// ////////////////////////////////////////////////////////////////////////// class JAnimatorObject { public: ////////////////////////////////////////////////////////////////////////// /// Constructor. /// ////////////////////////////////////////////////////////////////////////// JAnimatorObject(); ////////////////////////////////////////////////////////////////////////// /// Destructor. /// ////////////////////////////////////////////////////////////////////////// ~JAnimatorObject(); ////////////////////////////////////////////////////////////////////////// /// Update object. /// /// @param dt - Time elapsed since last update (in second). /// ////////////////////////////////////////////////////////////////////////// void Update(float dt); ////////////////////////////////////////////////////////////////////////// /// Render object. /// /// @param x - X position for rendering. /// @param y - Y position for rendering. /// ////////////////////////////////////////////////////////////////////////// void Render(float x, float y); ////////////////////////////////////////////////////////////////////////// /// Set something to show. /// /// @param quad - Image quad. /// ////////////////////////////////////////////////////////////////////////// void SetQuad(JQuad *quad); ////////////////////////////////////////////////////////////////////////// /// Set position of the object. /// /// @param x - X position. /// @param y - Y position. /// ////////////////////////////////////////////////////////////////////////// void SetPosition(float x, float y); ////////////////////////////////////////////////////////////////////////// /// Set rotation factor of the object. /// /// @param angle - Rotation angle in radian. /// ////////////////////////////////////////////////////////////////////////// void SetRotation(float angle); ////////////////////////////////////////////////////////////////////////// /// Set horizontal scale of the object. /// /// @param scale - Horizontal scale. /// ////////////////////////////////////////////////////////////////////////// void SetHScale(float scale); ////////////////////////////////////////////////////////////////////////// /// Set vertical scale of the object. /// /// @param scale - Vertical scale. /// ////////////////////////////////////////////////////////////////////////// void SetVScale(float scale); ////////////////////////////////////////////////////////////////////////// /// Set blending color of the object. /// /// @param color - Blending color. /// ////////////////////////////////////////////////////////////////////////// void SetColor(PIXEL_TYPE color); ////////////////////////////////////////////////////////////////////////// /// Set horizontal flipping. /// /// @param flag - flipping flag. /// ////////////////////////////////////////////////////////////////////////// void SetFlip(bool flag); private: JRenderer* mRenderer; JQuad* mQuad; float mX; float mY; float mRotation; float mHScale; float mVScale; PIXEL_TYPE mColor; bool mFlipped; }; #endif