From 25240241e91dc913d20fbb93aa4acc9433dda6a0 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sun, 15 Mar 2015 20:58:02 -0400 Subject: Map editor can now save and load (but not edit) entities in maps --- tools/mapedit/src/object.cpp | 99 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 99 insertions(+) create mode 100644 tools/mapedit/src/object.cpp (limited to 'tools/mapedit/src/object.cpp') diff --git a/tools/mapedit/src/object.cpp b/tools/mapedit/src/object.cpp new file mode 100644 index 0000000..4cd267d --- /dev/null +++ b/tools/mapedit/src/object.cpp @@ -0,0 +1,99 @@ +#include "object.h" +#include +#include +#include + +static std::map> allObjects; +static bool objsInit = false; + +const std::map> MapObject::getAllObjects() +{ + if (!objsInit) + { + DIR* dir = opendir("../../../entities/"); + if (dir != NULL) + { + struct dirent* ent; + while ((ent = readdir(dir)) != NULL) + { + std::string path = ent->d_name; + if ((path.length() >= 4) && (path.substr(path.length() - 4, 4) == ".xml")) + { + std::string name = path.substr(0, path.length() - 4); + auto obj = std::make_shared(name.c_str()); + + allObjects[name] = obj; + } + } + } + + objsInit = true; + } + + return allObjects; +} + +MapObject::MapObject(const char* filename) +{ + type = filename; + + xmlDocPtr doc = xmlParseFile(("../../../entities/" + std::string(filename) + ".xml").c_str()); + if (doc == nullptr) throw MapObjectLoadException(filename); + + xmlNodePtr top = xmlDocGetRootElement(doc); + if (top == nullptr) throw MapObjectLoadException(filename); + + if (xmlStrcmp(top->name, (const xmlChar*) "entity-def")) + { + throw MapObjectLoadException(filename); + } + + for (xmlNodePtr node = top->xmlChildrenNode; node != NULL; node = node->next) + { + if (!xmlStrcmp(node->name, (const xmlChar*) "sprite")) + { + xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); + std::string spriteFile = (char*) key; + xmlFree(key); + + sprite = wxImage("../../" + spriteFile); + } else if (!xmlStrcmp(node->name, (const xmlChar*) "action")) + { + xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); + action = (char*) key; + xmlFree(key); + } else if (!xmlStrcmp(node->name, (const xmlChar*) "size")) + { + xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); + sscanf((char*) key, "%d,%d", &width, &height); + xmlFree(key); + } + } + + xmlFreeDoc(doc); +} + +wxBitmap MapObject::getSprite() const +{ + return sprite; +} + +std::string MapObject::getAction() const +{ + return action; +} + +int MapObject::getWidth() const +{ + return width; +} + +int MapObject::getHeight() const +{ + return height; +} + +std::string MapObject::getType() const +{ + return type; +} -- cgit 1.4.1