diff options
Diffstat (limited to 'src/simpleanimation.cpp')
-rw-r--r-- | src/simpleanimation.cpp | 77 |
1 files changed, 57 insertions, 20 deletions
diff --git a/src/simpleanimation.cpp b/src/simpleanimation.cpp index a8d33a43..c6dc1efe 100644 --- a/src/simpleanimation.cpp +++ b/src/simpleanimation.cpp @@ -21,10 +21,12 @@ #include "simpleanimation.h" +#include "game.h" #include "graphics.h" #include "log.h" #include "resources/animation.h" +#include "resources/dye.h" #include "resources/image.h" #include "resources/imageset.h" #include "resources/resourcemanager.h" @@ -33,22 +35,26 @@ SimpleAnimation::SimpleAnimation(Animation *animation): mAnimation(animation), mAnimationTime(0), mAnimationPhase(0), - mCurrentFrame(mAnimation->getFrame(0)) + mCurrentFrame(mAnimation->getFrame(0)), + mInitialized(true) { } -SimpleAnimation::SimpleAnimation(xmlNodePtr animationNode): +SimpleAnimation::SimpleAnimation(xmlNodePtr animationNode, + const std::string& dyePalettes): mAnimation(new Animation), mAnimationTime(0), - mAnimationPhase(0) + mAnimationPhase(0), + mInitialized(false) { - initializeAnimation(animationNode); + initializeAnimation(animationNode, dyePalettes); mCurrentFrame = mAnimation->getFrame(0); } SimpleAnimation::~SimpleAnimation() { - delete mAnimation; + if (mAnimation) + delete mAnimation; } bool SimpleAnimation::draw(Graphics *graphics, int posX, int posY) const @@ -79,48 +85,77 @@ 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; + + while (mAnimationTime > mCurrentFrame->delay && mCurrentFrame->delay > 0) + { + mAnimationTime -= mCurrentFrame->delay; + mAnimationPhase++; - if (mAnimationPhase >= mAnimation->getLength()) - mAnimationPhase = 0; + if (mAnimationPhase >= mAnimation->getLength()) + mAnimationPhase = 0; - mCurrentFrame = mAnimation->getFrame(mAnimationPhase); + 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) +void SimpleAnimation::initializeAnimation(xmlNodePtr animationNode, + const std::string& dyePalettes) { + mInitialized = false; + + if (!animationNode) + return; + + std::string imagePath = XML::getProperty(animationNode, + "imageset", ""); + + // Instanciate the dye coloration. + if (!imagePath.empty() && !dyePalettes.empty()) + Dye::instantiate(imagePath, dyePalettes); + 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; + for (xmlNodePtr frameNode = animationNode->xmlChildrenNode; frameNode; frameNode = frameNode->next) { int delay = XML::getProperty(frameNode, "delay", 0); int offsetX = XML::getProperty(frameNode, "offsetX", 0); int offsetY = XML::getProperty(frameNode, "offsetY", 0); - offsetY -= imageset->getHeight() - 32; - offsetX -= imageset->getWidth() / 2 - 16; + Game *game = Game::instance(); + if (game) + { + offsetX -= imageset->getWidth() / 2 + - game->getCurrentTileWidth() / 2; + offsetY -= imageset->getHeight() - game->getCurrentTileHeight(); + } if (xmlStrEqual(frameNode->name, BAD_CAST "frame")) { @@ -172,4 +207,6 @@ void SimpleAnimation::initializeAnimation(xmlNodePtr animationNode) mAnimation->addTerminator(); } } + + mInitialized = true; } |