Skip to content

Commit bdf21ff

Browse files
committed
fix bug where incorrect tiles were returned from a tileset due to the assumption the first tile id would always be zero (it isn't)
1 parent 8ed4107 commit bdf21ff

2 files changed

Lines changed: 11 additions & 2 deletions

File tree

tmxlite/include/tmxlite/Tileset.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ namespace tmx
118118
\brief Returns the last GID of this tile set.
119119
This is the ID of the last tile in the tile set.
120120
*/
121-
std::uint32_t getLastGID() const { return m_firstGID + getTileCount() - 1; }
121+
std::uint32_t getLastGID() const;
122122
/*!
123123
\brief Returns the name of this tile set.
124124
*/

tmxlite/src/Tileset.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -165,6 +165,15 @@ void Tileset::parse(pugi::xml_node node)
165165
createMissingTile(ID);
166166
}
167167
}
168+
169+
//sort these just to make sure when we request last GID we get the corrtect value
170+
std::sort(m_tiles.begin(), m_tiles.end(), [](const Tile& t1, const Tile& t2) {return t1.ID < t2.ID; });
171+
}
172+
173+
std::uint32_t Tileset::getLastGID() const
174+
{
175+
assert(!m_tiles.empty());
176+
return m_firstGID + m_tiles.back().ID;
168177
}
169178

170179
const Tileset::Tile* Tileset::getTile(std::uint32_t id) const
@@ -174,7 +183,7 @@ const Tileset::Tile* Tileset::getTile(std::uint32_t id) const
174183
return nullptr;
175184
}
176185

177-
//corrects the ID. Indiecies and ID`s are diffrent.
186+
//corrects the ID. Indices and IDs are different.
178187
id = (getLastGID() - m_firstGID) - (getLastGID() - id);
179188

180189
const auto itr = std::find_if(m_tiles.begin(), m_tiles.end(),

0 commit comments

Comments
 (0)