diff options
Diffstat (limited to 'tools/mapedit/src/object.cpp')
-rw-r--r-- | tools/mapedit/src/object.cpp | 99 |
1 files changed, 99 insertions, 0 deletions
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 @@ | |||
1 | #include "object.h" | ||
2 | #include <dirent.h> | ||
3 | #include <libxml/parser.h> | ||
4 | #include <memory> | ||
5 | |||
6 | static std::map<std::string, std::shared_ptr<MapObject>> allObjects; | ||
7 | static bool objsInit = false; | ||
8 | |||
9 | const std::map<std::string, std::shared_ptr<MapObject>> MapObject::getAllObjects() | ||
10 | { | ||
11 | if (!objsInit) | ||
12 | { | ||
13 | DIR* dir = opendir("../../../entities/"); | ||
14 | if (dir != NULL) | ||
15 | { | ||
16 | struct dirent* ent; | ||
17 | while ((ent = readdir(dir)) != NULL) | ||
18 | { | ||
19 | std::string path = ent->d_name; | ||
20 | if ((path.length() >= 4) && (path.substr(path.length() - 4, 4) == ".xml")) | ||
21 | { | ||
22 | std::string name = path.substr(0, path.length() - 4); | ||
23 | auto obj = std::make_shared<MapObject>(name.c_str()); | ||
24 | |||
25 | allObjects[name] = obj; | ||
26 | } | ||
27 | } | ||
28 | } | ||
29 | |||
30 | objsInit = true; | ||
31 | } | ||
32 | |||
33 | return allObjects; | ||
34 | } | ||
35 | |||
36 | MapObject::MapObject(const char* filename) | ||
37 | { | ||
38 | type = filename; | ||
39 | |||
40 | xmlDocPtr doc = xmlParseFile(("../../../entities/" + std::string(filename) + ".xml").c_str()); | ||
41 | if (doc == nullptr) throw MapObjectLoadException(filename); | ||
42 | |||
43 | xmlNodePtr top = xmlDocGetRootElement(doc); | ||
44 | if (top == nullptr) throw MapObjectLoadException(filename); | ||
45 | |||
46 | if (xmlStrcmp(top->name, (const xmlChar*) "entity-def")) | ||
47 | { | ||
48 | throw MapObjectLoadException(filename); | ||
49 | } | ||
50 | |||
51 | for (xmlNodePtr node = top->xmlChildrenNode; node != NULL; node = node->next) | ||
52 | { | ||
53 | if (!xmlStrcmp(node->name, (const xmlChar*) "sprite")) | ||
54 | { | ||
55 | xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); | ||
56 | std::string spriteFile = (char*) key; | ||
57 | xmlFree(key); | ||
58 | |||
59 | sprite = wxImage("../../" + spriteFile); | ||
60 | } else if (!xmlStrcmp(node->name, (const xmlChar*) "action")) | ||
61 | { | ||
62 | xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); | ||
63 | action = (char*) key; | ||
64 | xmlFree(key); | ||
65 | } else if (!xmlStrcmp(node->name, (const xmlChar*) "size")) | ||
66 | { | ||
67 | xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); | ||
68 | sscanf((char*) key, "%d,%d", &width, &height); | ||
69 | xmlFree(key); | ||
70 | } | ||
71 | } | ||
72 | |||
73 | xmlFreeDoc(doc); | ||
74 | } | ||
75 | |||
76 | wxBitmap MapObject::getSprite() const | ||
77 | { | ||
78 | return sprite; | ||
79 | } | ||
80 | |||
81 | std::string MapObject::getAction() const | ||
82 | { | ||
83 | return action; | ||
84 | } | ||
85 | |||
86 | int MapObject::getWidth() const | ||
87 | { | ||
88 | return width; | ||
89 | } | ||
90 | |||
91 | int MapObject::getHeight() const | ||
92 | { | ||
93 | return height; | ||
94 | } | ||
95 | |||
96 | std::string MapObject::getType() const | ||
97 | { | ||
98 | return type; | ||
99 | } | ||