Web   ·   Wiki   ·   Activities   ·   Blog   ·   Lists   ·   Chat   ·   Meeting   ·   Bugs   ·   Git   ·   Translate   ·   Archive   ·   People   ·   Donate
summaryrefslogtreecommitdiffstats
path: root/src/CSprite.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'src/CSprite.cpp')
-rwxr-xr-xsrc/CSprite.cpp177
1 files changed, 177 insertions, 0 deletions
diff --git a/src/CSprite.cpp b/src/CSprite.cpp
new file mode 100755
index 0000000..2814bd0
--- /dev/null
+++ b/src/CSprite.cpp
@@ -0,0 +1,177 @@
+#include "CSprite.h"
+
+CSprite::CSprite(void)
+{
+ mOldX = 0;
+ mOldY = 0;
+ mFrame = 0;
+ mLastUpdate = 0;
+ mDrawn = false;
+ mParticle = new CParticle(0, 0, 0, 0, 0, 0);
+ mStartFrame = 0;
+ mEndFrame = 0;
+ mIsLooping = false;
+}
+
+CSprite::CSprite(CSpriteAnimation *aSpriteAnimation, CSurface *aScreen)
+{
+ mOldX = 0;
+ mOldY = 0;
+ mFrame = 0;
+ mLastUpdate = 0;
+ mDrawn = false;
+ mParticle = new CParticle(0, 0, 0, 0, 0, 0);
+ mStartFrame = 0;
+ mEndFrame = 0;
+ mIsLooping = false;
+
+ mSpriteAnimation = aSpriteAnimation;
+ mScreen = aScreen;
+
+ if (mSpriteAnimation != NULL)
+ {
+ // Create the background image.
+ mBackground = new CSurface(mSpriteAnimation->getWidth(), mSpriteAnimation->getHeight());
+
+ if (mSpriteAnimation->getNumFrames() > 1)
+ mAnimating = true;
+ else
+ mAnimating = false;
+ }
+ else
+ {
+ fprintf(stderr, "Sprite constructor failed!\n");
+ fprintf(stderr, "Sprite animation is not built!\n");
+ }
+}
+
+CSprite::~CSprite(void)
+{
+ destroy();
+}
+
+void CSprite::destroy(void)
+{
+ if (mBackground != NULL)
+ {
+ mBackground->destroy();
+ mBackground = NULL;
+ }
+
+ if (mParticle != NULL)
+ {
+ delete mParticle;
+ mParticle = NULL;
+ }
+}
+
+// Restore the background.
+void CSprite::restoreBackground(void)
+{
+ // If the sprite has been drawn once before, then we can clear the screen.
+ // If we do not do this, the first time the backgrouns is restored and this results in a black rectangle.
+ if (mDrawn)
+ {
+ CSurface::drawImage(mScreen, mBackground, mOldX, mOldY);
+ }
+}
+
+// Save the background.
+void CSprite::saveBackground(void)
+{
+ CSurface::drawImage(mBackground, mScreen, 0, 0, getIntX()+mOffsetX, getIntY()+mOffsetY, mSpriteAnimation->getWidth(), mSpriteAnimation->getHeight());
+ mOldX = getIntX()+mOffsetX;
+ mOldY = getIntY()+mOffsetY;
+}
+
+// Draw the sprite in the screen.
+void CSprite::draw(void)
+{
+ CSurface::drawImage(mScreen, mSpriteAnimation->getFrame(mFrame)->getImage(), getIntX()+mOffsetX, getIntY()+mOffsetY);
+
+ if (!mDrawn)
+ mDrawn = true;
+}
+
+void CSprite::update(void)
+{
+ if (mAnimating)
+ {
+ if (mLastUpdate + mSpriteAnimation->getFrame(mFrame)->getFrameTime() < SDL_GetTicks())
+ {
+ mFrame++;
+ if (mFrame > mEndFrame)
+ {
+ if (mIsLooping)
+ {
+ mFrame = mStartFrame;
+ mAnimating = true;
+ }
+ else
+ {
+ mFrame = mEndFrame;
+ mAnimating = false;
+ }
+ }
+ mLastUpdate = SDL_GetTicks();
+ }
+ }
+
+ mParticle->update();
+
+// fprintf(stdout, "mAnimating = %d.\n", mAnimating);
+// fprintf(stdout, "mFrame = %d.\n", mFrame);
+}
+
+void CSprite::render(void)
+{
+ restoreBackground();
+ saveBackground();
+ draw();
+}
+
+int CSprite::getIntX(void)
+{
+ return (int) floor(mParticle->getX());
+}
+
+int CSprite::getIntY(void)
+{
+ return (int) floor(mParticle->getY());
+}
+
+void CSprite::setXVel(float aXVel)
+{
+ mParticle->setXVel(aXVel);
+}
+
+void CSprite::setYVel(float aYVel)
+{
+ mParticle->setYVel(aYVel);
+}
+
+void CSprite::setAnim(int aStartFrame, int aEndFrame, bool aIsLooping)
+{
+ if (aStartFrame == aEndFrame)
+ mAnimating = false;
+ else
+ mAnimating = true;
+
+ mIsLooping = aIsLooping;
+ mStartFrame = aStartFrame;
+ mEndFrame = aEndFrame;
+
+ mFrame = mStartFrame;
+ mLastUpdate = SDL_GetTicks();
+}
+
+void CSprite::setRenderOffsets(int aOffsetX, int aOffsetY)
+{
+ mOffsetX = aOffsetX;
+ mOffsetY = aOffsetY;
+}
+
+void CSprite::setXY(int aX, int aY)
+{
+ mParticle->setXY(aX, aY);
+}