diff options
author | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2006-12-12 15:06:06 +0000 |
---|---|---|
committer | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2006-12-12 15:06:06 +0000 |
commit | 7467afb637960fc8f3c8e631b95bb3e662285057 (patch) | |
tree | 34ba6896c8795ab3c1e6bb7fccbce27a5dd491ed /src/resources | |
parent | f4f6c66a1ebe115dde9dc7a9b793b36ddc708d3c (diff) | |
download | mana-client-7467afb637960fc8f3c8e631b95bb3e662285057.tar.gz mana-client-7467afb637960fc8f3c8e631b95bb3e662285057.tar.bz2 mana-client-7467afb637960fc8f3c8e631b95bb3e662285057.tar.xz mana-client-7467afb637960fc8f3c8e631b95bb3e662285057.zip |
Moved Action, Animation and Frame (renamed from AnimationPhase) classes into
the resources directory, since they are part of the SpriteDef class.
Diffstat (limited to 'src/resources')
-rw-r--r-- | src/resources/action.cpp | 67 | ||||
-rw-r--r-- | src/resources/action.h | 61 | ||||
-rw-r--r-- | src/resources/animation.cpp | 53 | ||||
-rw-r--r-- | src/resources/animation.h | 99 | ||||
-rw-r--r-- | src/resources/spritedef.cpp | 37 | ||||
-rw-r--r-- | src/resources/spritedef.h | 3 |
6 files changed, 298 insertions, 22 deletions
diff --git a/src/resources/action.cpp b/src/resources/action.cpp new file mode 100644 index 00000000..247455db --- /dev/null +++ b/src/resources/action.cpp @@ -0,0 +1,67 @@ +/* + * The Mana World + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * The Mana World is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with The Mana World; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id$ + */ + +#include "action.h" + +#include <algorithm> + +#include "animation.h" + +#include "../utils/dtor.h" + + +Action::Action() +{ +} + +Action::~Action() +{ + std::for_each(mAnimations.begin(), mAnimations.end(), + make_dtor(mAnimations)); +} + +Animation* +Action::getAnimation(int direction) const +{ + Animations::const_iterator i = mAnimations.find(direction); + + // When the direction isn't defined, try the default + if (i == mAnimations.end()) + { + i = mAnimations.find(0); + } + + return (i == mAnimations.end()) ? NULL : i->second; +} + +void +Action::setAnimation(int direction, Animation *animation) +{ + // Set first direction as default direction + if (mAnimations.empty()) + { + mAnimations[0] = animation; + } + + mAnimations[direction] = animation; +} diff --git a/src/resources/action.h b/src/resources/action.h new file mode 100644 index 00000000..8d5e8d11 --- /dev/null +++ b/src/resources/action.h @@ -0,0 +1,61 @@ +/* + * The Mana World + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * The Mana World is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with The Mana World; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id$ + */ + +#ifndef _TMW_ACTION_H +#define _TMW_ACTION_H + +#include <map> + +#include <libxml/tree.h> + +class Animation; + +/** + * An action consists of several animations, one for each direction. + */ +class Action +{ + public: + /** + * Constructor. + */ + Action(); + + /** + * Destructor. + */ + ~Action(); + + void + setAnimation(int direction, Animation *animation); + + Animation* + getAnimation(int direction) const; + + protected: + typedef std::map<int, Animation*> Animations; + typedef Animations::iterator AnimationIterator; + Animations mAnimations; +}; + +#endif diff --git a/src/resources/animation.cpp b/src/resources/animation.cpp new file mode 100644 index 00000000..de96525c --- /dev/null +++ b/src/resources/animation.cpp @@ -0,0 +1,53 @@ +/* + * The Mana World + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * The Mana World is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with The Mana World; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id$ + */ + +#include "animation.h" + +#include <algorithm> + +#include "../utils/dtor.h" + +Animation::Animation(): + mDuration(0) +{ +} + +void +Animation::addFrame(Image *image, unsigned int delay, int offsetX, int offsetY) +{ + Frame frame = { image, delay, offsetX, offsetY }; + mFrames.push_back(frame); + mDuration += delay; +} + +void +Animation::addTerminator() +{ + addFrame(NULL, 0, 0, 0); +} + +bool +Animation::isTerminator(const Frame &candidate) +{ + return (candidate.image == NULL); +} diff --git a/src/resources/animation.h b/src/resources/animation.h new file mode 100644 index 00000000..54142bcb --- /dev/null +++ b/src/resources/animation.h @@ -0,0 +1,99 @@ +/* + * The Mana World + * Copyright 2004 The Mana World Development Team + * + * This file is part of The Mana World. + * + * The Mana World is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * any later version. + * + * The Mana World is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with The Mana World; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + * + * $Id$ + */ + +#ifndef _TMW_ANIMATION_H +#define _TMW_ANIMATION_H + +#include <vector> + +#include <libxml/tree.h> + +class Image; +class Spriteset; + +/** + * A single frame in an animation, with a delay and an offset. + */ +struct Frame +{ + Image *image; + unsigned int delay; + int offsetX; + int offsetY; +}; + +/** + * An animation consists of several frames, each with their own delay and + * offset. + */ +class Animation +{ + public: + /** + * Constructor. + */ + Animation(); + + /** + * Appends a new animation at the end of the sequence + */ + void + addFrame(Image *image, unsigned int delay, int offsetX, int offsetY); + + /** + * Appends an animation terminator that states that the animation + * should not loop. + */ + void + addTerminator(); + + /** + * 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 mFrames.size(); } + + /** + * Returns the duration of this animation. + */ + int + getDuration() const { return mDuration; } + + /** + * Determines whether the given animation frame is a terminator. + */ + static bool + isTerminator(const Frame &phase); + + protected: + std::vector<Frame> mFrames; + int mDuration; +}; + +#endif diff --git a/src/resources/spritedef.cpp b/src/resources/spritedef.cpp index bd273b3b..feb6f8f8 100644 --- a/src/resources/spritedef.cpp +++ b/src/resources/spritedef.cpp @@ -23,11 +23,10 @@ #include "spritedef.h" -#include "../animation.h" -#include "../action.h" -#include "../graphics.h" #include "../log.h" +#include "animation.h" +#include "action.h" #include "resourcemanager.h" #include "spriteset.h" #include "image.h" @@ -208,20 +207,20 @@ SpriteDef::loadAnimation(xmlNodePtr animationNode, Animation *animation = new Animation(); action->setAnimation(directionType, animation); - // Get animation phases - for (xmlNodePtr phaseNode = animationNode->xmlChildrenNode; - phaseNode != NULL; - phaseNode = phaseNode->next) + // Get animation frames + for (xmlNodePtr frameNode = animationNode->xmlChildrenNode; + frameNode != NULL; + frameNode = frameNode->next) { - int delay = XML::getProperty(phaseNode, "delay", 0); - int offsetX = XML::getProperty(phaseNode, "offsetX", 0); - int offsetY = XML::getProperty(phaseNode, "offsetY", 0); + 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; - if (xmlStrEqual(phaseNode->name, BAD_CAST "frame")) + if (xmlStrEqual(frameNode->name, BAD_CAST "frame")) { - int index = XML::getProperty(phaseNode, "index", -1); + int index = XML::getProperty(frameNode, "index", -1); if (index < 0) { @@ -237,12 +236,12 @@ SpriteDef::loadAnimation(xmlNodePtr animationNode, continue; } - animation->addPhase(img, delay, offsetX, offsetY); + animation->addFrame(img, delay, offsetX, offsetY); } - else if (xmlStrEqual(phaseNode->name, BAD_CAST "sequence")) + else if (xmlStrEqual(frameNode->name, BAD_CAST "sequence")) { - int start = XML::getProperty(phaseNode, "start", -1); - int end = XML::getProperty(phaseNode, "end", -1); + int start = XML::getProperty(frameNode, "start", -1); + int end = XML::getProperty(frameNode, "end", -1); if (start < 0 || end < 0) { @@ -261,15 +260,15 @@ SpriteDef::loadAnimation(xmlNodePtr animationNode, continue; } - animation->addPhase(img, delay, offsetX, offsetY); + animation->addFrame(img, delay, offsetX, offsetY); start++; } } - else if (xmlStrEqual(phaseNode->name, BAD_CAST "end")) + else if (xmlStrEqual(frameNode->name, BAD_CAST "end")) { animation->addTerminator(); } - } // for phaseNode + } // for frameNode } void diff --git a/src/resources/spritedef.h b/src/resources/spritedef.h index 64414259..057129ad 100644 --- a/src/resources/spritedef.h +++ b/src/resources/spritedef.h @@ -32,10 +32,7 @@ #include <libxml/tree.h> class Action; -class Graphics; class Spriteset; -struct AnimationPhase; -class Animation; enum SpriteAction { |