summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrei Karas <akaras@inbox.ru>2013-03-24 01:14:38 +0300
committerAndrei Karas <akaras@inbox.ru>2013-03-24 01:26:46 +0300
commit39b61e709ca41804af30410f4a82a99f34900a29 (patch)
treed5fc07f014924feab1a5992d5a83ce697b2b13a2
parent1e37ebb4eb808f56bbbd522dbab524c24439d92b (diff)
downloadplus-39b61e709ca41804af30410f4a82a99f34900a29.tar.gz
plus-39b61e709ca41804af30410f4a82a99f34900a29.tar.bz2
plus-39b61e709ca41804af30410f4a82a99f34900a29.tar.xz
plus-39b61e709ca41804af30410f4a82a99f34900a29.zip
imporve simpleanimation class.
-rw-r--r--src/simpleanimation.cpp50
-rw-r--r--src/simpleanimation.h2
2 files changed, 23 insertions, 29 deletions
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<unsigned>(frame) >= mAnimation->getLength())
- frame = static_cast<int>(mAnimation->getLength()) - 1;
+ const unsigned int len = mAnimation->getLength();
+ if (static_cast<unsigned>(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<unsigned>(mAnimationPhase) >= mAnimation->getLength())
+ mAnimationPhase = 0;
- if (static_cast<unsigned>(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;