diff options
author | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2006-11-15 21:43:36 +0000 |
---|---|---|
committer | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2006-11-15 21:43:36 +0000 |
commit | 86db18d65a1c6e92b115efc3afe65f770ec93398 (patch) | |
tree | 9fedd710f689b3c9732f758330578b7b7347f391 /src | |
parent | 2d7084e4f07cd56021ca47dc6124ed587883acc2 (diff) | |
download | mana-86db18d65a1c6e92b115efc3afe65f770ec93398.tar.gz mana-86db18d65a1c6e92b115efc3afe65f770ec93398.tar.bz2 mana-86db18d65a1c6e92b115efc3afe65f770ec93398.tar.xz mana-86db18d65a1c6e92b115efc3afe65f770ec93398.zip |
Separated getProperty method to an XML utility namespace.
Diffstat (limited to 'src')
-rw-r--r-- | src/CMakeLists.txt | 2 | ||||
-rw-r--r-- | src/Makefile.am | 2 | ||||
-rw-r--r-- | src/animatedsprite.cpp | 63 | ||||
-rw-r--r-- | src/animatedsprite.h | 18 | ||||
-rw-r--r-- | src/resources/mapreader.cpp | 30 | ||||
-rw-r--r-- | src/utils/xml.cpp | 54 | ||||
-rw-r--r-- | src/utils/xml.h | 46 |
7 files changed, 131 insertions, 84 deletions
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 35b01931..3981ee58 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -227,6 +227,8 @@ SET(SRCS resources/buddylist.cpp utils/dtor.h utils/tostring.h + utils/xml.cpp + utils/xml.h action.cpp action.h animatedsprite.cpp diff --git a/src/Makefile.am b/src/Makefile.am index 8166efed..5f29bebc 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -189,6 +189,8 @@ tmw_SOURCES = graphic/imagerect.h \ resources/buddylist.cpp \ utils/dtor.h \ utils/tostring.h \ + utils/xml.cpp \ + utils/xml.h \ action.cpp \ action.h \ animatedsprite.cpp \ diff --git a/src/animatedsprite.cpp b/src/animatedsprite.cpp index 61d2f3cf..c2f7f133 100644 --- a/src/animatedsprite.cpp +++ b/src/animatedsprite.cpp @@ -32,11 +32,12 @@ #include "resources/spriteset.h" #include "resources/image.h" +#include "utils/xml.h" + AnimatedSprite::AnimatedSprite(const std::string& animationFile, int variant): mAction(NULL), mDirection(DIRECTION_DOWN), - mLastTime(0), - mSpeed(1.0f) + mLastTime(0) { int size; ResourceManager *resman = ResourceManager::getInstance(); @@ -61,8 +62,8 @@ AnimatedSprite::AnimatedSprite(const std::string& animationFile, int variant): } // Get the variant - int variant_num = getProperty(node, "variants", 0); - int variant_offset = getProperty(node, "variant_offset", 0); + int variant_num = XML::getProperty(node, "variants", 0); + int variant_offset = XML::getProperty(node, "variant_offset", 0); if (variant_num > 0 && variant < variant_num ) { variant_offset *= variant; @@ -74,10 +75,10 @@ AnimatedSprite::AnimatedSprite(const std::string& animationFile, int variant): { if (xmlStrEqual(node->name, BAD_CAST "imageset")) { - int width = getProperty(node, "width", 0); - int height = getProperty(node, "height", 0); - std::string name = getProperty(node, "name", ""); - std::string imageSrc = getProperty(node, "src", ""); + int width = XML::getProperty(node, "width", 0); + int height = XML::getProperty(node, "height", 0); + std::string name = XML::getProperty(node, "name", ""); + std::string imageSrc = XML::getProperty(node, "src", ""); Spriteset *spriteset = resman->getSpriteset(imageSrc, width, height); @@ -91,8 +92,8 @@ AnimatedSprite::AnimatedSprite(const std::string& animationFile, int variant): // get action else if (xmlStrEqual(node->name, BAD_CAST "action")) { - std::string actionName = getProperty(node, "name", ""); - std::string imagesetName = getProperty(node, "imageset", ""); + std::string actionName = XML::getProperty(node, "name", ""); + std::string imagesetName = XML::getProperty(node, "imageset", ""); SpritesetIterator si = mSpritesets.find(imagesetName); if (si == mSpritesets.end()) { @@ -133,7 +134,7 @@ AnimatedSprite::AnimatedSprite(const std::string& animationFile, int variant): continue; std::string directionName = - getProperty(animationNode, "direction", ""); + XML::getProperty(animationNode, "direction", ""); SpriteDirection directionType = makeSpriteDirection(directionName); @@ -155,13 +156,13 @@ AnimatedSprite::AnimatedSprite(const std::string& animationFile, int variant): phaseNode != NULL; phaseNode = phaseNode->next) { - int delay = getProperty(phaseNode, "delay", 0); + int delay = XML::getProperty(phaseNode, "delay", 0); if (xmlStrEqual(phaseNode->name, BAD_CAST "frame")) { - int index = getProperty(phaseNode, "index", -1); - int offsetX = getProperty(phaseNode, "offsetX", 0); - int offsetY = getProperty(phaseNode, "offsetY", 0); + int index = XML::getProperty(phaseNode, "index", -1); + int offsetX = XML::getProperty(phaseNode, "offsetX", 0); + int offsetY = XML::getProperty(phaseNode, "offsetY", 0); offsetY -= imageset->getHeight() - 32; offsetX -= imageset->getWidth() / 2 - 16; @@ -170,8 +171,8 @@ AnimatedSprite::AnimatedSprite(const std::string& animationFile, int variant): } else if (xmlStrEqual(phaseNode->name, BAD_CAST "sequence")) { - int start = getProperty(phaseNode, "start", 0); - int end = getProperty(phaseNode, "end", 0); + int start = XML::getProperty(phaseNode, "start", 0); + int end = XML::getProperty(phaseNode, "end", 0); int offsetY = -imageset->getHeight() + 32; int offsetX = -imageset->getWidth() / 2 + 16; while (end >= start) @@ -212,34 +213,6 @@ AnimatedSprite::AnimatedSprite(const std::string& animationFile, int variant): xmlFreeDoc(doc); } -int -AnimatedSprite::getProperty(xmlNodePtr node, const char* name, int def) -{ - int &ret = def; - - xmlChar *prop = xmlGetProp(node, BAD_CAST name); - if (prop) { - ret = atoi((char*)prop); - xmlFree(prop); - } - - return ret; -} - -std::string -AnimatedSprite::getProperty(xmlNodePtr node, const char *name, - const std::string &def) -{ - xmlChar *prop = xmlGetProp(node, BAD_CAST name); - if (prop) { - std::string val = (char*)prop; - xmlFree(prop); - return val; - } - - return def; -} - void AnimatedSprite::substituteAction(SpriteAction complete, SpriteAction with) diff --git a/src/animatedsprite.h b/src/animatedsprite.h index 73dfa529..721a2824 100644 --- a/src/animatedsprite.h +++ b/src/animatedsprite.h @@ -28,8 +28,6 @@ #include <string> #include <SDL_types.h> -#include <libxml/tree.h> - class Action; class Graphics; class Spriteset; @@ -144,21 +142,6 @@ class AnimatedSprite getCurrentPhase() const; /** - * 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); - - /** * Converts a string into a SpriteAction enum. */ static SpriteAction @@ -182,7 +165,6 @@ class AnimatedSprite Action *mAction; SpriteDirection mDirection; int mLastTime; - float mSpeed; }; #endif diff --git a/src/resources/mapreader.cpp b/src/resources/mapreader.cpp index 2aea3dc5..cd74ded6 100644 --- a/src/resources/mapreader.cpp +++ b/src/resources/mapreader.cpp @@ -36,6 +36,7 @@ #include "../tileset.h" #include "../utils/tostring.h" +#include "../utils/xml.h" const unsigned int DEFAULT_TILE_WIDTH = 32; const unsigned int DEFAULT_TILE_HEIGHT = 32; @@ -205,10 +206,10 @@ MapReader::readMap(xmlNodePtr node, const std::string &path) prop = xmlGetProp(node, BAD_CAST "version"); xmlFree(prop); - int w = getProperty(node, "width", 0); - int h = getProperty(node, "height", 0); - int tilew = getProperty(node, "tilewidth", DEFAULT_TILE_WIDTH); - int tileh = getProperty(node, "tileheight", DEFAULT_TILE_HEIGHT); + int w = XML::getProperty(node, "width", 0); + int h = XML::getProperty(node, "height", 0); + int tilew = XML::getProperty(node, "tilewidth", DEFAULT_TILE_WIDTH); + int tileh = XML::getProperty(node, "tileheight", DEFAULT_TILE_HEIGHT); int layerNr = 0; Map *map = new Map(w, h, tilew, tileh); @@ -354,7 +355,7 @@ MapReader::readLayer(xmlNodePtr node, Map *map, int layer) if (!xmlStrEqual(n2->name, BAD_CAST "tile")) continue; - int gid = getProperty(n2, "gid", -1); + int gid = XML::getProperty(n2, "gid", -1); map->setTileWithGid(x, y, layer, gid); x++; @@ -387,9 +388,9 @@ MapReader::readTileset(xmlNodePtr node, return NULL; } - int firstGid = getProperty(node, "firstgid", 0); - int tw = getProperty(node, "tilewidth", map->getTileWidth()); - int th = getProperty(node, "tileheight", map->getTileHeight()); + int firstGid = XML::getProperty(node, "firstgid", 0); + int tw = XML::getProperty(node, "tilewidth", map->getTileWidth()); + int th = XML::getProperty(node, "tileheight", map->getTileHeight()); for (node = node->xmlChildrenNode; node; node = node->next) { if (!xmlStrEqual(node->name, BAD_CAST "image")) @@ -422,16 +423,3 @@ MapReader::readTileset(xmlNodePtr node, return NULL; } - -int -MapReader::getProperty(xmlNodePtr node, const char* name, int def) -{ - int &ret = def; - - xmlChar *prop = xmlGetProp(node, BAD_CAST name); - if (prop) { - ret = atoi((char*)prop); - xmlFree(prop); - } - return ret; -} diff --git a/src/utils/xml.cpp b/src/utils/xml.cpp new file mode 100644 index 00000000..b820011d --- /dev/null +++ b/src/utils/xml.cpp @@ -0,0 +1,54 @@ +/* + * 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: dtor.h 2822 2006-11-05 14:30:53Z b_lindeijer $ + */ + +#include "xml.h" + +namespace XML +{ + int + getProperty(xmlNodePtr node, const char* name, int def) + { + int &ret = def; + + xmlChar *prop = xmlGetProp(node, BAD_CAST name); + if (prop) { + ret = atoi((char*)prop); + xmlFree(prop); + } + + return ret; + } + + std::string + getProperty(xmlNodePtr node, const char *name, const std::string &def) + { + xmlChar *prop = xmlGetProp(node, BAD_CAST name); + if (prop) { + std::string val = (char*)prop; + xmlFree(prop); + return val; + } + + return def; + } +} diff --git a/src/utils/xml.h b/src/utils/xml.h new file mode 100644 index 00000000..9cbb5b2a --- /dev/null +++ b/src/utils/xml.h @@ -0,0 +1,46 @@ +/* + * 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: dtor.h 2822 2006-11-05 14:30:53Z b_lindeijer $ + */ + +#ifndef _TMW_XML_H +#define _TMW_XML_H + +#include <libxml/tree.h> + +#include <string> + +namespace XML +{ + /** + * Gets an integer property from an xmlNodePtr. + */ + int + getProperty(xmlNodePtr node, const char *name, int def); + + /** + * Gets a string property from an xmlNodePtr. + */ + std::string + getProperty(xmlNodePtr node, const char *name, const std::string &def); +} + +#endif |