summaryrefslogtreecommitdiff
path: root/src/resources/spritedef.cpp
diff options
context:
space:
mode:
authorGuillaume Melquiond <guillaume.melquiond@gmail.com>2007-10-19 17:46:46 +0000
committerGuillaume Melquiond <guillaume.melquiond@gmail.com>2007-10-19 17:46:46 +0000
commit41cf359468d6379bc0e033bee9ac148d1b9255dd (patch)
treed351692d76e861607fd504366ae22374613b0fec /src/resources/spritedef.cpp
parentc3e36e443698b001e98219e48cc97e259ada0a1d (diff)
downloadMana-41cf359468d6379bc0e033bee9ac148d1b9255dd.tar.gz
Mana-41cf359468d6379bc0e033bee9ac148d1b9255dd.tar.bz2
Mana-41cf359468d6379bc0e033bee9ac148d1b9255dd.tar.xz
Mana-41cf359468d6379bc0e033bee9ac148d1b9255dd.zip
Factored code between resource handlers. Implemented failure-friendly sprite loader.
Diffstat (limited to 'src/resources/spritedef.cpp')
-rw-r--r--src/resources/spritedef.cpp47
1 files changed, 22 insertions, 25 deletions
diff --git a/src/resources/spritedef.cpp b/src/resources/spritedef.cpp
index d90d4067..0daa0cc0 100644
--- a/src/resources/spritedef.cpp
+++ b/src/resources/spritedef.cpp
@@ -33,16 +33,6 @@
#include "../utils/xml.h"
-SpriteDef::SpriteDef(const std::string &idPath,
- const std::string &file, int variant):
- Resource(idPath),
- mAction(NULL),
- mDirection(DIRECTION_DOWN),
- mLastTime(0)
-{
- load(file, variant);
-}
-
Action*
SpriteDef::getAction(SpriteAction action) const
{
@@ -57,29 +47,29 @@ SpriteDef::getAction(SpriteAction action) const
return i->second;
}
-void
-SpriteDef::load(const std::string &animationFile, int variant)
+SpriteDef *SpriteDef::load(std::string const &animationFile, int variant)
{
int size;
ResourceManager *resman = ResourceManager::getInstance();
char *data = (char*) resman->loadFile(animationFile.c_str(), size);
- if (!data) {
- logger->error("Animation: Could not find " + animationFile + "!");
- }
+ if (!data) return NULL;
xmlDocPtr doc = xmlParseMemory(data, size);
free(data);
- if (!doc) {
- logger->error(
- "Animation: Error while parsing " + animationFile + " file!");
+ if (!doc)
+ {
+ logger->log("Error, failed to parse %s.", animationFile.c_str());
+ return NULL;
}
xmlNodePtr rootNode = xmlDocGetRootElement(doc);
- if (!rootNode || !xmlStrEqual(rootNode->name, BAD_CAST "sprite")) {
- logger->error(
- "Animation: this is not a valid " + animationFile + " file!");
+ if (!rootNode || !xmlStrEqual(rootNode->name, BAD_CAST "sprite"))
+ {
+ logger->log("Error, failed to parse %s.", animationFile.c_str());
+ xmlFreeDoc(doc);
+ return NULL;
}
// Get the variant
@@ -91,25 +81,32 @@ SpriteDef::load(const std::string &animationFile, int variant)
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"))
{
- loadImageSet(node);
+ def->loadImageSet(node);
}
else if (xmlStrEqual(node->name, BAD_CAST "action"))
{
- loadAction(node, variant_offset);
+ def->loadAction(node, variant_offset);
}
else if (xmlStrEqual(node->name, BAD_CAST "include"))
{
- includeSprite(node);
+ def->includeSprite(node);
}
}
xmlFreeDoc(doc);
- // Complete missing actions
+ def->substituteActions();
+ return def;
+}
+
+void SpriteDef::substituteActions()
+{
substituteAction(ACTION_STAND, ACTION_DEFAULT);
substituteAction(ACTION_WALK, ACTION_STAND);
substituteAction(ACTION_WALK, ACTION_RUN);