Skip to content

Commit 01b0e59

Browse files
committed
add TMX class support in TiledMap
1 parent fb1f8ec commit 01b0e59

File tree

2 files changed

+20
-14
lines changed

2 files changed

+20
-14
lines changed

include/gf2/core/TiledMap.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,14 @@ namespace gf {
3434
struct GF_CORE_API MapLayer {
3535
uint32_t properties_index = NoIndex;
3636
std::string name;
37+
std::string type;
3738
Vec2I offset = { 0, 0 };
3839
};
3940

4041
template<typename Archive>
4142
Archive& operator|(Archive& ar, MaybeConst<MapLayer, Archive>& data)
4243
{
43-
return ar | data.properties_index | data.name | data.offset;
44+
return ar | data.properties_index | data.name | data.type | data.offset;
4445
}
4546

4647
struct GF_CORE_API MapTile {
@@ -137,6 +138,7 @@ namespace gf {
137138
}
138139

139140
struct GF_CORE_API MapTileset {
141+
std::string type;
140142
uint32_t properties_index = NoIndex;
141143
uint32_t texture_index = NoIndex;
142144
uint32_t first_gid = 0;
@@ -155,10 +157,11 @@ namespace gf {
155157
template<typename Archive>
156158
Archive& operator|(Archive& ar, MaybeConst<MapTileset, Archive>& data)
157159
{
158-
return ar | data.properties_index | data.texture_index | data.first_gid | data.tile_size | data.spacing | data.margin | data.tiles;
160+
return ar | data.type | data.properties_index | data.texture_index | data.first_gid | data.tile_size | data.spacing | data.margin | data.tiles;
159161
}
160162

161163
struct GF_CORE_API TiledMap {
164+
std::string type;
162165
uint32_t properties_index = NoIndex;
163166
GridOrientation orientation = GridOrientation::Unknown;
164167
CellAxis cell_axis = CellAxis::X;
@@ -189,7 +192,7 @@ namespace gf {
189192
template<typename Archive>
190193
Archive& operator|(Archive& ar, MaybeConst<TiledMap, Archive>& map)
191194
{
192-
return ar | map.properties_index | map.orientation | map.cell_axis | map.cell_index | map.hex_side_length | map.map_size | map.tile_size | map.properties | map.tilesets | map.tile_layers | map.object_layers | map.group_layers | map.layers | map.textures;
195+
return ar | map.type | map.properties_index | map.orientation | map.cell_axis | map.cell_index | map.hex_side_length | map.map_size | map.tile_size | map.properties | map.tilesets | map.tile_layers | map.object_layers | map.group_layers | map.layers | map.textures;
193196
}
194197

195198
}

library/core/TiledMap.cc

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,7 @@ namespace gf {
201201
MapLayer layer;
202202
layer.properties_index = parse_tmx_properties(node, map);
203203
layer.name = node.attribute("name").as_string();
204+
layer.type = node.attribute("class").as_string();
204205
layer.offset.x = node.attribute("offsetx").as_int(0);
205206
layer.offset.y = node.attribute("offsety").as_int(0);
206207
unsupported_attribute(node, "opacity");
@@ -682,10 +683,15 @@ namespace gf {
682683
return tile;
683684
}
684685

685-
void parse_tmx_tileset_from_element(MapTileset& tileset, const pugi::xml_node node, TiledMap& map, const std::filesystem::path& base_directory)
686+
MapTileset parse_tmx_tileset_from_element(const pugi::xml_node node, TiledMap& map, const std::filesystem::path& base_directory)
686687
{
687688
assert(node.name() == "tileset"sv);
688689

690+
MapTileset tileset;
691+
tileset.type = node.attribute("class").as_string();
692+
tileset.properties_index = parse_tmx_properties(node, map);
693+
tileset.first_gid = node.attribute("firstgid").as_uint();
694+
689695
tileset.tile_size.w = node.attribute("tilewidth").as_int();
690696
tileset.tile_size.h = node.attribute("tileheight").as_int();
691697

@@ -710,9 +716,11 @@ namespace gf {
710716
std::sort(tileset.tiles.begin(), tileset.tiles.end(), MapTilesetTileComparator());
711717

712718
unsupported_node(node, "wangsets");
719+
720+
return tileset;
713721
}
714722

715-
void parse_tmx_tileset_from_file(MapTileset& tileset, const std::filesystem::path& source, TiledMap& map, const std::filesystem::path& base_directory)
723+
MapTileset parse_tmx_tileset_from_file(const std::filesystem::path& source, TiledMap& map, const std::filesystem::path& base_directory)
716724
{
717725
std::filesystem::path tileset_path = base_directory / source;
718726
std::ifstream tileset_file(tileset_path);
@@ -738,25 +746,19 @@ namespace gf {
738746
Log::warning("Attribute 'source' present in a TSX file: '{}'.", tileset_path);
739747
}
740748

741-
parse_tmx_tileset_from_element(tileset, node, map, tileset_path.parent_path());
749+
return parse_tmx_tileset_from_element(node, map, tileset_path.parent_path());
742750
}
743751

744752
MapTileset parse_tmx_tileset(const pugi::xml_node node, TiledMap& map, const std::filesystem::path& base_directory)
745753
{
746754
assert(node.name() == "tileset"sv);
747-
MapTileset tileset;
748-
tileset.properties_index = parse_tmx_properties(node, map);
749-
tileset.first_gid = node.attribute("firstgid").as_uint();
750-
751755
const std::filesystem::path source = node.attribute("source").as_string();
752756

753757
if (!source.empty()) {
754-
parse_tmx_tileset_from_file(tileset, source, map, base_directory);
755-
} else {
756-
parse_tmx_tileset_from_element(tileset, node, map, base_directory);
758+
return parse_tmx_tileset_from_file(source, map, base_directory);
757759
}
758760

759-
return tileset;
761+
return parse_tmx_tileset_from_element(node, map, base_directory);
760762
}
761763

762764
/*
@@ -767,6 +769,7 @@ namespace gf {
767769
{
768770
assert(node.name() == "map"sv);
769771

772+
map.type = node.attribute("class").as_string();
770773
map.properties_index = parse_tmx_properties(node, map);
771774

772775
std::string orientation = node.attribute("orientation").as_string();

0 commit comments

Comments
 (0)