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/map.cpp | 64 +++++++++++++++++++++++++++++++++++++++++------ 1 file changed, 57 insertions(+), 7 deletions(-) (limited to 'tools/mapedit/src/map.cpp') diff --git a/tools/mapedit/src/map.cpp b/tools/mapedit/src/map.cpp index 3a24ace..b0440fb 100644 --- a/tools/mapedit/src/map.cpp +++ b/tools/mapedit/src/map.cpp @@ -54,6 +54,31 @@ Map::Map(std::string filename) xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); rightmap = (char*) key; xmlFree(key); + } else if (!xmlStrcmp(node->name, (const xmlChar*) "entities")) + { + for (xmlNodePtr entityNode = node->xmlChildrenNode; entityNode != NULL; entityNode = entityNode->next) + { + if (!xmlStrcmp(entityNode->name, (const xmlChar*) "entity")) + { + MapObjectEntry data; + for (xmlNodePtr entityDataNode = entityNode->xmlChildrenNode; entityDataNode != NULL; entityDataNode = entityDataNode->next) + { + if (!xmlStrcmp(entityDataNode->name, (const xmlChar*) "entity-type")) + { + xmlChar* key = xmlNodeListGetString(doc, entityDataNode->xmlChildrenNode, 1); + data.object = MapObject::getAllObjects().at((char*) key); + xmlFree(key); + } else if (!xmlStrcmp(entityDataNode->name, (const xmlChar*) "entity-position")) + { + xmlChar* key = xmlNodeListGetString(doc, entityDataNode->xmlChildrenNode, 1); + sscanf((char*) key, "%lf,%lf", &data.position.first, &data.position.second); + xmlFree(key); + } + } + + objects.push_back(data); + } + } } } @@ -69,6 +94,7 @@ Map::Map(const Map& map) leftmap = map.leftmap; rightmap = map.rightmap; dirty = map.dirty; + objects = map.objects; } Map::Map(Map&& map) : Map() @@ -95,6 +121,7 @@ void swap(Map& first, Map& second) std::swap(first.leftmap, second.leftmap); std::swap(first.rightmap, second.rightmap); std::swap(first.dirty, second.dirty); + std::swap(first.objects, second.objects); } #define MY_ENCODING "ISO-8859-1" @@ -131,21 +158,39 @@ void Map::save(std::string name) rc = xmlTextWriterWriteElement(writer, (xmlChar*) "environment", (xmlChar*) mapdata_out.str().c_str()); if (rc < 0) throw MapWriteException(name); - if (leftmap != "") - { - rc = xmlTextWriterWriteElement(writer, (xmlChar*) "leftmap", (xmlChar*) leftmap.c_str()); - if (rc < 0) throw MapWriteException(name); - } + rc = xmlTextWriterWriteElement(writer, (xmlChar*) "leftmap", (xmlChar*) leftmap.c_str()); + if (rc < 0) throw MapWriteException(name); + + rc = xmlTextWriterWriteElement(writer, (xmlChar*) "rightmap", (xmlChar*) rightmap.c_str()); + if (rc < 0) throw MapWriteException(name); - if (rightmap != "") + rc = xmlTextWriterStartElement(writer, (xmlChar*) "entities"); + if (rc < 0) throw MapWriteException(name); + + for (auto object : objects) { - rc = xmlTextWriterWriteElement(writer, (xmlChar*) "rightmap", (xmlChar*) rightmap.c_str()); + rc = xmlTextWriterStartElement(writer, (xmlChar*) "entity"); + if (rc < 0) throw MapWriteException(name); + + rc = xmlTextWriterWriteElement(writer, (xmlChar*) "entity-type", (xmlChar*) object.object->getType().c_str()); + if (rc < 0) throw MapWriteException(name); + + std::ostringstream entpos_out; + entpos_out << object.position.first << "," << object.position.second; + + rc = xmlTextWriterWriteElement(writer, (xmlChar*) "entity-position", (xmlChar*) entpos_out.str().c_str()); + if (rc < 0) throw MapWriteException(name); + + rc = xmlTextWriterEndElement(writer); if (rc < 0) throw MapWriteException(name); } rc = xmlTextWriterEndElement(writer); if (rc < 0) throw MapWriteException(name); + rc = xmlTextWriterEndElement(writer); + if (rc < 0) throw MapWriteException(name); + rc = xmlTextWriterEndDocument(writer); if (rc < 0) throw MapWriteException(name); @@ -180,3 +225,8 @@ void Map::setTitle(std::string title) dirty = true; this->title = title; } + +std::list Map::getObjects() +{ + return objects; +} -- cgit 1.4.1