tmxlite 1.0.0
lightweight parse for Tiled maps
Tileset.hpp
1/*********************************************************************
2Matt Marchant (and contributors) 2016 - 2026
3http://trederia.blogspot.com
4
5tmxlite - Zlib license.
6
7This software is provided 'as-is', without any express or
8implied warranty. In no event will the authors be held
9liable for any damages arising from the use of this software.
10
11Permission is granted to anyone to use this software for any purpose,
12including commercial applications, and to alter it and redistribute
13it freely, subject to the following restrictions:
14
151. The origin of this software must not be misrepresented;
16you must not claim that you wrote the original software.
17If you use this software in a product, an acknowledgment
18in the product documentation would be appreciated but
19is not required.
20
212. Altered source versions must be plainly marked as such,
22and must not be misrepresented as being the original software.
23
243. This notice may not be removed or altered from any
25source distribution.
26*********************************************************************/
27
28#pragma once
29
30#include <tmxlite/Config.hpp>
31#include <tmxlite/ObjectGroup.hpp>
32#include <tmxlite/Property.hpp>
33
34#include <array>
35#include <limits>
36#include <string>
37#include <vector>
38
39namespace pugi
40{
41 class xml_node;
42}
43
44namespace tmx
45{
46 class Map;
47
53 class TMXLITE_EXPORT_API Tileset final
54 {
55 public:
56 explicit Tileset(const std::string& workingDir = "");
57
63 struct Tile final
64 {
65 std::uint32_t ID = 0;
66 std::array<std::int32_t, 4u> terrainIndices{};
67 std::uint32_t probability = 100;
68
72 struct Animation final
73 {
77 struct Frame final
78 {
83 std::uint32_t tileID = 0;
84
88 std::uint32_t duration = 0;
89
90 bool operator == (const Frame& other) const
91 {
92 return (this == &other) ||
93 (tileID == other.tileID && duration == other.duration);
94 }
95
96 bool operator != (const Frame& other) const
97 {
98 return !(*this == other);
99 }
100 };
101 std::vector<Frame> frames;
102 }animation;
103 std::vector<Property> properties;
104 ObjectGroup objectGroup;
105 std::string imagePath;
106 Vector2u imageSize;
111 std::string className;
112 };
113
118 struct Terrain final
119 {
120 std::string name;
121 std::uint32_t tileID = std::numeric_limits<uint32_t>::max();
122 std::vector<Property> properties;
123 };
124
129 {
130 Unspecified,
131 TopLeft,
132 Top,
133 TopRight,
134 Left,
135 Center,
136 Right,
137 BottomLeft,
138 Bottom,
139 BottomRight
140 };
141
148 bool loadWithoutMap(const std::string& path);
149
156 bool loadWithoutMapFromString(const std::string& xmlStr);
157
163 bool parse(pugi::xml_node, Map*);
164
170 std::uint32_t getFirstGID() const { return m_firstGID; }
171
176 void setFirstGID(std::uint32_t firstGID) { m_firstGID = firstGID; }
177
182 std::uint32_t getLastGID() const;
183
187 const std::string& getName() const { return m_name; }
188
192 const std::string& getClass() const { return m_class; }
193
198 const Vector2u& getTileSize() const { return m_tileSize; }
199
203 std::uint32_t getSpacing() const { return m_spacing; }
204
208 std::uint32_t getMargin() const { return m_margin; }
209
213 std::uint32_t getTileCount() const { return m_tileCount; }
214
219 std::uint32_t getColumnCount() const { return m_columnCount; }
220
228 ObjectAlignment getObjectAlignment() const { return m_objectAlignment; }
229
234 const Vector2u& getTileOffset() const { return m_tileOffset; }
235
240 const std::vector<Property>& getProperties() const { return m_properties; }
241
247 const std::string& getImagePath() const { return m_imagePath; }
248
252 const Vector2u& getImageSize() const { return m_imageSize; }
253
258 const Colour& getTransparencyColour() const { return m_transparencyColour; }
259
264 bool hasTransparency() const { return m_hasTransparency; }
265
270 const std::vector<Terrain>& getTerrainTypes() const { return m_terrainTypes; }
271
276 const std::vector<Tile>& getTiles() const { return m_tiles; }
277
283 bool hasTile(std::uint32_t id) const { return id >= m_firstGID && id <= getLastGID(); };
284
290 const Tile* getTile(std::uint32_t id) const;
291
292 private:
293
294 std::string m_workingDir;
295
296 std::uint32_t m_firstGID;
297 std::string m_source;
298 std::string m_name;
299 std::string m_class;
300 Vector2u m_tileSize;
301 std::uint32_t m_spacing;
302 std::uint32_t m_margin;
303 std::uint32_t m_tileCount;
304 std::uint32_t m_columnCount;
305 ObjectAlignment m_objectAlignment;
306 Vector2u m_tileOffset;
307
308 std::vector<Property> m_properties;
309 std::string m_imagePath;
310 Vector2u m_imageSize;
311 Colour m_transparencyColour;
312 bool m_hasTransparency;
313
314 std::vector<Terrain> m_terrainTypes;
315 std::vector<std::uint32_t> m_tileIndex;
316 std::vector<Tile> m_tiles;
317
318 //always returns false so we can return this
319 //on load failure
320 bool reset();
321
322 void parseOffsetNode(const pugi::xml_node&);
323 void parsePropertyNode(const pugi::xml_node&);
324 void parseTerrainNode(const pugi::xml_node&);
325 Tile& newTile(std::uint32_t ID);
326 void parseTileNode(const pugi::xml_node&, Map*);
327 void createMissingTile(std::uint32_t ID);
328 };
329}
Parser for TMX format tile maps. This class can be used to parse the XML format tile maps created wit...
Definition Map.hpp:94
ObjectGroup layers contain a series of Objects which may be made up of shapes or images.
Definition ObjectGroup.hpp:43
std::uint32_t getFirstGID() const
Returns the first GID of this tile set. This the ID of the first tile in the tile set,...
Definition Tileset.hpp:170
std::uint32_t getMargin() const
Returns the margin, in pixels, around each tile in the set.
Definition Tileset.hpp:208
void setFirstGID(std::uint32_t firstGID)
Sets the first GID of this tile set. This is set automatically if the tileset is loaded as part of a ...
Definition Tileset.hpp:176
ObjectAlignment
Declares the alignment of tile Objects.
Definition Tileset.hpp:129
const std::string & getImagePath() const
Returns the file path to the tile set image, relative to the working directory. Use this to load the ...
Definition Tileset.hpp:247
const std::vector< Property > & getProperties() const
Returns a reference to the list of Property objects for this tile set.
Definition Tileset.hpp:240
bool hasTile(std::uint32_t id) const
Checks if a tiled ID is in the range of the first ID and the last ID.
Definition Tileset.hpp:283
std::uint32_t getLastGID() const
Returns the last GID of this tile set. This is the ID of the last tile in the tile set.
const std::string & getClass() const
Returns the class of the Tileset, as defined in the editor Tiled 1.9+.
Definition Tileset.hpp:192
ObjectAlignment getObjectAlignment() const
Returns the alignment of tile objects. The default value is ObjectAlignment::Unspecified for compatib...
Definition Tileset.hpp:228
const Vector2u & getTileSize() const
Returns the width and height of a tile in the tile set, in pixels.
Definition Tileset.hpp:198
bool parse(pugi::xml_node, Map *)
Attempts to parse the given xml node as part of a map. If node parsing fails, an error is printed in ...
const std::vector< Terrain > & getTerrainTypes() const
Returns a vector of Terrain types associated with one or more tiles within this tile set.
Definition Tileset.hpp:270
const Colour & getTransparencyColour() const
Returns the colour used by the tile map image to represent transparency. By default this is a transpa...
Definition Tileset.hpp:258
bool hasTransparency() const
Returns true if the image used by this tileset specifically requests a colour to use as transparency.
Definition Tileset.hpp:264
const Vector2u & getTileOffset() const
Returns the tile offset in pixels. Tile will draw tiles offset from the top left using this value.
Definition Tileset.hpp:234
std::uint32_t getSpacing() const
Returns the spacing, in pixels, between each tile in the set.
Definition Tileset.hpp:203
const std::vector< Tile > & getTiles() const
Returns a reference to the vector of tile data used by tiles which make up this tile set.
Definition Tileset.hpp:276
std::uint32_t getTileCount() const
Returns the number of tiles in the tile set.
Definition Tileset.hpp:213
const Tile * getTile(std::uint32_t id) const
queries tiles and returns a tile with the given ID. Checks if the TileID is part of the Tileset with ...
bool loadWithoutMap(const std::string &path)
Loads the tilemap from the given location. This does not set the first GID. This does not support tem...
const std::string & getName() const
Returns the name of this tile set.
Definition Tileset.hpp:187
const Vector2u & getImageSize() const
Returns the size of the tile set image in pixels.
Definition Tileset.hpp:252
std::uint32_t getColumnCount() const
Returns the number of columns which make up the tile set. This is used when rendering collection of i...
Definition Tileset.hpp:219
bool loadWithoutMapFromString(const std::string &xmlStr)
Loads the tilemap from the given XML string. This does not set the first GID. This does not support t...
Contains the red, green, blue and alpha values of a colour in the range 0 - 255.
Definition Types.hpp:111
Terrain information with which one or more tiles may be associated.
Definition Tileset.hpp:119
A frame within an animation.
Definition Tileset.hpp:78
std::uint32_t tileID
ID of the tile to be displayed during this frame.
Definition Tileset.hpp:83
std::uint32_t duration
Duration of the animation frame, in milliseconds.
Definition Tileset.hpp:88
a group of frames which make up an animation
Definition Tileset.hpp:73
Any tiles within a tile set which have special data associated with them such as animation or terrain...
Definition Tileset.hpp:64
Vector2u imagePosition
The position of the tile within the image.
Definition Tileset.hpp:110