diff options
author | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2008-07-01 17:41:30 +0000 |
---|---|---|
committer | Bjørn Lindeijer <bjorn@lindeijer.nl> | 2008-07-01 17:41:30 +0000 |
commit | 4ca8675700dfae625376c4afdd30b8590d68087e (patch) | |
tree | 7f661baf3d566fcf5d0318da13e29c13098cf21a /src/resources/spritedef.cpp | |
parent | cf5cfbcb8ff63005b1e08f9718ce3f80f919271e (diff) | |
download | mana-4ca8675700dfae625376c4afdd30b8590d68087e.tar.gz mana-4ca8675700dfae625376c4afdd30b8590d68087e.tar.bz2 mana-4ca8675700dfae625376c4afdd30b8590d68087e.tar.xz mana-4ca8675700dfae625376c4afdd30b8590d68087e.zip |
Merged revisions 4044-4067 via svnmerge from
https://themanaworld.svn.sourceforge.net/svnroot/themanaworld/tmw/trunk
........
r4044 | b_lindeijer | 2008-04-07 11:02:38 +0200 (Mon, 07 Apr 2008) | 2 lines
Added support for basing one sprite definition on another by including it.
........
r4057 | b_lindeijer | 2008-04-08 20:43:44 +0200 (Tue, 08 Apr 2008) | 4 lines
Implemented support for an arbitrary number of map layers. The only layer
requirement for map layers are now that the fringe layer is called "Fringe" and
that the collision layer is called "Collision".
........
r4067 | b_lindeijer | 2008-04-11 00:03:57 +0200 (Fri, 11 Apr 2008) | 3 lines
Fixed some compiler warnings and don't try to load particle effects when no
file is set.
........
Diffstat (limited to 'src/resources/spritedef.cpp')
-rw-r--r-- | src/resources/spritedef.cpp | 98 |
1 files changed, 60 insertions, 38 deletions
diff --git a/src/resources/spritedef.cpp b/src/resources/spritedef.cpp index 334474d7..ebc60240 100644 --- a/src/resources/spritedef.cpp +++ b/src/resources/spritedef.cpp @@ -70,33 +70,8 @@ SpriteDef *SpriteDef::load(std::string const &animationFile, int variant) } } - // Get the variant - int variant_num = XML::getProperty(rootNode, "variants", 0); - int variant_offset = 0; - - if (variant_num > 0 && variant < variant_num) - { - variant_offset = variant * XML::getProperty(rootNode, "variant_offset", 0); - } - SpriteDef *def = new SpriteDef; - - for_each_xml_child_node(node, rootNode) - { - if (xmlStrEqual(node->name, BAD_CAST "imageset")) - { - def->loadImageSet(node, palettes); - } - else if (xmlStrEqual(node->name, BAD_CAST "action")) - { - def->loadAction(node, variant_offset); - } - else if (xmlStrEqual(node->name, BAD_CAST "include")) - { - def->includeSprite(node); - } - } - + def->loadSprite(rootNode, variant, palettes); def->substituteActions(); return def; } @@ -119,11 +94,47 @@ void SpriteDef::substituteActions() substituteAction(ACTION_DEAD, ACTION_HURT); } +void SpriteDef::loadSprite(xmlNodePtr spriteNode, int variant, + const std::string &palettes) +{ + // Get the variant + const int variantCount = XML::getProperty(spriteNode, "variants", 0); + int variant_offset = 0; + + if (variantCount > 0 && variant < variantCount) + { + variant_offset = + variant * XML::getProperty(spriteNode, "variant_offset", 0); + } + + for_each_xml_child_node(node, spriteNode) + { + if (xmlStrEqual(node->name, BAD_CAST "imageset")) + { + loadImageSet(node, palettes); + } + else if (xmlStrEqual(node->name, BAD_CAST "action")) + { + loadAction(node, variant_offset); + } + else if (xmlStrEqual(node->name, BAD_CAST "include")) + { + includeSprite(node); + } + } +} + void SpriteDef::loadImageSet(xmlNodePtr node, std::string const &palettes) { - int width = XML::getProperty(node, "width", 0); - int height = XML::getProperty(node, "height", 0); - std::string name = XML::getProperty(node, "name", ""); + const std::string name = XML::getProperty(node, "name", ""); + + // We don't allow redefining image sets. This way, an included sprite + // definition will use the already loaded image set with the same name. + if (mImageSets.find(name) != mImageSets.end()) + return; + + const int width = XML::getProperty(node, "width", 0); + const int height = XML::getProperty(node, "height", 0); std::string imageSrc = XML::getProperty(node, "src", ""); Dye::instantiate(imageSrc, palettes); @@ -184,9 +195,9 @@ SpriteDef::loadAnimation(xmlNodePtr animationNode, Action *action, ImageSet *imageSet, int variant_offset) { - std::string directionName = + const std::string directionName = XML::getProperty(animationNode, "direction", ""); - SpriteDirection directionType = makeSpriteDirection(directionName); + const SpriteDirection directionType = makeSpriteDirection(directionName); if (directionType == DIRECTION_INVALID) { @@ -201,7 +212,7 @@ SpriteDef::loadAnimation(xmlNodePtr animationNode, // Get animation frames for_each_xml_child_node(frameNode, animationNode) { - int delay = XML::getProperty(frameNode, "delay", 0); + const 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; @@ -209,7 +220,7 @@ SpriteDef::loadAnimation(xmlNodePtr animationNode, if (xmlStrEqual(frameNode->name, BAD_CAST "frame")) { - int index = XML::getProperty(frameNode, "index", -1); + const int index = XML::getProperty(frameNode, "index", -1); if (index < 0) { @@ -230,7 +241,7 @@ SpriteDef::loadAnimation(xmlNodePtr animationNode, else if (xmlStrEqual(frameNode->name, BAD_CAST "sequence")) { int start = XML::getProperty(frameNode, "start", -1); - int end = XML::getProperty(frameNode, "end", -1); + const int end = XML::getProperty(frameNode, "end", -1); if (start < 0 || end < 0) { @@ -263,12 +274,23 @@ SpriteDef::loadAnimation(xmlNodePtr animationNode, void SpriteDef::includeSprite(xmlNodePtr includeNode) { + // TODO: Perform circular dependency check, since it's easy to crash the + // client this way. const std::string filename = XML::getProperty(includeNode, "file", ""); - ResourceManager *resman = ResourceManager::getInstance(); - SpriteDef *sprite = resman->getSprite("graphics/sprites/" + filename); - // TODO: Somehow implement actually including it - sprite->decRef(); + if (filename.empty()) + return; + + XML::Document doc("graphics/sprites/" + filename); + xmlNodePtr rootNode = doc.rootNode(); + + if (!rootNode || !xmlStrEqual(rootNode->name, BAD_CAST "sprite")) + { + logger->log("Error, no sprite root node in %s", filename.c_str()); + return; + } + + loadSprite(rootNode, 0); } void |