summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--ChangeLog2
-rw-r--r--src/animatedsprite.cpp135
-rw-r--r--src/animatedsprite.h1
-rw-r--r--src/animation.cpp35
4 files changed, 64 insertions, 109 deletions
diff --git a/ChangeLog b/ChangeLog
index 32dbd9f3..be529017 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,7 @@
2006-07-29 Björn Steinbrink <B.Steinbrink@gmx.de>
+ * src/animatedsprite.h, src/animatedsprite.cpp, src/animation.cpp: A
+ bunch of cleanups.
* src/animation.h, src/animatedsprite.cpp: Moved included from header
to source file.
diff --git a/src/animatedsprite.cpp b/src/animatedsprite.cpp
index c3862e90..25525a69 100644
--- a/src/animatedsprite.cpp
+++ b/src/animatedsprite.cpp
@@ -47,29 +47,22 @@ AnimatedSprite::AnimatedSprite(const std::string& animationFile, int variant):
xmlDocPtr doc = xmlParseMemory(data, size);
free(data);
- if (!doc)
- {
+ if (!doc) {
logger->error("Animation: Error while parsing animation definition file!");
- return;
}
xmlNodePtr node = xmlDocGetRootElement(doc);
- if (!node || !xmlStrEqual(node->name, BAD_CAST "sprite"))
- {
+ if (!node || !xmlStrEqual(node->name, BAD_CAST "sprite")) {
logger->error("Animation: this is not a valid animation definition file!");
- return;
}
// Get the variant
int variant_num = getProperty(node, "variants", 0);
int variant_offset = getProperty(node, "variant_offset", 0);
- if (variant_num > 0 && variant < variant_num )
- {
+ if (variant_num > 0 && variant < variant_num ) {
variant_offset *= variant;
- }
- else
- {
+ } else {
variant_offset = 0;
}
@@ -85,14 +78,11 @@ AnimatedSprite::AnimatedSprite(const std::string& animationFile, int variant):
Spriteset *spriteset =
resman->getSpriteset(imageSrc, width, height);
- if (!spriteset)
- {
+ if (!spriteset) {
logger->error("Couldn't load spriteset!");
}
- else
- {
- mSpritesets[name] = spriteset;
- }
+
+ mSpritesets[name] = spriteset;
}
// get action
else if (xmlStrEqual(node->name, BAD_CAST "action"))
@@ -100,30 +90,25 @@ AnimatedSprite::AnimatedSprite(const std::string& animationFile, int variant):
std::string name = getProperty(node, "name", "");
std::string imageset = getProperty(node, "imageset", "");
- if (name.length() == 0)
+ if (name.empty())
{
logger->log("Warning: unnamed action in %s",
animationFile.c_str());
}
-
- Action *action = new Action();
-
- if (mSpritesets.find(imageset) != mSpritesets.end())
- {
- action->setSpriteset(mSpritesets[imageset]);
- mActions[name] = action;
- }
- else
- {
+ if (mSpritesets.find(imageset) == mSpritesets.end()) {
logger->log("Warning: imageset \"%s\" not defined in %s",
imageset.c_str(),
animationFile.c_str());
- // Discard action and skip loading animations
- delete action;
+ // skip loading animations
continue;
}
+ Action *action = new Action();
+
+ action->setSpriteset(mSpritesets[imageset]);
+ mActions[name] = action;
+
// get animations
for (xmlNodePtr animationNode = node->xmlChildrenNode;
animationNode != NULL;
@@ -149,8 +134,8 @@ AnimatedSprite::AnimatedSprite(const std::string& animationFile, int variant):
int offsetX = getProperty(phaseNode, "offsetX", 0);
int offsetY = getProperty(phaseNode, "offsetY", 0);
- offsetY = offsetY - mSpritesets[imageset]->getHeight() + 32;
- offsetX = offsetX - mSpritesets[imageset]->getWidth() / 2 + 16;
+ offsetY -= mSpritesets[imageset]->getHeight() - 32;
+ offsetX -= mSpritesets[imageset]->getWidth() / 2 - 16;
animation->addPhase(index + variant_offset, delay,
offsetX, offsetY);
}
@@ -158,8 +143,8 @@ AnimatedSprite::AnimatedSprite(const std::string& animationFile, int variant):
{
int start = getProperty(phaseNode, "start", 0);
int end = getProperty(phaseNode, "end", 0);
- int offsetY = 0 - mSpritesets[imageset]->getHeight() + 32;
- int offsetX = 0 - mSpritesets[imageset]->getWidth() / 2 + 16;
+ int offsetY = -mSpritesets[imageset]->getHeight() + 32;
+ int offsetX = -mSpritesets[imageset]->getWidth() / 2 + 16;
while (end >= start)
{
animation->addPhase(start + variant_offset,
@@ -198,15 +183,15 @@ AnimatedSprite::AnimatedSprite(const std::string& animationFile, int variant):
int
AnimatedSprite::getProperty(xmlNodePtr node, const char* name, int def)
{
+ int &ret = def;
+
xmlChar *prop = xmlGetProp(node, BAD_CAST name);
if (prop) {
- int val = atoi((char*)prop);
+ ret = atoi((char*)prop);
xmlFree(prop);
- return val;
- }
- else {
- return def;
}
+
+ return ret;
}
std::string
@@ -219,9 +204,8 @@ AnimatedSprite::getProperty(xmlNodePtr node, const char* name,
xmlFree(prop);
return val;
}
- else {
- return def;
- }
+
+ return def;
}
void
@@ -246,19 +230,18 @@ AnimatedSprite::~AnimatedSprite()
void
AnimatedSprite::play(const std::string& action)
{
- Actions::iterator iAction;
- iAction = mActions.find(action);
+ ActionIterator i = mActions.find(action);
- if (iAction == mActions.end())
+ if (i == mActions.end())
{
logger->log("Warning: no action \"%s\" defined!", action.c_str());
mAction = NULL;
return;
}
- if (mAction != iAction->second)
+ if (mAction != i->second)
{
- mAction = iAction->second;
+ mAction = i->second;
mLastTime = 0;
}
@@ -282,60 +265,44 @@ void
AnimatedSprite::update(int time)
{
// Avoid freaking out at first frame or when tick_time overflows
- if (time < mLastTime || mLastTime == 0) mLastTime = time;
+ if (time < mLastTime || mLastTime == 0)
+ mLastTime = time;
// If not enough time have passed yet, do nothing
- if (time > mLastTime)
+ if (time > mLastTime && mAction)
{
- if (mAction != NULL)
- {
- Animation *animation = mAction->getAnimation(mDirection);
- animation->update((unsigned int)((time - mLastTime) * mSpeed));
- mLastTime = time;
- }
+ Animation *animation = mAction->getAnimation(mDirection);
+ animation->update((unsigned int)((time - mLastTime) * mSpeed));
+ mLastTime = time;
}
}
bool
AnimatedSprite::draw(Graphics* graphics, Sint32 posX, Sint32 posY) const
{
- if (mAction != NULL)
- {
- Animation *animation = mAction->getAnimation(mDirection);
-
- if (animation->getCurrentPhase() >= 0)
- {
- Spriteset *spriteset = mAction->getSpriteset();
- Image *image = spriteset->get(animation->getCurrentPhase());
- Sint32 offsetX = animation->getOffsetX();
- Sint32 offsetY = animation->getOffsetY();
- return graphics->drawImage(image, posX + offsetX, posY + offsetY);
- }
- }
-
- return false;
+ if (!mAction)
+ return false;
+
+ Animation *animation = mAction->getAnimation(mDirection);
+ int phase = animation->getCurrentPhase();
+ if (phase < 0)
+ return false;
+
+ Spriteset *spriteset = mAction->getSpriteset();
+ Image *image = spriteset->get(phase);
+ Sint32 offsetX = animation->getOffsetX();
+ Sint32 offsetY = animation->getOffsetY();
+ return graphics->drawImage(image, posX + offsetX, posY + offsetY);
}
int
AnimatedSprite::getWidth() const
{
- if (mAction != NULL)
- {
- Spriteset *spriteset = mAction->getSpriteset();
- return spriteset->getWidth();
- }
-
- return 0;
+ return mAction ? mAction->getSpriteset()->getWidth() : 0;
}
int
AnimatedSprite::getHeight() const
{
- if (mAction != NULL)
- {
- Spriteset *spriteset = mAction->getSpriteset();
- return spriteset->getHeight();
- }
-
- return 0;
+ return mAction ? mAction->getSpriteset()->getHeight() : 0;
}
diff --git a/src/animatedsprite.h b/src/animatedsprite.h
index cea0d876..32466de8 100644
--- a/src/animatedsprite.h
+++ b/src/animatedsprite.h
@@ -109,6 +109,7 @@ class AnimatedSprite
typedef Spritesets::iterator SpritesetIterator;
Spritesets mSpritesets;
typedef std::map<std::string, Action*> Actions;
+ typedef Actions::iterator ActionIterator;
Actions mActions;
Action *mAction;
std::string mDirection;
diff --git a/src/animation.cpp b/src/animation.cpp
index f9a5f6bb..cd716d78 100644
--- a/src/animation.cpp
+++ b/src/animation.cpp
@@ -59,25 +59,15 @@ Animation::update(unsigned int time)
int
Animation::getCurrentPhase() const
{
- if (mAnimationPhases.empty())
- {
- return -1;
- }
- else
- {
- return iCurrentPhase->image;
- }
+ return mAnimationPhases.empty() ? -1 : iCurrentPhase->image;
}
void
Animation::addPhase(int image, unsigned int delay, int offsetX, int offsetY)
{
//add new phase to animation list
- AnimationPhase newPhase;
- newPhase.image = image;
- newPhase.delay = delay;
- newPhase.offsetX = offsetX;
- newPhase.offsetY = offsetY;
+ AnimationPhase newPhase = { image, delay, offsetX, offsetY };
+
mAnimationPhases.push_back(newPhase);
//reset animation circle
iCurrentPhase = mAnimationPhases.begin();
@@ -86,14 +76,15 @@ Animation::addPhase(int image, unsigned int delay, int offsetX, int offsetY)
int
Animation::getLength()
{
+ if (mAnimationPhases.empty())
+ return 0;
+
+
std::list<AnimationPhase>::iterator i;
int length = 0;
- if (!mAnimationPhases.empty())
+ for (i = mAnimationPhases.begin(); i != mAnimationPhases.end(); i++)
{
- for (i = mAnimationPhases.begin(); i != mAnimationPhases.end(); i++)
- {
- length += (*i).delay;
- }
+ length += i->delay;
}
return length;
}
@@ -115,7 +106,6 @@ Action::~Action()
Animation*
Action::getAnimation(const std::string& direction) const
{
- Animation *animation = NULL;
Animations::const_iterator i = mAnimations.find(direction);
// When the direction isn't defined, try the default
@@ -124,12 +114,7 @@ Action::getAnimation(const std::string& direction) const
i = mAnimations.find("default");
}
- if (i != mAnimations.end())
- {
- animation = i->second;
- }
-
- return animation;
+ return (i == mAnimations.end()) ? NULL : i->second;
}
void