summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/simpleanimation.cpp48
-rw-r--r--src/simpleanimation.h3
2 files changed, 38 insertions, 13 deletions
diff --git a/src/simpleanimation.cpp b/src/simpleanimation.cpp
index a8d33a43..7ef433ea 100644
--- a/src/simpleanimation.cpp
+++ b/src/simpleanimation.cpp
@@ -33,14 +33,16 @@ SimpleAnimation::SimpleAnimation(Animation *animation):
mAnimation(animation),
mAnimationTime(0),
mAnimationPhase(0),
- mCurrentFrame(mAnimation->getFrame(0))
+ mCurrentFrame(mAnimation->getFrame(0)),
+ mInitialized(true)
{
}
SimpleAnimation::SimpleAnimation(xmlNodePtr animationNode):
mAnimation(new Animation),
mAnimationTime(0),
- mAnimationPhase(0)
+ mAnimationPhase(0),
+ mInitialized(false)
{
initializeAnimation(animationNode);
mCurrentFrame = mAnimation->getFrame(0);
@@ -48,7 +50,8 @@ SimpleAnimation::SimpleAnimation(xmlNodePtr animationNode):
SimpleAnimation::~SimpleAnimation()
{
- delete mAnimation;
+ if (mAnimation)
+ delete mAnimation;
}
bool SimpleAnimation::draw(Graphics *graphics, int posX, int posY) const
@@ -79,38 +82,55 @@ void SimpleAnimation::setFrame(int frame)
void SimpleAnimation::update(int timePassed)
{
- mAnimationTime += timePassed;
-
- while (mAnimationTime > mCurrentFrame->delay && mCurrentFrame->delay > 0)
+ if (mInitialized)
{
- mAnimationTime -= mCurrentFrame->delay;
- mAnimationPhase++;
+ mAnimationTime += timePassed;
- if (mAnimationPhase >= mAnimation->getLength())
- mAnimationPhase = 0;
+ while (mAnimationTime > mCurrentFrame->delay && mCurrentFrame->delay > 0)
+ {
+ mAnimationTime -= mCurrentFrame->delay;
+ mAnimationPhase++;
- mCurrentFrame = mAnimation->getFrame(mAnimationPhase);
+ if (mAnimationPhase >= mAnimation->getLength())
+ mAnimationPhase = 0;
+
+ mCurrentFrame = mAnimation->getFrame(mAnimationPhase);
+ }
}
}
int SimpleAnimation::getLength() const
{
- return mAnimation->getLength();
+ if (mAnimation)
+ return mAnimation->getLength();
+ else
+ return 0;
}
Image *SimpleAnimation::getCurrentImage() const
{
- return mCurrentFrame->image;
+ if (mCurrentFrame)
+ return mCurrentFrame->image;
+ else
+ return NULL;
}
void SimpleAnimation::initializeAnimation(xmlNodePtr animationNode)
{
+ mInitialized = false;
+
+ if (!animationNode)
+ return;
+
ImageSet *imageset = ResourceManager::getInstance()->getImageSet(
XML::getProperty(animationNode, "imageset", ""),
XML::getProperty(animationNode, "width", 0),
XML::getProperty(animationNode, "height", 0)
);
+ if (!imageset)
+ return;
+
// Get animation frames
for ( xmlNodePtr frameNode = animationNode->xmlChildrenNode;
frameNode;
@@ -172,4 +192,6 @@ void SimpleAnimation::initializeAnimation(xmlNodePtr animationNode)
mAnimation->addTerminator();
}
}
+
+ mInitialized = true;
}
diff --git a/src/simpleanimation.h b/src/simpleanimation.h
index a8a43b33..e679442e 100644
--- a/src/simpleanimation.h
+++ b/src/simpleanimation.h
@@ -78,6 +78,9 @@ class SimpleAnimation
/** Current animation phase. */
Frame *mCurrentFrame;
+
+ /** Tell whether the animation is ready */
+ bool mInitialized;
};
#endif