From 39b61e709ca41804af30410f4a82a99f34900a29 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Sun, 24 Mar 2013 01:14:38 +0300 Subject: imporve simpleanimation class. --- src/simpleanimation.cpp | 50 ++++++++++++++++++++++--------------------------- src/simpleanimation.h | 2 +- 2 files changed, 23 insertions(+), 29 deletions(-) (limited to 'src') diff --git a/src/simpleanimation.cpp b/src/simpleanimation.cpp index 67c697348..91f491ffe 100644 --- a/src/simpleanimation.cpp +++ b/src/simpleanimation.cpp @@ -97,37 +97,31 @@ void SimpleAnimation::setFrame(int frame) if (frame < 0) frame = 0; - if (static_cast(frame) >= mAnimation->getLength()) - frame = static_cast(mAnimation->getLength()) - 1; + const unsigned int len = mAnimation->getLength(); + if (static_cast(frame) >= len) + frame = len - 1; mAnimationPhase = frame; - mCurrentFrame = &mAnimation->mFrames[mAnimationPhase]; + mCurrentFrame = &mAnimation->mFrames[frame]; } bool SimpleAnimation::update(const int timePassed) { - if (!mCurrentFrame || !mAnimation) + if (!mCurrentFrame || !mAnimation || !mInitialized || !mAnimation) return false; bool updated(false); - if (mInitialized && mAnimation) + mAnimationTime += timePassed; + + while (mAnimationTime > mCurrentFrame->delay && mCurrentFrame->delay > 0) { - mAnimationTime += timePassed; + updated = true; + mAnimationTime -= mCurrentFrame->delay; + mAnimationPhase++; - while (mAnimationTime > mCurrentFrame->delay - && mCurrentFrame->delay > 0) - { - updated = true; - mAnimationTime -= mCurrentFrame->delay; - mAnimationPhase++; + if (static_cast(mAnimationPhase) >= mAnimation->getLength()) + mAnimationPhase = 0; - if (static_cast(mAnimationPhase) - >= mAnimation->getLength()) - { - mAnimationPhase = 0; - } - - mCurrentFrame = &mAnimation->mFrames[mAnimationPhase]; - } + mCurrentFrame = &mAnimation->mFrames[mAnimationPhase]; } return updated; } @@ -149,15 +143,15 @@ Image *SimpleAnimation::getCurrentImage() const } void SimpleAnimation::initializeAnimation(const XmlNodePtr animationNode, - const std::string& dyePalettes) + const std::string &dyePalettes) { mInitialized = false; if (!animationNode) return; - std::string imagePath = XML::getProperty(animationNode, - "imageset", ""); + std::string imagePath = XML::getProperty( + animationNode, "imageset", ""); // Instanciate the dye coloration. if (!imagePath.empty() && !dyePalettes.empty()) @@ -172,17 +166,18 @@ void SimpleAnimation::initializeAnimation(const XmlNodePtr animationNode, if (!imageset) return; + const int x1 = imageset->getWidth() / 2 - 16; + const int y1 = imageset->getHeight() - 32; + // Get animation frames for (XmlNodePtr frameNode = animationNode->xmlChildrenNode; frameNode; frameNode = frameNode->next) { const int delay = XML::getIntProperty( frameNode, "delay", 0, 0, 100000); - int offsetX = XML::getProperty(frameNode, "offsetX", 0); - int offsetY = XML::getProperty(frameNode, "offsetY", 0); + const int offsetX = XML::getProperty(frameNode, "offsetX", 0) - x1; + const int offsetY = XML::getProperty(frameNode, "offsetY", 0) - y1; const int rand = XML::getIntProperty(frameNode, "rand", 100, 0, 100); - offsetY -= imageset->getHeight() - 32; - offsetX -= imageset->getWidth() / 2 - 16; if (xmlNameEqual(frameNode, "frame")) { @@ -238,6 +233,5 @@ void SimpleAnimation::initializeAnimation(const XmlNodePtr animationNode, } } -// imageset->decRef(); mInitialized = true; } diff --git a/src/simpleanimation.h b/src/simpleanimation.h index e30455a04..a0dc60a72 100644 --- a/src/simpleanimation.h +++ b/src/simpleanimation.h @@ -88,7 +88,7 @@ class SimpleAnimation final int mAnimationPhase; /** Current animation phase. */ - Frame *mCurrentFrame; + const Frame *mCurrentFrame; /** Tell whether the animation is ready */ bool mInitialized; -- cgit v1.2.3-70-g09d2