diff options
Diffstat (limited to 'tools/mapedit/src/map.cpp')
| -rw-r--r-- | tools/mapedit/src/map.cpp | 94 |
1 files changed, 94 insertions, 0 deletions
| diff --git a/tools/mapedit/src/map.cpp b/tools/mapedit/src/map.cpp new file mode 100644 index 0000000..52a2096 --- /dev/null +++ b/tools/mapedit/src/map.cpp | |||
| @@ -0,0 +1,94 @@ | |||
| 1 | #include "map.h" | ||
| 2 | #include <libxml/parser.h> | ||
| 3 | |||
| 4 | Map::Map() | ||
| 5 | { | ||
| 6 | mapdata = (int*) calloc(MAP_WIDTH * MAP_HEIGHT, sizeof(int)); | ||
| 7 | } | ||
| 8 | |||
| 9 | Map::Map(const std::string filename) | ||
| 10 | { | ||
| 11 | xmlDocPtr doc = xmlParseFile(filename.c_str()); | ||
| 12 | if (doc == nullptr) | ||
| 13 | { | ||
| 14 | throw MapLoadException(filename); | ||
| 15 | } | ||
| 16 | |||
| 17 | xmlNodePtr top = xmlDocGetRootElement(doc); | ||
| 18 | if (top == nullptr) | ||
| 19 | { | ||
| 20 | throw MapLoadException(filename); | ||
| 21 | } | ||
| 22 | |||
| 23 | if (xmlStrcmp(top->name, (const xmlChar*) "map-def")) | ||
| 24 | { | ||
| 25 | throw MapLoadException(filename); | ||
| 26 | } | ||
| 27 | |||
| 28 | for (xmlNodePtr node = top->xmlChildrenNode; node != NULL; node = node->next) | ||
| 29 | { | ||
| 30 | if (!xmlStrcmp(node->name, (const xmlChar*) "name")) | ||
| 31 | { | ||
| 32 | xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); | ||
| 33 | title = (char*) key; | ||
| 34 | xmlFree(key); | ||
| 35 | } else if (!xmlStrcmp(node->name, (const xmlChar*) "environment")) | ||
| 36 | { | ||
| 37 | xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); | ||
| 38 | mapdata = (int*) malloc(MAP_WIDTH*MAP_HEIGHT*sizeof(int)); | ||
| 39 | mapdata[0] = atoi(strtok((char*) key, ",\n")); | ||
| 40 | for (int i=1; i<(MAP_WIDTH*MAP_HEIGHT); i++) | ||
| 41 | { | ||
| 42 | mapdata[i] = atoi(strtok(NULL, ",\n")); | ||
| 43 | } | ||
| 44 | xmlFree(key); | ||
| 45 | } else if (!xmlStrcmp(node->name, (const xmlChar*) "leftmap")) | ||
| 46 | { | ||
| 47 | xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); | ||
| 48 | leftmap = (char*) key; | ||
| 49 | xmlFree(key); | ||
| 50 | } else if (!xmlStrcmp(node->name, (const xmlChar*) "rightmap")) | ||
| 51 | { | ||
| 52 | xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); | ||
| 53 | rightmap = (char*) key; | ||
| 54 | xmlFree(key); | ||
| 55 | } | ||
| 56 | } | ||
| 57 | |||
| 58 | xmlFreeDoc(doc); | ||
| 59 | } | ||
| 60 | |||
| 61 | Map::Map(const Map& map) | ||
| 62 | { | ||
| 63 | mapdata = (int*) malloc(MAP_WIDTH*MAP_HEIGHT*sizeof(int)); | ||
| 64 | memcpy(mapdata, map.mapdata, MAP_WIDTH*MAP_HEIGHT*sizeof(int)); | ||
| 65 | |||
| 66 | title = map.title; | ||
| 67 | leftmap = map.leftmap; | ||
| 68 | rightmap = map.rightmap; | ||
| 69 | } | ||
| 70 | |||
| 71 | Map::Map(Map&& map) : Map() | ||
| 72 | { | ||
| 73 | swap(*this, map); | ||
| 74 | } | ||
| 75 | |||
| 76 | Map::~Map() | ||
| 77 | { | ||
| 78 | free(mapdata); | ||
| 79 | } | ||
| 80 | |||
| 81 | Map& Map::operator= (Map map) | ||
| 82 | { | ||
| 83 | swap(*this, map); | ||
| 84 | |||
| 85 | return *this; | ||
| 86 | } | ||
| 87 | |||
| 88 | void swap(Map& first, Map& second) | ||
| 89 | { | ||
| 90 | std::swap(first.mapdata, second.mapdata); | ||
| 91 | std::swap(first.title, second.title); | ||
| 92 | std::swap(first.leftmap, second.leftmap); | ||
| 93 | std::swap(first.rightmap, second.rightmap); | ||
| 94 | } \ No newline at end of file | ||
