From 98c10803f79ce9decb4a431719e18bb27aac4485 Mon Sep 17 00:00:00 2001 From: Andrei Karas Date: Sun, 3 Jun 2012 20:06:08 +0300 Subject: Improve a bit animation speed. --- src/animatedsprite.cpp | 14 ++++++++------ src/particleemitter.cpp | 4 ++-- src/resources/animation.h | 27 +++++++-------------------- src/resources/mapreader.cpp | 2 +- src/simpleanimation.cpp | 8 ++++---- 5 files changed, 22 insertions(+), 33 deletions(-) (limited to 'src') diff --git a/src/animatedsprite.cpp b/src/animatedsprite.cpp index 9bf7018a3..1014fbe46 100644 --- a/src/animatedsprite.cpp +++ b/src/animatedsprite.cpp @@ -85,7 +85,7 @@ bool AnimatedSprite::reset() mLastTime = 0; if (mAnimation) - mFrame = mAnimation->getFrame(0); + mFrame = &mAnimation->mFrames[0]; else mFrame = nullptr; return ret; @@ -142,7 +142,8 @@ bool AnimatedSprite::update(int time) bool AnimatedSprite::updateCurrentAnimation(unsigned int time) { - if (!mFrame || !mAnimation || Animation::isTerminator(*mFrame)) + // move code from Animation::isTerminator(*mFrame) + if (!mFrame || !mAnimation || (!mFrame->image && mFrame->type == Frame::ANIMATION)) return false; mFrameTime += time; @@ -158,7 +159,7 @@ bool AnimatedSprite::updateCurrentAnimation(unsigned int time) if (mFrameIndex >= mAnimation->getLength()) mFrameIndex = 0; - mFrame = mAnimation->getFrame(mFrameIndex); + mFrame = &mAnimation->mFrames[mFrameIndex]; if (!mFrame || (mFrame->type == Frame::LABEL && !mFrame->nextAction.empty())) { @@ -170,7 +171,7 @@ bool AnimatedSprite::updateCurrentAnimation(unsigned int time) { for (unsigned i = 0; i < mAnimation->getLength(); i ++) { - Frame *frame = mAnimation->getFrame(i); + Frame *frame = &mAnimation->mFrames[i]; if (frame->type == Frame::LABEL && mFrame->nextAction == frame->nextAction) { @@ -179,7 +180,7 @@ bool AnimatedSprite::updateCurrentAnimation(unsigned int time) if (mFrameIndex >= mAnimation->getLength()) mFrameIndex = 0; - mFrame = mAnimation->getFrame(mFrameIndex); + mFrame = &mAnimation->mFrames[mFrameIndex]; fail = true; break; @@ -195,7 +196,8 @@ bool AnimatedSprite::updateCurrentAnimation(unsigned int time) return true; } } - else if (Animation::isTerminator(*mFrame)) + // copy code from Animation::isTerminator(*mFrame) + else if (!mFrame->image && mFrame->type == Frame::ANIMATION) { if (mFrame->rand == 100 || rand() % 100 <= mFrame->rand) { diff --git a/src/particleemitter.cpp b/src/particleemitter.cpp index 5fefd3609..27eb89291 100644 --- a/src/particleemitter.cpp +++ b/src/particleemitter.cpp @@ -527,12 +527,12 @@ std::list ParticleEmitter::createParticles(int tick) newParticle = new ImageParticle(mMap, mParticleImage); } - else if (!mParticleRotation.empty()) + else if (!mParticleRotation.mFrames.empty()) { Animation *newAnimation = new Animation(mParticleRotation); newParticle = new RotationalParticle(mMap, newAnimation); } - else if (!mParticleAnimation.empty()) + else if (!mParticleAnimation.mFrames.empty()) { Animation *newAnimation = new Animation(mParticleAnimation); newParticle = new AnimationParticle(mMap, newAnimation); diff --git a/src/resources/animation.h b/src/resources/animation.h index 1d23b1608..ffa717f8c 100644 --- a/src/resources/animation.h +++ b/src/resources/animation.h @@ -58,6 +58,10 @@ struct Frame */ class Animation { + friend class AnimatedSprite; + friend class ParticleEmitter; + friend class SimpleAnimation; + public: Animation(); @@ -73,29 +77,11 @@ class Animation */ void addTerminator(int rand); - /** - * Returns the frame at the specified index. - */ - Frame *getFrame(int index) - { return &(mFrames[index]); } - /** * Returns the length of this animation in frames. */ - unsigned int getLength() const - { return static_cast(mFrames.size()); } - - /** - * Check for empty animation. - */ - bool empty() const - { return mFrames.empty(); } - - /** - * Returns the duration of this animation. - */ - int getDuration() const - { return mDuration; } + size_t getLength() const + { return mFrames.size(); } void addJump(std::string name, int rand); @@ -113,6 +99,7 @@ class Animation static bool isTerminator(const Frame &phase); protected: + typedef std::vector Frames; typedef Frames::iterator FramesIter; typedef Frames::reverse_iterator FramesRevIter; diff --git a/src/resources/mapreader.cpp b/src/resources/mapreader.cpp index 3f1ef1239..fd04f015f 100644 --- a/src/resources/mapreader.cpp +++ b/src/resources/mapreader.cpp @@ -777,7 +777,7 @@ Tileset *MapReader::readTileset(XmlNodePtr node, const std::string &path, if (ani->getLength() > 0) { map->addAnimation(tileGID, new TileAnimation(ani)); - logger->log("Animation length: %d", ani->getLength()); +// logger->log("Animation length: %d", ani->getLength()); } else { diff --git a/src/simpleanimation.cpp b/src/simpleanimation.cpp index 89431a916..483061516 100644 --- a/src/simpleanimation.cpp +++ b/src/simpleanimation.cpp @@ -39,7 +39,7 @@ SimpleAnimation::SimpleAnimation(Animation *animation): mAnimation(animation), mAnimationTime(0), mAnimationPhase(0), - mCurrentFrame(mAnimation->getFrame(0)), + mCurrentFrame(&mAnimation->mFrames[0]), mInitialized(true) { } @@ -53,7 +53,7 @@ SimpleAnimation::SimpleAnimation(XmlNodePtr animationNode, { initializeAnimation(animationNode, dyePalettes); if (mAnimation) - mCurrentFrame = mAnimation->getFrame(0); + mCurrentFrame = &mAnimation->mFrames[0]; else mCurrentFrame = nullptr; } @@ -90,7 +90,7 @@ void SimpleAnimation::setFrame(int frame) if (static_cast(frame) >= mAnimation->getLength()) frame = mAnimation->getLength() - 1; mAnimationPhase = frame; - mCurrentFrame = mAnimation->getFrame(mAnimationPhase); + mCurrentFrame = &mAnimation->mFrames[mAnimationPhase]; } bool SimpleAnimation::update(int timePassed) @@ -116,7 +116,7 @@ bool SimpleAnimation::update(int timePassed) mAnimationPhase = 0; } - mCurrentFrame = mAnimation->getFrame(mAnimationPhase); + mCurrentFrame = &mAnimation->mFrames[mAnimationPhase]; } } return updated; -- cgit v1.2.3-60-g2f50