2010-04-05 23:38:59 +02:00
|
|
|
#ifndef SPRITE_H
|
|
|
|
#define SPRITE_H
|
|
|
|
|
|
|
|
#include <cmath>
|
2011-06-13 17:05:07 +02:00
|
|
|
#include <boost/shared_ptr.hpp>
|
2010-04-05 23:38:59 +02:00
|
|
|
|
|
|
|
namespace Engine {
|
|
|
|
|
2011-06-13 17:05:07 +02:00
|
|
|
/** \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.
|
|
|
|
*
|
|
|
|
*/
|
2010-04-05 23:38:59 +02:00
|
|
|
class Sprite {
|
|
|
|
public:
|
2011-06-13 17:05:07 +02:00
|
|
|
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;
|
2010-04-05 23:38:59 +02:00
|
|
|
}
|
|
|
|
|
2011-06-13 17:05:07 +02:00
|
|
|
/** \brief Loads a Sprite from a .png file */
|
2010-12-03 00:15:26 +01:00
|
|
|
bool LoadFromPNG (const std::string &filename);
|
2011-06-13 17:05:07 +02:00
|
|
|
/** \brief Draws a sprite with the center at the given position */
|
2010-04-05 23:38:59 +02:00
|
|
|
void DrawAt (float xpos, float ypos, float zpos);
|
|
|
|
void DrawAt2D (float xpos, float ypos);
|
|
|
|
unsigned int GetWidth() { return mWidth; };
|
|
|
|
unsigned int GetHeight() { return mHeight; };
|
|
|
|
|
2011-06-13 17:05:07 +02:00
|
|
|
void SetSize (unsigned int width, unsigned int height) {
|
|
|
|
mWidth = width;
|
|
|
|
mHeight = height;
|
|
|
|
}
|
2010-04-05 23:38:59 +02:00
|
|
|
void SetScale (float scale) { mScale = scale; };
|
|
|
|
|
2011-06-13 17:05:07 +02:00
|
|
|
/** \brief Marks the sprite as an animation and initializes its animation values */
|
2010-04-05 23:38:59 +02:00
|
|
|
void SetAnimation (int frame_count, float frame_rate) {
|
|
|
|
mAnimation = true;
|
|
|
|
mAnimationFrameCount = frame_count;
|
|
|
|
mAnimationFrameRate = frame_rate;
|
|
|
|
mAnimationTimer = 0.;
|
|
|
|
mWidth = static_cast<unsigned int>(ceil (static_cast<float> (mWidth / mAnimationFrameCount)));
|
|
|
|
}
|
|
|
|
void ResetAnimation () {
|
|
|
|
mAnimationTimer = 0.;
|
|
|
|
}
|
|
|
|
void UpdateAnimation (float seconds) {
|
|
|
|
mAnimationTimer += seconds;
|
|
|
|
while (mAnimationTimer >= mAnimationFrameCount / mAnimationFrameRate) {
|
|
|
|
mAnimationTimer -= mAnimationFrameCount / mAnimationFrameRate;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-06-13 17:05:07 +02:00
|
|
|
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
|
|
|
|
*/
|
2010-04-05 23:38:59 +02:00
|
|
|
void SetSubSpriteCount (const unsigned int count) {
|
|
|
|
mSubSpriteCount = count;
|
|
|
|
mWidth = static_cast<unsigned int>(ceil (static_cast<float> (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;
|
2011-06-13 17:05:07 +02:00
|
|
|
GLuint mGLTextureName;
|
2010-04-05 23:38:59 +02:00
|
|
|
|
|
|
|
unsigned int mSubSpriteCount;
|
|
|
|
|
|
|
|
bool mAnimation;
|
|
|
|
int mAnimationFrameCount;
|
|
|
|
float mAnimationTimer;
|
|
|
|
float mAnimationFrameRate;
|
|
|
|
};
|
|
|
|
|
2011-06-13 17:05:07 +02:00
|
|
|
typedef boost::shared_ptr<Sprite> SpritePtr;
|
|
|
|
|
2010-04-05 23:38:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
#endif /* SPRITE_H */
|