From 91640f2f35d703898edb14abaae7dd63f5346027 Mon Sep 17 00:00:00 2001 From: Kelly Rauchenberger Date: Sun, 15 Mar 2015 17:20:42 -0400 Subject: Added file management to map editor (only edits environment at current time) --- tools/mapedit/src/map.cpp | 84 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 82 insertions(+), 2 deletions(-) (limited to 'tools/mapedit/src/map.cpp') diff --git a/tools/mapedit/src/map.cpp b/tools/mapedit/src/map.cpp index 52a2096..7976419 100644 --- a/tools/mapedit/src/map.cpp +++ b/tools/mapedit/src/map.cpp @@ -1,12 +1,15 @@ #include "map.h" #include +#include +#include Map::Map() { mapdata = (int*) calloc(MAP_WIDTH * MAP_HEIGHT, sizeof(int)); + dirty = true; } -Map::Map(const std::string filename) +Map::Map(std::string filename) { xmlDocPtr doc = xmlParseFile(filename.c_str()); if (doc == nullptr) @@ -56,6 +59,8 @@ Map::Map(const std::string filename) } xmlFreeDoc(doc); + + dirty = false; } Map::Map(const Map& map) @@ -66,6 +71,7 @@ Map::Map(const Map& map) title = map.title; leftmap = map.leftmap; rightmap = map.rightmap; + dirty = map.dirty; } Map::Map(Map&& map) : Map() @@ -91,4 +97,78 @@ void swap(Map& first, Map& second) std::swap(first.title, second.title); std::swap(first.leftmap, second.leftmap); std::swap(first.rightmap, second.rightmap); -} \ No newline at end of file + std::swap(first.dirty, second.dirty); +} + +#define MY_ENCODING "ISO-8859-1" + +void Map::save(std::string name) +{ + if (!dirty) return; + + int rc; + + xmlTextWriterPtr writer = xmlNewTextWriterFilename(name.c_str(), 0); + if (writer == NULL) throw MapWriteException(name); + + rc = xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL); + if (rc < 0) throw MapWriteException(name); + + rc = xmlTextWriterStartElement(writer, (xmlChar*) "map-def"); + if (rc < 0) throw MapWriteException(name); + + rc = xmlTextWriterWriteElement(writer, (xmlChar*) "name", (xmlChar*) title.c_str()); + if (rc < 0) throw MapWriteException(name); + + std::ostringstream mapdata_out; + for (int y=0; y