Skip to content

Commit 51375e9

Browse files
committed
Allow importing tiles from other tilesets
1 parent e040846 commit 51375e9

File tree

2 files changed

+81
-61
lines changed

2 files changed

+81
-61
lines changed

src/supertux/tile_set_parser.cpp

Lines changed: 78 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ TileSetParser::TileSetParser(TileSet& tileset, const std::string& filename) :
3939
}
4040

4141
void
42-
TileSetParser::parse()
42+
TileSetParser::parse(uint32_t start, uint32_t end, int32_t offset)
4343
{
4444
m_tiles_path = FileSystem::dirname(m_filename);
4545

@@ -56,11 +56,13 @@ TileSetParser::parse()
5656
if (iter.get_key() == "tile")
5757
{
5858
ReaderMapping tile_mapping = iter.as_mapping();
59-
parse_tile(tile_mapping);
59+
parse_tile(tile_mapping, start, end, offset);
6060
}
6161
else if (iter.get_key() == "tilegroup")
6262
{
6363
/* tilegroups are only interesting for the editor */
64+
/* ignore tilegroups for imported tilesets */
65+
if (end) continue;
6466
ReaderMapping reader = iter.as_mapping();
6567
Tilegroup tilegroup;
6668
reader.get("name", tilegroup.name);
@@ -70,10 +72,12 @@ TileSetParser::parse()
7072
else if (iter.get_key() == "tiles")
7173
{
7274
ReaderMapping tiles_mapping = iter.as_mapping();
73-
parse_tiles(tiles_mapping);
75+
parse_tiles(tiles_mapping, start, end, offset);
7476
}
7577
else if (iter.get_key() == "autotileset")
7678
{
79+
/* ignore autotiles for imported tilesets */
80+
if (end) continue;
7781
ReaderMapping reader = iter.as_mapping();
7882
std::string autotile_filename;
7983
if (!reader.get("source", autotile_filename))
@@ -87,25 +91,41 @@ TileSetParser::parse()
8791
parser->parse();
8892
}
8993
}
94+
else if (iter.get_key() == "import-tileset")
95+
{
96+
ReaderMapping reader = iter.as_mapping();
97+
std::string import_filename;
98+
uint32_t start = 0, end = 0;
99+
int32_t offset = 0;
100+
reader.get("file", import_filename);
101+
reader.get("start", start);
102+
reader.get("end", end);
103+
reader.get("offset", offset);
104+
TileSetParser import_parser(m_tileset, import_filename);
105+
import_parser.parse(start, end, offset);
106+
}
90107
else
91108
{
92109
log_warning << "Unknown symbol '" << iter.get_key() << "' in tileset file" << std::endl;
93110
}
94111
}
95-
if (g_config->developer_mode)
112+
/* only create the unassigned tilegroup from the parent strf */
113+
if (g_config->developer_mode && !end)
96114
{
97115
m_tileset.add_unassigned_tilegroup();
98116
}
99117
}
100118

101119
void
102-
TileSetParser::parse_tile(const ReaderMapping& reader)
120+
TileSetParser::parse_tile(const ReaderMapping& reader, uint32_t min, uint32_t max, int32_t offset)
103121
{
104122
uint32_t id;
105123
if (!reader.get("id", id))
106124
{
107125
throw std::runtime_error("Missing tile-id.");
108126
}
127+
if (max && (id < min || id > max)) return;
128+
id += offset;
109129

110130
uint32_t attributes = 0;
111131

@@ -182,7 +202,7 @@ TileSetParser::parse_tile(const ReaderMapping& reader)
182202
}
183203

