summary refs log tree commit diff stats
diff options
context:
space:
mode:
authorKelly Rauchenberger <fefferburbia@gmail.com>2015-03-15 20:58:02 -0400
committerKelly Rauchenberger <fefferburbia@gmail.com>2015-03-15 20:58:02 -0400
commit25240241e91dc913d20fbb93aa4acc9433dda6a0 (patch)
treede4f80fefc08d30716f564872e296748c7cdd452
parent440df3425dcc2376755d622ad6fd965eb25a25c2 (diff)
downloadtherapy-25240241e91dc913d20fbb93aa4acc9433dda6a0.tar.gz
therapy-25240241e91dc913d20fbb93aa4acc9433dda6a0.tar.bz2
therapy-25240241e91dc913d20fbb93aa4acc9433dda6a0.zip
Map editor can now save and load (but not edit) entities in maps
-rw-r--r--tools/mapedit/CMakeLists.txt1
-rw-r--r--tools/mapedit/src/map.cpp64
-rw-r--r--tools/mapedit/src/map.h11
-rw-r--r--tools/mapedit/src/object.cpp99
-rw-r--r--tools/mapedit/src/object.h47
-rw-r--r--tools/mapedit/src/widget.cpp12
6 files changed, 227 insertions, 7 deletions
diff --git a/tools/mapedit/CMakeLists.txt b/tools/mapedit/CMakeLists.txt index ffc9f05..9343f06 100644 --- a/tools/mapedit/CMakeLists.txt +++ b/tools/mapedit/CMakeLists.txt
@@ -43,6 +43,7 @@ add_executable(AromatherapyMapEditor
43 src/frame.cpp 43 src/frame.cpp
44 src/widget.cpp 44 src/widget.cpp
45 src/tile_widget.cpp 45 src/tile_widget.cpp
46 src/object.cpp
46) 47)
47target_link_libraries(AromatherapyMapEditor ${ALL_LIBS}) 48target_link_libraries(AromatherapyMapEditor ${ALL_LIBS})
48install(TARGETS AromatherapyMapEditor RUNTIME DESTINATION ${BIN_DIR}) 49install(TARGETS AromatherapyMapEditor RUNTIME DESTINATION ${BIN_DIR})
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)
54 xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1); 54 xmlChar* key = xmlNodeListGetString(doc, node->xmlChildrenNode, 1);
55 rightmap = (char*) key; 55 rightmap = (char*) key;
56 xmlFree(key); 56 xmlFree(key);
57 } else if (!xmlStrcmp(node->name, (const xmlChar*) "entities"))
58 {
59 for (xmlNodePtr entityNode = node->xmlChildrenNode; entityNode != NULL; entityNode = entityNode->next)
60 {
61 if (!xmlStrcmp(entityNode->name, (const xmlChar*) "entity"))
62 {
63 MapObjectEntry data;
64 for (xmlNodePtr entityDataNode = entityNode->xmlChildrenNode; entityDataNode != NULL; entityDataNode = entityDataNode->next)
65 {
66 if (!xmlStrcmp(entityDataNode->name, (const xmlChar*) "entity-type"))
67 {
68 xmlChar* key = xmlNodeListGetString(doc, entityDataNode->xmlChildrenNode, 1);
69 data.object = MapObject::getAllObjects().at((char*) key);
70 xmlFree(key);
71 } else if (!xmlStrcmp(entityDataNode->name, (const xmlChar*) "entity-position"))
72 {
73 xmlChar* key = xmlNodeListGetString(doc, entityDataNode->xmlChildrenNode, 1);
74 sscanf((char*) key, "%lf,%lf", &data.position.first, &data.position.second);
75 xmlFree(key);
76 }
77 }
78
79 objects.push_back(data);
80 }
81 }
57 } 82 }
58 } 83 }
59 84
@@ -69,6 +94,7 @@ Map::Map(const Map& map)
69 leftmap = map.leftmap; 94 leftmap = map.leftmap;
70 rightmap = map.rightmap; 95 rightmap = map.rightmap;
71 dirty = map.dirty; 96 dirty = map.dirty;
97 objects = map.objects;
72} 98}
73 99
74Map::Map(Map&& map) : Map() 100Map::Map(Map&& map) : Map()
@@ -95,6 +121,7 @@ void swap(Map& first, Map& second)
95 std::swap(first.leftmap, second.leftmap); 121 std::swap(first.leftmap, second.leftmap);
96 std::swap(first.rightmap, second.rightmap); 122 std::swap(first.rightmap, second.rightmap);
97 std::swap(first.dirty, second.dirty); 123 std::swap(first.dirty, second.dirty);
124 std::swap(first.objects, second.objects);
98} 125}
99 126
100#define MY_ENCODING "ISO-8859-1" 127#define MY_ENCODING "ISO-8859-1"
@@ -131,21 +158,39 @@ void Map::save(std::string name)
131 rc = xmlTextWriterWriteElement(writer, (xmlChar*) "environment", (xmlChar*) mapdata_out.str().c_str()); 158 rc = xmlTextWriterWriteElement(writer, (xmlChar*) "environment", (xmlChar*) mapdata_out.str().c_str());
132 if (rc < 0) throw MapWriteException(name); 159 if (rc < 0) throw MapWriteException(name);
133 160
134 if (leftmap != "") 161 rc = xmlTextWriterWriteElement(writer, (xmlChar*) "leftmap", (xmlChar*) leftmap.c_str());
135 { 162 if (rc < 0) throw MapWriteException(name);
136 rc = xmlTextWriterWriteElement(writer, (xmlChar*) "leftmap", (xmlChar*) leftmap.c_str()); 163
137 if (rc < 0) throw MapWriteException(name); 164 rc = xmlTextWriterWriteElement(writer, (xmlChar*) "rightmap", (xmlChar*) rightmap.c_str());
138 } 165 if (rc < 0) throw MapWriteException(name);
139 166
140 if (rightmap != "") 167 rc = xmlTextWriterStartElement(writer, (xmlChar*) "entities");
168 if (rc < 0) throw MapWriteException(name);
169
170 for (auto object : objects)
141 { 171 {
142 rc = xmlTextWriterWriteElement(writer, (xmlChar*) "rightmap", (xmlChar*) rightmap.c_str()); 172 rc = xmlTextWriterStartElement(writer, (xmlChar*) "entity");
173 if (rc < 0) throw MapWriteException(name);
174
175 rc = xmlTextWriterWriteElement(writer, (xmlChar*) "entity-type", (xmlChar*) object.object->getType().c_str());
176 if (rc < 0) throw MapWriteException(name);
177
178 std::ostringstream entpos_out;
179 entpos_out << object.position.first << "," << object.position.second;
180
181 rc = xmlTextWriterWriteElement(writer, (xmlChar*) "entity-position", (xmlChar*) entpos_out.str().c_str());
182 if (rc < 0) throw MapWriteException(name);
183
184 rc = xmlTextWriterEndElement(writer);
143 if (rc < 0) throw MapWriteException(name); 185 if (rc < 0) throw MapWriteException(name);
144 } 186 }
145 187
146 rc = xmlTextWriterEndElement(writer); 188 rc = xmlTextWriterEndElement(writer);
147 if (rc < 0) throw MapWriteException(name); 189 if (rc < 0) throw MapWriteException(name);
148 190
191 rc = xmlTextWriterEndElement(writer);
192 if (rc < 0) throw MapWriteException(name);
193
149 rc = xmlTextWriterEndDocument(writer); 194 rc = xmlTextWriterEndDocument(writer);
150 if (rc < 0) throw MapWriteException(name); 195 if (rc < 0) throw MapWriteException(name);
151 196
@@ -180,3 +225,8 @@ void Map::setTitle(std::string title)
180 dirty = true; 225 dirty = true;
181 this->title = title; 226 this->title = title;
182} 227}
228
229std::list<MapObjectEntry> Map::getObjects()
230{
231 return objects;
232}
diff --git a/tools/mapedit/src/map.h b/tools/mapedit/src/map.h index 45ec7e1..52e047d 100644 --- a/tools/mapedit/src/map.h +++ b/tools/mapedit/src/map.h
@@ -3,6 +3,10 @@
3 3
4#include <string> 4#include <string>
5#include <exception> 5#include <exception>
6#include <utility>
7#include <list>
8#include "object.h"
9#include <memory>
6 10
7const int TILE_WIDTH = 8; 11const int TILE_WIDTH = 8;
8const int TILE_HEIGHT = 8; 12const int TILE_HEIGHT = 8;
@@ -39,6 +43,11 @@ class MapWriteException: public std::exception
39 std::string mapname; 43 std::string mapname;
40}; 44};
41 45
46struct MapObjectEntry {
47 std::shared_ptr<MapObject> object;
48 std::pair<double, double> position;
49};
50
42class Map { 51class Map {
43 public: 52 public:
44 Map(); 53 Map();
@@ -55,8 +64,10 @@ class Map {
55 bool hasUnsavedChanges() const; 64 bool hasUnsavedChanges() const;
56 void setTileAt(int x, int y, int tile); 65 void setTileAt(int x, int y, int tile);
57 int getTileAt(int x, int y) const; 66 int getTileAt(int x, int y) const;
67 std::list<MapObjectEntry> getObjects();
58 68
59 private: 69 private:
70 std::list<MapObjectEntry> objects;
60 int* mapdata; 71 int* mapdata;
61 std::string title; 72 std::string title;
62 std::string leftmap; 73 std::string leftmap;
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
6static std::map<std::string, std::shared_ptr<MapObject>> allObjects;
7static bool objsInit = false;
8
9const 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
36MapObject::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
76wxBitmap MapObject::getSprite() const
77{
78 return sprite;
79}
80
81std::string MapObject::getAction() const
82{
83 return action;
84}
85
86int MapObject::getWidth() const
87{
88 return width;
89}
90
91int MapObject::getHeight() const
92{
93 return height;
94}
95
96std::string MapObject::getType() const
97{
98 return type;
99}
diff --git a/tools/mapedit/src/object.h b/tools/mapedit/src/object.h new file mode 100644 index 0000000..bfb493c --- /dev/null +++ b/tools/mapedit/src/object.h
@@ -0,0 +1,47 @@
1#ifndef OBJECT_H
2#define OBJECT_H
3
4#include <wx/wxprec.h>
5
6#ifndef WX_PRECOMP
7#include <wx/wx.h>
8#endif
9
10#include <string>
11#include <map>
12
13class MapObjectLoadException: public std::exception
14{
15 public:
16 MapObjectLoadException(std::string mapname) : mapname(mapname) {}
17
18 virtual const char* what() const throw()
19 {
20 return ("An error occured loading map object " + mapname).c_str();
21 }
22
23 private:
24 std::string mapname;
25};
26
27class MapObject {
28 public:
29 MapObject(const char* filename);
30
31 static const std::map<std::string, std::shared_ptr<MapObject>> getAllObjects();
32
33 std::string getType() const;
34 wxBitmap getSprite() const;
35 std::string getAction() const;
36 int getWidth() const;
37 int getHeight() const;
38
39 private:
40 std::string type;
41 wxBitmap sprite;
42 std::string action;
43 int width;
44 int height;
45};
46
47#endif
diff --git a/tools/mapedit/src/widget.cpp b/tools/mapedit/src/widget.cpp index 9c8dae3..d7f1a51 100644 --- a/tools/mapedit/src/widget.cpp +++ b/tools/mapedit/src/widget.cpp
@@ -53,12 +53,24 @@ void MapeditWidget::OnPaint(wxPaintEvent& event)
53 } 53 }
54 } 54 }
55 55
56 for (auto object : map->getObjects())
57 {
58 tiles_dc.SelectObject(wxNullBitmap);
59
60 wxBitmap sprite = object.object->getSprite();
61 tiles_dc.SelectObject(sprite);
62
63 dc.StretchBlit(object.position.first*scale-vX, object.position.second*scale-vY, object.object->getWidth()*scale, object.object->getHeight()*scale, &tiles_dc, 0, 0, object.object->getWidth(), object.object->getHeight());
64 }
65
56 if (mouseIsIn) 66 if (mouseIsIn)
57 { 67 {
58 int tile = tileWidget->getSelected(); 68 int tile = tileWidget->getSelected();
59 int x = (mousePos.x + vX) / (TILE_WIDTH * scale); 69 int x = (mousePos.x + vX) / (TILE_WIDTH * scale);
60 int y = (mousePos.y + vY) / (TILE_HEIGHT * scale); 70 int y = (mousePos.y + vY) / (TILE_HEIGHT * scale);
61 71
72 tiles_dc.SelectObject(wxNullBitmap);
73 tiles_dc.SelectObject(tiles);
62 dc.StretchBlit(x*TILE_WIDTH*scale-vX, y*TILE_HEIGHT*scale-vY, TILE_WIDTH*scale, TILE_HEIGHT*scale, &tiles_dc, tile%8*TILE_WIDTH, tile/8*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT); 74 dc.StretchBlit(x*TILE_WIDTH*scale-vX, y*TILE_HEIGHT*scale-vY, TILE_WIDTH*scale, TILE_HEIGHT*scale, &tiles_dc, tile%8*TILE_WIDTH, tile/8*TILE_HEIGHT, TILE_WIDTH, TILE_HEIGHT);
63 75
64 wxPen pen(*wxGREEN, 2); 76 wxPen pen(*wxGREEN, 2);