00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022 #include "graphics.h"
00023 #include "log.h"
00024 #include "simpleanimation.h"
00025
00026 #include "resources/animation.h"
00027 #include "resources/image.h"
00028 #include "resources/imageset.h"
00029 #include "resources/resourcemanager.h"
00030
00031 SimpleAnimation::SimpleAnimation(Animation *animation):
00032 mAnimation(animation),
00033 mAnimationTime(0),
00034 mAnimationPhase(0),
00035 mCurrentFrame(mAnimation->getFrame(0))
00036 {
00037 }
00038
00039 SimpleAnimation::SimpleAnimation(xmlNodePtr animationNode):
00040 mAnimationTime(0),
00041 mAnimationPhase(0)
00042 {
00043 mAnimation = new Animation;
00044
00045 ImageSet *imageset = ResourceManager::getInstance()->getImageSet(
00046 XML::getProperty(animationNode, "imageset", ""),
00047 XML::getProperty(animationNode, "width", 0),
00048 XML::getProperty(animationNode, "height", 0)
00049 );
00050
00051
00052 for ( xmlNodePtr frameNode = animationNode->xmlChildrenNode;
00053 frameNode;
00054 frameNode = frameNode->next)
00055 {
00056 int delay = XML::getProperty(frameNode, "delay", 0);
00057 int offsetX = XML::getProperty(frameNode, "offsetX", 0);
00058 int offsetY = XML::getProperty(frameNode, "offsetY", 0);
00059 offsetY -= imageset->getHeight() - 32;
00060 offsetX -= imageset->getWidth() / 2 - 16;
00061
00062 if (xmlStrEqual(frameNode->name, BAD_CAST "frame"))
00063 {
00064 int index = XML::getProperty(frameNode, "index", -1);
00065
00066 if (index < 0)
00067 {
00068 logger->log("No valid value for 'index'");
00069 continue;
00070 }
00071
00072 Image *img = imageset->get(index);
00073
00074 if (!img)
00075 {
00076 logger->log("No image at index %d", index);
00077 continue;
00078 }
00079
00080 mAnimation->addFrame(img, delay, offsetX, offsetY);
00081 }
00082 else if (xmlStrEqual(frameNode->name, BAD_CAST "sequence"))
00083 {
00084 int start = XML::getProperty(frameNode, "start", -1);
00085 int end = XML::getProperty(frameNode, "end", -1);
00086
00087 if (start < 0 || end < 0)
00088 {
00089 logger->log("No valid value for 'start' or 'end'");
00090 continue;
00091 }
00092
00093 while (end >= start)
00094 {
00095 Image *img = imageset->get(start);
00096
00097 if (!img)
00098 {
00099 logger->log("No image at index %d", start);
00100 continue;
00101 }
00102
00103 mAnimation->addFrame(img, delay, offsetX, offsetY);
00104 start++;
00105 }
00106 }
00107 else if (xmlStrEqual(frameNode->name, BAD_CAST "end"))
00108 {
00109 mAnimation->addTerminator();
00110 }
00111 }
00112
00113 mCurrentFrame = mAnimation->getFrame(0);
00114 }
00115
00116 bool SimpleAnimation::draw(Graphics* graphics, int posX, int posY) const
00117 {
00118 if (!mCurrentFrame || !mCurrentFrame->image)
00119 return false;
00120
00121 return graphics->drawImage(mCurrentFrame->image,
00122 posX + mCurrentFrame->offsetX,
00123 posY + mCurrentFrame->offsetY);
00124 }
00125
00126 void SimpleAnimation::reset()
00127 {
00128 mAnimationTime = 0;
00129 mAnimationPhase = 0;
00130 }
00131
00132 void SimpleAnimation::update(unsigned int timePassed)
00133 {
00134 mAnimationTime += timePassed;
00135
00136 while (mAnimationTime > mCurrentFrame->delay && mCurrentFrame->delay > 0)
00137 {
00138 mAnimationTime -= mCurrentFrame->delay;
00139 mAnimationPhase++;
00140
00141 if (mAnimationPhase >= mAnimation->getLength())
00142 mAnimationPhase = 0;
00143
00144 mCurrentFrame = mAnimation->getFrame(mAnimationPhase);
00145 }
00146 }
00147
00148 Image *SimpleAnimation::getCurrentImage() const
00149 {
00150 return mCurrentFrame->image;
00151 }
00152
00153 SimpleAnimation::~SimpleAnimation()
00154 {
00155 delete mAnimation;
00156 }