#ifndef SPRITE_H #define SPRITE_H #include #include namespace Engine { /** \brief A simple sprite class that allows animations and multiple sprites in one image * * Sprites are drawn aligned with the XZ-plane and the Y-axis pointing up. * */ class Sprite { public: Sprite() : mScale (1.f), mWidth (0), mHeight (0), mGLTextureName (0), mSubSpriteCount (1), mAnimation (false), mAnimationFrameCount(1), mAnimationTimer(0.f), mAnimationFrameRate(0.f) { } Sprite (const Sprite &othersprite) : mScale (othersprite.mScale), mWidth (othersprite.mWidth), mHeight (othersprite.mHeight), mGLTextureName (othersprite.mGLTextureName), mSubSpriteCount (othersprite.mSubSpriteCount), mAnimation (othersprite.mAnimation), mAnimationFrameCount(othersprite.mAnimationFrameCount), mAnimationTimer(othersprite.mAnimationTimer), mAnimationFrameRate(othersprite.mAnimationFrameRate) {} Sprite& operator= (const Sprite &othersprite) { if (this != &othersprite) { mScale = othersprite.mScale; mWidth = othersprite.mWidth; mHeight = othersprite.mHeight; mGLTextureName = othersprite.mGLTextureName; mSubSpriteCount = othersprite.mSubSpriteCount; mAnimation = othersprite.mAnimation; mAnimationFrameCount= othersprite.mAnimationFrameCount; mAnimationTimer= othersprite.mAnimationTimer; mAnimationFrameRate= othersprite.mAnimationFrameRate; } return *this; } /** \brief Loads a Sprite from a .png file */ bool LoadFromPNG (const std::string &filename); /** \brief Draws a sprite with the center at the given position */ void DrawAt (float xpos, float ypos, float zpos); void DrawAt2D (float xpos, float ypos); unsigned int GetWidth() { return mWidth; }; unsigned int GetHeight() { return mHeight; }; void SetSize (unsigned int width, unsigned int height) { mWidth = width; mHeight = height; } void SetScale (float scale) { mScale = scale; }; /** \brief Marks the sprite as an animation and initializes its animation values */ void SetAnimation (int frame_count, float frame_rate) { mAnimation = true; mAnimationFrameCount = frame_count; mAnimationFrameRate = frame_rate; mAnimationTimer = 0.; mWidth = static_cast(ceil (static_cast (mWidth / mAnimationFrameCount))); } void ResetAnimation () { mAnimationTimer = 0.; } void UpdateAnimation (float seconds) { mAnimationTimer += seconds; while (mAnimationTimer >= mAnimationFrameCount / mAnimationFrameRate) { mAnimationTimer -= mAnimationFrameCount / mAnimationFrameRate; } } void SetGLTextureName (GLuint texture_name) { mGLTextureName = texture_name; } GLuint GetGLTextureName () { return mGLTextureName; } /** \brief Marks the Sprite as a collection of sprites * * All sprites have to be aligned in a row and the SubSprite * functionality eases retrieval and drawing of individual sprites */ void SetSubSpriteCount (const unsigned int count) { mSubSpriteCount = count; mWidth = static_cast(ceil (static_cast (mWidth / mSubSpriteCount))); } unsigned int GetSubSpriteCount () const { return mSubSpriteCount; } void DrawSubAt (unsigned int index, float xpos, float ypos, float zpos); private: float mScale; unsigned int mWidth; unsigned int mHeight; GLuint mGLTextureName; unsigned int mSubSpriteCount; bool mAnimation; int mAnimationFrameCount; float mAnimationTimer; float mAnimationFrameRate; }; typedef boost::shared_ptr SpritePtr; } #endif /* SPRITE_H */