184204
void
185-
TileSetParser::parse_tiles(const ReaderMapping& reader)
205+
TileSetParser::parse_tiles(const ReaderMapping& reader, uint32_t min, uint32_t max, int32_t offset)
186206
{
187207
// List of ids (use 0 if the tile should be ignored)
188208
std::vector<uint32_t> ids;
@@ -271,65 +291,65 @@ TileSetParser::parse_tiles(const ReaderMapping& reader)
271291

272292
for (size_t i = 0; i < ids.size(); ++i)
273293
{
274-
if (ids[i] != 0)
275-
{
276-
const int x = static_cast<int>(32 * (i % width));
277-
const int y = static_cast<int>(32 * (i / width));
278-
279-
std::vector<SurfacePtr> regions;
280-
regions.reserve(surfaces.size());
281-
std::transform(surfaces.begin(), surfaces.end(), std::back_inserter(regions),
282-
[x, y] (const SurfacePtr& surface) {
283-
return surface->region(Rect(x, y, Size(32, 32)));
284-
});
285-
286-
std::vector<SurfacePtr> editor_regions;
287-
editor_regions.reserve(editor_surfaces.size());
288-
std::transform(editor_surfaces.begin(), editor_surfaces.end(), std::back_inserter(editor_regions),
289-
[x, y] (const SurfacePtr& surface) {
290-
return surface->region(Rect(x, y, Size(32, 32)));
291-
});
292-
293-
auto tile = std::make_unique<Tile>(regions,
294-
editor_regions,
295-
(has_attributes ? attributes[i] : 0),
296-
(has_datas ? datas[i] : 0),
297-
fps);
298-
299-
m_tileset.add_tile(ids[i], std::move(tile));
300-
}
294+
if(!ids[i] || (max && (ids[i] < min || ids[i] > max))) continue;
295+
ids[i] += offset;
296+
297+
const int x = static_cast<int>(32 * (i % width));
298+
const int y = static_cast<int>(32 * (i / width));
299+
300+
std::vector<SurfacePtr> regions;
301+
regions.reserve(surfaces.size());
302+
std::transform(surfaces.begin(), surfaces.end(), std::back_inserter(regions),
303+
[x, y] (const SurfacePtr& surface) {
304+
return surface->region(Rect(x, y, Size(32, 32)));
305+
});
306+
307+
std::vector<SurfacePtr> editor_regions;
308+
editor_regions.reserve(editor_surfaces.size());
309+
std::transform(editor_surfaces.begin(), editor_surfaces.end(), std::back_inserter(editor_regions),
310+
[x, y] (const SurfacePtr& surface) {
311+
return surface->region(Rect(x, y, Size(32, 32)));
312+
});
313+
314+
auto tile = std::make_unique<Tile>(regions,
315+
editor_regions,
316+
(has_attributes ? attributes[i] : 0),
317+
(has_datas ? datas[i] : 0),
318+
fps);
319+
320+
m_tileset.add_tile(ids[i], std::move(tile));
301321
}
302322
}
303323
else // (!shared_surface)
304324
{
305325
for (size_t i = 0; i < ids.size(); ++i)
306326
{
307-
if (ids[i] != 0)
308-
{
309-
int x = static_cast<int>(32 * (i % width));
310-
int y = static_cast<int>(32 * (i / width));
311-
312-
std::vector<SurfacePtr> surfaces;
313-
boost::optional<ReaderMapping> surfaces_mapping;
314-
if (reader.get("image", surfaces_mapping) ||
315-
reader.get("images", surfaces_mapping)) {
316-
surfaces = parse_imagespecs(*surfaces_mapping, Rect(x, y, Size(32, 32)));
317-
}
318-
319-
std::vector<SurfacePtr> editor_surfaces;
320-
boost::optional<ReaderMapping> editor_surfaces_mapping;
321-
if (reader.get("editor-images", editor_surfaces_mapping)) {
322-
editor_surfaces = parse_imagespecs(*editor_surfaces_mapping, Rect(x, y, Size(32, 32)));
323-
}
324-
325-
auto tile = std::make_unique<Tile>(surfaces,
326-
editor_surfaces,
327-
(has_attributes ? attributes[i] : 0),
328-
(has_datas ? datas[i] : 0),
329-
fps);
330-
331-
m_tileset.add_tile(ids[i], std::move(tile));
327+
if(!ids[i] || (max && (ids[i] < min || ids[i] > max))) continue;
328+
ids[i] += offset;
329+
330+
int x = static_cast<int>(32 * (i % width));
331+
int y = static_cast<int>(32 * (i / width));
332+
333+
std::vector<SurfacePtr> surfaces;
334+
boost::optional<ReaderMapping> surfaces_mapping;
335+
if (reader.get("image", surfaces_mapping) ||
336+
reader.get("images", surfaces_mapping)) {
337+
surfaces = parse_imagespecs(*surfaces_mapping, Rect(x, y, Size(32, 32)));
338+
}
339+
340+
std::vector<SurfacePtr> editor_surfaces;
341+
boost::optional<ReaderMapping> editor_surfaces_mapping;
342+
if (reader.get("editor-images", editor_surfaces_mapping)) {
343+
editor_surfaces = parse_imagespecs(*editor_surfaces_mapping, Rect(x, y, Size(32, 32)));
332344
}
345+
346+
auto tile = std::make_unique<Tile>(surfaces,
347+
editor_surfaces,
348+
(has_attributes ? attributes[i] : 0),
349+
(has_datas ? datas[i] : 0),
350+
fps);
351+
352+
m_tileset.add_tile(ids[i], std::move(tile));
333353
}
334354
}
335355
}

src/supertux/tile_set_parser.hpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -38,11 +38,11 @@ class TileSetParser final
3838
public:
3939
TileSetParser(TileSet& tileset, const std::string& filename);
4040

41-
void parse();
41+
void parse(uint32_t start = 0, uint32_t end = 0, int32_t offset = 0);
4242

4343
private:
44-
void parse_tile(const ReaderMapping& reader);
45-
void parse_tiles(const ReaderMapping& reader);
44+
void parse_tile(const ReaderMapping& reader, uint32_t min, uint32_t max, int32_t offset);
45+
void parse_tiles(const ReaderMapping& reader, uint32_t min, uint32_t max, int32_t offset);
4646
std::vector<SurfacePtr> parse_imagespecs(const ReaderMapping& cur,
4747
const boost::optional<Rect>& region = boost::none) const;
4848

0 commit comments

Comments
 (0)