/
tilemap.cpp
284 lines (225 loc) · 8.83 KB
/
tilemap.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
#include "tilemap.hpp"
#include "utils.hpp"
#include <iostream>
#include <stdexcept>
#include <map>
#include <utility>
#include <string>
#include "pugixml/pugixml.hpp"
using namespace pugi;
namespace Blit
{
Tilemap::Tilemap(const std::string& path) : dir(Utils::basedir(path))
{
xml_document doc;
if (!doc.load_file(path.c_str()))
throw std::runtime_error(Utils::join("Failed to load XML map: ", path, "."));
auto map = doc.child("map");
width = map.attribute("width").as_int();
height = map.attribute("height").as_int();
tilewidth = map.attribute("tilewidth").as_int();
tileheight = map.attribute("tileheight").as_int();
if (!width || !height || !tilewidth || !tileheight)
throw std::logic_error("Tilemap is malformed.");
std::map<unsigned, Surface> tiles;
for (auto set = map.child("tileset"); set; set = set.next_sibling("tileset"))
add_tileset(tiles, set);
for (auto layer = map.child("layer"); layer; layer = layer.next_sibling("layer"))
add_layer(tiles, layer, tilewidth, tileheight);
}
std::map<std::string, std::string> Tilemap::get_attributes(xml_node parent, const std::string& child) const
{
std::map<std::string, std::string> attrs;
for (auto node = parent.child(child.c_str()); node; node = node.next_sibling(child.c_str()))
{
auto name = node.attribute("name").value();
auto value = node.attribute("value").value();
attrs.insert({name, value});
}
return attrs;
}
void Tilemap::add_tileset(std::map<unsigned, Surface>& tiles, xml_node node)
{
int first_gid = node.attribute("firstgid").as_int();
int id_cnt = 0;
int tilewidth = node.attribute("tilewidth").as_int();
int tileheight = node.attribute("tileheight").as_int();
auto image = node.child("image");
auto source = image.attribute("source").value();
int width = image.attribute("width").as_int();
int height = image.attribute("height").as_int();
#if 0
std::cerr << "Adding tileset:" <<
" Name: " << node.attribute("name").value() <<
" Gid: " << first_gid <<
" Tilewidth: " << tilewidth <<
" Tileheight: " << tileheight <<
" Source: " << source <<
" Width: " << width <<
" Height: " << height << std::endl;
#endif
if (!width || !height || !tilewidth || !tileheight)
throw std::logic_error("Tilemap is malformed.");
SurfaceCache cache;
auto surf = cache.from_image(Utils::join(dir, "/", source));
if (surf.rect().w != width || surf.rect().h != height)
throw std::logic_error("Tilemap geometry does not correspond with image values.");
auto global_attr = get_attributes(node.child("properties"), "property");
#if 0
std::cerr << "Dumping attrs:" << std::endl;
for (auto& attr : global_attr)
std::cerr << "Found global attr (" << attr.first << " => " << attr.second << ")." << std::endl;
std::cerr << "Dumped attrs." << std::endl;
#endif
for (int y = 0; y < height; y += tileheight)
{
for (int x = 0; x < width; x += tilewidth, id_cnt++)
{
int id = first_gid + id_cnt;
tiles[id] = surf.sub({{x, y}, tilewidth, tileheight});
std::copy(std::begin(global_attr), std::end(global_attr), std::inserter(tiles[id].attr(), std::begin(tiles[id].attr())));
}
}
// Load all attributes for a tile into the surface.
for (auto tile = node.child("tile"); tile; tile = tile.next_sibling("tile"))
{
int id = first_gid + tile.attribute("id").as_int();
auto attrs = get_attributes(tile.child("properties"), "property");
std::copy(std::begin(global_attr), std::end(global_attr), std::inserter(attrs, std::begin(attrs)));
auto itr = attrs.find("sprite");
if (itr != std::end(attrs))
tiles[id] = cache.from_sprite(Utils::join(dir, "/", itr->second));
tiles[id].attr() = std::move(attrs);
}
}
void Tilemap::add_layer(std::map<unsigned, Surface>& tiles, xml_node node,
int tilewidth, int tileheight)
{
Layer layer;
int width = node.attribute("width").as_int();
int height = node.attribute("height").as_int();
if (!width || !height)
throw std::logic_error("Layer is empty.");
#if 0
std::cerr << "Adding layer:" <<
" Name: " << node.attribute("name").value() <<
" Width: " << width <<
" Height: " << height << std::endl;
#endif
Utils::xml_node_walker walk{node.child("data"), "tile", "gid"};
int index = 0;
for (auto& gid_str : walk)
{
Pos pos = {index % width, index / width};
unsigned gid = Utils::stoi(gid_str);
if (gid)
{
auto& surf = tiles[gid];
surf.rect().pos = pos * Pos{tilewidth, tileheight};
layer.cluster.vec().push_back({surf, Pos{}});
if (Utils::find_or_default(surf.attr(), "collision", "") == "true")
collisions.insert(pos);
}
index++;
}
layer.attr = get_attributes(node.child("properties"), "property");
layer.name = node.attribute("name").value();
m_layers.push_back(std::move(layer));
}
void Tilemap::pos(Pos position)
{
for (auto& layer : m_layers)
layer.cluster.pos(position);
Renderable::pos(position);
}
void Tilemap::render(RenderTarget& target) const
{
for (auto& layer : m_layers)
layer.cluster.render(target);
}
void Tilemap::render_until_layer(unsigned index, RenderTarget& target) const
{
for (unsigned i = 0; i <= index; i++)
m_layers.at(i).cluster.render(target);
}
void Tilemap::render_after_layer(unsigned index, RenderTarget& target) const
{
for (unsigned i = index + 1; i < m_layers.size(); i++)
m_layers.at(i).cluster.render(target);
}
bool Tilemap::collision(Pos tile) const
{
return collisions.count(tile) ||
find_tile("blocks", {tile.x * tilewidth, tile.y * tileheight});
}
Surface* Tilemap::find_tile(unsigned layer_index, Pos offset)
{
auto& layer = m_layers.at(layer_index);
auto& elems = layer.cluster.vec();
auto itr = std::find_if(std::begin(elems), std::end(elems), [offset](const SurfaceCluster::Elem& elem) {
return (elem.surf.rect().pos + elem.offset) == offset;
});
if (itr == std::end(elems))
return nullptr;
return &itr->surf;
}
Surface* Tilemap::find_tile(const std::string& name, Pos pos)
{
auto layer = std::find_if(std::begin(m_layers), std::end(m_layers), [&name](const Layer& layer) {
return Utils::tolower(layer.name) == name;
});
if (layer == std::end(m_layers))
return nullptr;
return find_tile(std::distance(std::begin(m_layers), layer), pos);
}
const Surface* Tilemap::find_tile(unsigned layer_index, Pos offset) const
{
auto& layer = m_layers.at(layer_index);
auto& elems = layer.cluster.vec();
auto itr = std::find_if(std::begin(elems), std::end(elems), [offset](const SurfaceCluster::Elem& elem) {
return (elem.surf.rect().pos + elem.offset) == offset;
});
if (itr == std::end(elems))
return nullptr;
return &itr->surf;
}
const Surface* Tilemap::find_tile(const std::string& name, Pos pos) const
{
auto layer = std::find_if(std::begin(m_layers), std::end(m_layers), [&name](const Layer& layer) {
return Utils::tolower(layer.name) == name;
});
if (layer == std::end(m_layers))
return nullptr;
return find_tile(std::distance(std::begin(m_layers), layer), pos);
}
const Tilemap::Layer* Tilemap::find_layer(const std::string& name) const
{
auto layer = std::find_if(std::begin(m_layers), std::end(m_layers), [&name](const Layer& layer) {
return Utils::tolower(layer.name) == name;
});
if (layer != std::end(m_layers))
return &*layer;
else
return nullptr;
}
int Tilemap::find_layer_index(const std::string& name) const
{
auto layer = std::find_if(std::begin(m_layers), std::end(m_layers), [&name](const Layer& layer) {
return Utils::tolower(layer.name) == name;
});
if (layer != std::end(m_layers))
return layer - std::begin(m_layers);
else
return -1;
}
Tilemap::Layer* Tilemap::find_layer(const std::string& name)
{
auto layer = std::find_if(std::begin(m_layers), std::end(m_layers), [&name](const Layer& layer) {
return Utils::tolower(layer.name) == name;
});
if (layer != std::end(m_layers))
return &*layer;
else
return nullptr;
}
}