map loads, but renders tiles incorrectly
[Tsunagari.git] / src / area.cpp
blobb91403f6cee4c7ba20316245ec8caf6f03dee276
1 /******************************
2 ** Tsunagari Tile Engine **
3 ** area.cpp **
4 ** Copyright 2011 OmegaSDG **
5 ******************************/
7 #include <boost/shared_ptr.hpp>
8 #include <libxml/parser.h>
9 #include <libxml/tree.h>
11 #include "area.h"
12 #include "common.h"
13 #include "entity.h"
14 #include "log.h"
15 #include "resourcer.h"
16 #include "sprite.h"
18 Area::Area(Resourcer* rc, Entity* player, const std::string descriptor)
19 : rc(rc), player(player), descriptor(descriptor)
21 dim.z = 0;
24 Area::~Area()
28 bool Area::init()
30 if (!processDescriptor()) // Try to load in descriptor.
31 return false;
32 return true;
35 void Area::buttonDown(const Gosu::Button btn)
37 if (btn == Gosu::kbRight)
38 player->moveByTile(coord(1, 0, 0));
39 else if (btn == Gosu::kbLeft)
40 player->moveByTile(coord(-1, 0, 0));
41 else if (btn == Gosu::kbUp)
42 player->moveByTile(coord(0, -1, 0));
43 else if (btn == Gosu::kbDown)
44 player->moveByTile(coord(0, 1, 0));
47 void Area::draw()
49 for (unsigned int layer = 0; layer != map.size(); layer++)
51 grid_t grid = map[layer];
52 for (unsigned int y = 0; y != grid.size(); y++)
54 row_t row = grid[layer];
55 for (unsigned int x = 0; x != row.size(); x++)
57 // TODO support animations
58 Tile* tile = row[x];
59 Gosu::Image* img = tile->type->graphics[0];
60 img->draw(x*img->width(), y*img->height(), 0);
64 player->draw();
67 bool Area::needsRedraw() const
69 return player->needsRedraw();
72 bool Area::processDescriptor()
74 xmlDoc* doc = rc->getXMLDoc(descriptor);
75 if (!doc)
76 return false;
78 // use RAII to ensure doc is freed
79 boost::shared_ptr<void> alwaysFreeTheDoc(doc, xmlFreeDoc);
81 // Iterate and process children of <map>
82 xmlNode* root = xmlDocGetRootElement(doc); // <map> element
84 xmlChar* width = xmlGetProp(root, BAD_CAST("width"));
85 xmlChar* height = xmlGetProp(root, BAD_CAST("height"));
86 dim.x = atol((const char*)width);
87 dim.y = atol((const char*)height);
89 xmlNode* child = root->xmlChildrenNode;
90 for (; child != NULL; child = child->next) {
91 if (!xmlStrncmp(child->name, BAD_CAST("properties"), 11)) {
92 if (!processMapProperties(child))
93 return false;
95 else if (!xmlStrncmp(child->name, BAD_CAST("tileset"), 8)) {
96 if (!processTileset(child))
97 return false;
99 else if (!xmlStrncmp(child->name, BAD_CAST("layer"), 6)) {
100 if (!processLayer(child))
101 return false;
103 else if (!xmlStrncmp(child->name, BAD_CAST("objectgroup"), 12)) {
104 if (!processObjectGroup(child))
105 return false;
109 return true;
112 bool Area::processMapProperties(xmlNode* node)
116 <properties>
117 <property name="areaspec" value="1"/>
118 <property name="author" value="Michael D. Reiley"/>
119 <property name="name" value="Baby's First Area"/>
120 <property name="music_loop" value="true"/>
121 <property name="music_main" value="wind.music"/>
122 <property name="onLoad" value="babysfirst_init()"/>
123 <property name="scripts" value="areainits.event,test.event"/>
124 </properties>
127 xmlNode* child = node->xmlChildrenNode;
128 for (; child != NULL; child = child->next) {
129 xmlChar* name = xmlGetProp(child, BAD_CAST("name"));
130 xmlChar* value = xmlGetProp(child, BAD_CAST("value"));
131 if (!xmlStrncmp(name, BAD_CAST("author"), 7))
132 author = (const char*)value;
133 else if (!xmlStrncmp(name, BAD_CAST("name"), 5))
134 this->name = (const char*)value;
135 else if (!xmlStrncmp(name, BAD_CAST("music_loop"), 11))
136 main.loop = parseBool((const char*)value);
137 else if (!xmlStrncmp(name, BAD_CAST("music_main"), 11))
138 main.filename = (const char*)value;
139 else if (!xmlStrncmp(name, BAD_CAST("onLoad"), 7))
140 onLoadEvents = (const char*)value;
141 else if (!xmlStrncmp(name, BAD_CAST("scripts"), 8))
142 scripts = (const char*)value; // TODO split(), load
144 return true;
147 bool Area::processTileset(xmlNode* node)
151 <tileset firstgid="1" name="tiles.sheet" tilewidth="64" tileheight="64">
152 <image source="tiles.sheet" width="256" height="256"/>
153 <tile id="14">
155 </tile>
156 </tileset>
158 Tileset ts;
159 xmlChar* width = xmlGetProp(node, BAD_CAST("tilewidth"));
160 xmlChar* height = xmlGetProp(node, BAD_CAST("tileheight"));
161 ts.tiledim.x = atol((const char*)width);
162 ts.tiledim.y = atol((const char*)height);
164 xmlNode* child = node->xmlChildrenNode;
165 for (; child != NULL; child = child->next) {
166 if (!xmlStrncmp(child->name, BAD_CAST("tile"), 5)) {
167 xmlChar* idstr = xmlGetProp(child, BAD_CAST("id"));
168 unsigned id = atol((const char*)idstr);
170 // Undeclared TileTypes have default properties.
171 while (ts.defaults.size() != id) {
172 TileType tt = defaultTileType(ts.source,
173 ts.tiledim, ts.defaults.size());
174 ts.defaults.push_back(tt);
177 // Handle explicit TileType
178 if (!processTileType(child, ts))
179 return false;
181 else if (!xmlStrncmp(child->name, BAD_CAST("image"), 6)) {
182 xmlChar* source = xmlGetProp(child, BAD_CAST("source"));
183 ts.source = rc->getBitmap((const char*)source);
187 tilesets.push_back(ts);
188 return true;
191 Area::TileType Area::defaultTileType(const Gosu::Bitmap source, coord_t tiledim,
192 int id)
194 int x = tiledim.x * (id % source.width());
195 int y = tiledim.y * (id / source.height());
197 TileType tt;
198 Gosu::Image* img = rc->bitmapSection(source, x, y,
199 tiledim.x, tiledim.y, true);
200 tt.graphics.push_back(img);
201 tt.animated = false;
202 tt.ani_speed = 0.0;
203 return tt;
206 bool Area::processTileType(xmlNode* node, Tileset& ts)
209 <tile id="8">
210 <properties>
211 <property name="flags" value="nowalk"/>
212 <property name="onEnter" value="skid();speed(2)"/>
213 <property name="onLeave" value="undo()"/>
214 </properties>
215 </tile>
216 <tile id="14">
217 <properties>
218 <property name="animated" value="1"/>
219 <property name="size" value="2"/>
220 <property name="speed" value="2"/>
221 </properties>
222 </tile>
225 // Initialize a default TileType, we'll build on that.
226 TileType tt = defaultTileType(ts.source,
227 ts.tiledim, ts.defaults.size());
229 xmlChar* idstr = xmlGetProp(node, BAD_CAST("id"));
230 unsigned id = atol((const char*)idstr);
231 if (id != ts.defaults.size()) {
232 // XXX we need to know the Area we're loading...
233 Log::err("unknown area", std::string("expected TileType id ") +
234 itostr(ts.defaults.size()) + ", but got " + itostr(id));
235 return false;
238 xmlNode* child = node->xmlChildrenNode; // <properties>
239 child = node->xmlChildrenNode; // <property>
240 for (; child != NULL; child = child->next) {
241 xmlChar* name = xmlGetProp(child, BAD_CAST("name"));
242 xmlChar* value = xmlGetProp(child, BAD_CAST("value"));
243 if (!xmlStrncmp(child->name, BAD_CAST("flags"), 6)) {
244 // TODO flags
246 else if (!xmlStrncmp(name, BAD_CAST("onEnter"), 8)) {
247 // TODO events
249 else if (!xmlStrncmp(name, BAD_CAST("onLeave"), 8)) {
250 // TODO events
252 else if (!xmlStrncmp(name, BAD_CAST("animated"), 9)) {
253 tt.animated = parseBool((const char*)value);
255 else if (!xmlStrncmp(name, BAD_CAST("size"), 5)) {
256 // TODO animation
258 else if (!xmlStrncmp(name, BAD_CAST("speed"), 6)) {
259 tt.ani_speed = atol((const char*)value);
263 ts.defaults.push_back(tt);
264 return true;
267 bool Area::processLayer(xmlNode* node)
271 <layer name="Tiles0" width="5" height="5">
272 <properties>
274 </properties>
275 <data>
276 <tile gid="9"/>
277 <tile gid="9"/>
278 <tile gid="9"/>
280 <tile gid="3"/>
281 <tile gid="9"/>
282 <tile gid="9"/>
283 </data>
284 </layer>
287 xmlChar* width = xmlGetProp(node, BAD_CAST("width"));
288 xmlChar* height = xmlGetProp(node, BAD_CAST("height"));
289 unsigned x = atol((const char*)width);
290 unsigned y = atol((const char*)height);
292 if (dim.x != x || dim.y != y) {
293 // XXX we need to know the Area we're loading...
294 Log::err("unknown area", "layer x,y size != map x,y size");
295 return false;
298 xmlNode* child = node->xmlChildrenNode;
299 for (; child != NULL; child = child->next) {
300 if (!xmlStrncmp(child->name, BAD_CAST("properties"), 11)) {
301 if (!processLayerProperties(child))
302 return false;
304 else if (!xmlStrncmp(child->name, BAD_CAST("data"), 5)) {
305 if (!processLayerData(child))
306 return false;
309 return true;
312 bool Area::processLayerProperties(xmlNode* node)
316 <properties>
317 <property name="layer" value="0"/>
318 </properties>
321 xmlNode* child = node->xmlChildrenNode;
322 for (; child != NULL; child = child->next) {
323 xmlChar* name = xmlGetProp(child, BAD_CAST("name"));
324 xmlChar* value = xmlGetProp(child, BAD_CAST("value"));
325 if (!xmlStrncmp(name, BAD_CAST("layer"), 6)) {
326 unsigned depth = atol((const char*)value);
327 if (depth != dim.z) {
328 Log::err("unknown area", "invalid layer depth");
329 return false;
334 return true;
337 bool Area::processLayerData(xmlNode* node)
341 <data>
342 <tile gid="9"/>
343 <tile gid="9"/>
344 <tile gid="9"/>
346 <tile gid="3"/>
347 <tile gid="9"/>
348 <tile gid="9"/>
349 </data>
352 row_t row;
353 grid_t grid;
355 xmlNode* child = node->xmlChildrenNode;
356 for (int i = 1; child != NULL; i++, child = child->next) {
357 if (!xmlStrncmp(child->name, BAD_CAST("tile"), 5)) {
358 xmlChar* gidStr = xmlGetProp(child, BAD_CAST("gid"));
359 unsigned gid = atol((const char*)gidStr);
360 Tile* t = new Tile;
361 t->type = &tilesets[0].defaults[gid]; // XXX can only access first tileset
362 row.push_back(t);
363 if (i % dim.x == 0) {
364 grid.push_back(row);
365 row.clear();
370 map.push_back(grid);
371 dim.z++;
372 return true;
375 bool Area::processObjectGroup(xmlNode*)
377 return true;
380 coord_t Area::getDimensions() const
382 return dim;
385 Area::Tile* Area::getTile(coord_t c)
387 return map[c.z][c.y][c.x];