diff options
author | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2006-07-24 00:13:24 +0000 |
---|---|---|
committer | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2006-07-24 00:13:24 +0000 |
commit | 4789ebb11407eb9402c385f3cc6fa242d91214c2 (patch) | |
tree | 6bf5bed52073ae19ef4b5b08e7fe92af603f04d8 /src/animation.h | |
parent | 1b7ff9ad4160fd0ff79d2da658eca8d8c8abf815 (diff) | |
download | mana-4789ebb11407eb9402c385f3cc6fa242d91214c2.tar.gz mana-4789ebb11407eb9402c385f3cc6fa242d91214c2.tar.bz2 mana-4789ebb11407eb9402c385f3cc6fa242d91214c2.tar.xz mana-4789ebb11407eb9402c385f3cc6fa242d91214c2.zip |
Implemented caching of spritesets, including a lot of cleanups to the new
animation system. Action now refers to the Spriteset directly and
AnimatedSprite refers to the current Action directly instead of using the
std::map with a std::string constantly. Some methods and parameters are marked
as const. The READ_PROP macro was replaced by static methods. Warnings are
logged when unnamed actions are defined or when actions refer to undefined
imagesets. Code is more tolerant towards missing actions.
Diffstat (limited to 'src/animation.h')
-rw-r--r-- | src/animation.h | 100 |
1 files changed, 77 insertions, 23 deletions
diff --git a/src/animation.h b/src/animation.h index 4f7d57a9..c5adb873 100644 --- a/src/animation.h +++ b/src/animation.h @@ -28,11 +28,16 @@ #include <map> #include <string> +#include <libxml/tree.h> + #include "graphics.h" class Image; class Spriteset; +/** + * A single frame in an animation, with a delay and an offset. + */ struct AnimationPhase { int image; @@ -41,19 +46,26 @@ struct AnimationPhase int offsetY; }; +/** + * An animation consists of several frames, each with their own delay and + * offset. + */ class Animation { public: + /** + * Constructor. + */ Animation(); void addPhase(int image, unsigned int delay, int offsetX, int offsetY); void update(unsigned int time); - int getCurrentPhase(); + int getCurrentPhase() const; - int getOffsetX() { return (*iCurrentPhase).offsetX; }; - int getOffsetY() { return (*iCurrentPhase).offsetY; }; + int getOffsetX() const { return (*iCurrentPhase).offsetX; }; + int getOffsetY() const { return (*iCurrentPhase).offsetY; }; int getLength(); @@ -63,23 +75,42 @@ class Animation unsigned int mTime; }; +/** + * An action consists of several animations, one for each direction. + */ class Action { public: + /** + * Constructor. + */ Action(); + /** + * Destructor. + */ ~Action(); - void setImageset(std::string imageset) { mImageset = imageset; } + /** + * Sets the spriteset used by this action. + */ + void + setSpriteset(Spriteset *spriteset) { mSpriteset = spriteset; } - std::string getImageset() { return mImageset; } + /** + * Returns the spriteset used by this action. + */ + Spriteset* + getSpriteset() const { return mSpriteset; } - void setAnimation(std::string direction, Animation *animation); + void + setAnimation(const std::string& direction, Animation *animation); - Animation *getAnimation(std::string direction); // { return mAnimations[direction]; } + Animation* + getAnimation(const std::string& direction) const; protected: - std::string mImageset; + Spriteset *mSpriteset; typedef std::map<std::string, Animation*> Animations; typedef Animations::iterator AnimationIterator; Animations mAnimations; @@ -94,7 +125,7 @@ class AnimatedSprite /** * Constructor. */ - AnimatedSprite(std::string animationFile, int variant); + AnimatedSprite(const std::string& animationFile, int variant); /** * Destructor. @@ -104,12 +135,14 @@ class AnimatedSprite /** * Sets a new action using the current direction. */ - void play(std::string action); + void + play(const std::string& action); /** * Plays an action in a specified time. */ - void play(std::string action, int time); + void + play(const std::string& action, int time); /** * Inform the animation of the passed time so that it can output the @@ -119,45 +152,66 @@ class AnimatedSprite /** * Draw the current animation phase at the coordinates given in screen - * pixels - */ - bool draw(Graphics * graphics, Sint32 posX, Sint32 posY); - - /** - * Draw the current animation phase. + * pixels. */ - Image *getCurrentFrame(); + bool + draw(Graphics* graphics, Sint32 posX, Sint32 posY) const; /** * gets the width in pixels of the current animation phase. */ - int getWidth(); + int + getWidth() const; /** * gets the height in pixels of the current animation phase. */ - int getHeight(); + int + getHeight() const; /** * Sets the direction. */ - void setDirection(std::string direction) { mDirection = direction; } + void + setDirection(const std::string& direction) + { + mDirection = direction; + } protected: /** * When there are no animations defined for the action "complete", its * animations become a copy of those of the action "with". */ - void substituteAction(std::string complete, std::string with); + void + substituteAction(const std::string& complete, + const std::string& with); typedef std::map<std::string, Spriteset*> Spritesets; typedef Spritesets::iterator SpritesetIterator; Spritesets mSpritesets; typedef std::map<std::string, Action*> Actions; Actions mActions; - std::string mAction, mDirection; + Action *mAction; + std::string mDirection; int mLastTime; float mSpeed; + + private: + /** + * Gets an integer property from an xmlNodePtr. + * + * TODO: Same function is present in MapReader. Should probably be + * TODO: shared in a static utility class. + */ + static int + getProperty(xmlNodePtr node, const char* name, int def); + + /** + * Gets a string property from an xmlNodePtr. + */ + static std::string + getProperty(xmlNodePtr node, const char* name, const std::string& def); }; #endif |