diff options
Diffstat (limited to 'tools/mapedit/src/map.cpp')
-rw-r--r-- | tools/mapedit/src/map.cpp | 84 |
1 files changed, 82 insertions, 2 deletions
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 @@ | |||
1 | #include "map.h" | 1 | #include "map.h" |
2 | #include <libxml/parser.h> | 2 | #include <libxml/parser.h> |
3 | #include <libxml/xmlwriter.h> | ||
4 | #include <sstream> | ||
3 | 5 | ||
4 | Map::Map() | 6 | Map::Map() |
5 | { | 7 | { |
6 | mapdata = (int*) calloc(MAP_WIDTH * MAP_HEIGHT, sizeof(int)); | 8 | mapdata = (int*) calloc(MAP_WIDTH * MAP_HEIGHT, sizeof(int)); |
9 | dirty = true; | ||
7 | } | 10 | } |
8 | 11 | ||
9 | Map::Map(const std::string filename) | 12 | Map::Map(std::string filename) |
10 | { | 13 | { |
11 | xmlDocPtr doc = xmlParseFile(filename.c_str()); | 14 | xmlDocPtr doc = xmlParseFile(filename.c_str()); |
12 | if (doc == nullptr) | 15 | if (doc == nullptr) |
@@ -56,6 +59,8 @@ Map::Map(const std::string filename) | |||
56 | } | 59 | } |
57 | 60 | ||
58 | xmlFreeDoc(doc); | 61 | xmlFreeDoc(doc); |
62 | |||
63 | dirty = false; | ||
59 | } | 64 | } |
60 | 65 | ||
61 | Map::Map(const Map& map) | 66 | Map::Map(const Map& map) |
@@ -66,6 +71,7 @@ Map::Map(const Map& map) | |||
66 | title = map.title; | 71 | title = map.title; |
67 | leftmap = map.leftmap; | 72 | leftmap = map.leftmap; |
68 | rightmap = map.rightmap; | 73 | rightmap = map.rightmap; |
74 | dirty = map.dirty; | ||
69 | } | 75 | } |
70 | 76 | ||
71 | Map::Map(Map&& map) : Map() | 77 | Map::Map(Map&& map) : Map() |
@@ -91,4 +97,78 @@ void swap(Map& first, Map& second) | |||
91 | std::swap(first.title, second.title); | 97 | std::swap(first.title, second.title); |
92 | std::swap(first.leftmap, second.leftmap); | 98 | std::swap(first.leftmap, second.leftmap); |
93 | std::swap(first.rightmap, second.rightmap); | 99 | std::swap(first.rightmap, second.rightmap); |
94 | } \ No newline at end of file | 100 | std::swap(first.dirty, second.dirty); |
101 | } | ||
102 | |||
103 | #define MY_ENCODING "ISO-8859-1" | ||
104 | |||
105 | void Map::save(std::string name) | ||
106 | { | ||
107 | if (!dirty) return; | ||
108 | |||
109 | int rc; | ||
110 | |||
111 | xmlTextWriterPtr writer = xmlNewTextWriterFilename(name.c_str(), 0); | ||
112 | if (writer == NULL) throw MapWriteException(name); | ||
113 | |||
114 | rc = xmlTextWriterStartDocument(writer, NULL, MY_ENCODING, NULL); | ||
115 | if (rc < 0) throw MapWriteException(name); | ||
116 | |||
117 | rc = xmlTextWriterStartElement(writer, (xmlChar*) "map-def"); | ||
118 | if (rc < 0) throw MapWriteException(name); | ||
119 | |||
120 | rc = xmlTextWriterWriteElement(writer, (xmlChar*) "name", (xmlChar*) title.c_str()); | ||
121 | if (rc < 0) throw MapWriteException(name); | ||
122 | |||
123 | std::ostringstream mapdata_out; | ||
124 | for (int y=0; y<MAP_HEIGHT; y++) | ||
125 | { | ||
126 | for (int x=0; x<MAP_WIDTH; x++) | ||
127 | { | ||
128 | mapdata_out << mapdata[x+y*MAP_WIDTH] << ","; | ||
129 | } | ||
130 | |||
131 | mapdata_out << std::endl; | ||
132 | } | ||
133 | |||
134 | rc = xmlTextWriterWriteElement(writer, (xmlChar*) "environment", (xmlChar*) mapdata_out.str().c_str()); | ||
135 | if (rc < 0) throw MapWriteException(name); | ||
136 | |||
137 | if (leftmap != "") | ||
138 | { | ||
139 | rc = xmlTextWriterWriteElement(writer, (xmlChar*) "leftmap", (xmlChar*) leftmap.c_str()); | ||
140 | if (rc < 0) throw MapWriteException(name); | ||
141 | } | ||
142 | |||
143 | if (rightmap != "") | ||
144 | { | ||
145 | rc = xmlTextWriterWriteElement(writer, (xmlChar*) "rightmap", (xmlChar*) rightmap.c_str()); | ||
146 | if (rc < 0) throw MapWriteException(name); | ||
147 | } | ||
148 | |||
149 | rc = xmlTextWriterEndElement(writer); | ||
150 | if (rc < 0) throw MapWriteException(name); | ||
151 | |||
152 | rc = xmlTextWriterEndDocument(writer); | ||
153 | if (rc < 0) throw MapWriteException(name); | ||
154 | |||
155 | xmlFreeTextWriter(writer); | ||
156 | |||
157 | dirty = false; | ||
158 | } | ||
159 | |||
160 | bool Map::hasUnsavedChanges() const | ||
161 | { | ||
162 | return dirty; | ||
163 | } | ||
164 | |||
165 | void Map::setTileAt(int x, int y, int tile) | ||
166 | { | ||
167 | dirty = true; | ||
168 | mapdata[x+y*MAP_WIDTH] = tile; | ||
169 | } | ||
170 | |||
171 | int Map::getTileAt(int x, int y) const | ||
172 | { | ||
173 | return mapdata[x+y*MAP_WIDTH]; | ||
174 | } | ||