tmxlite 1.0.0
lightweight parse for Tiled maps
Tileset.hpp
1/*********************************************************************
2Matt Marchant 2016 - 2023
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/Property.hpp>
32#include <tmxlite/ObjectGroup.hpp>
33
34#include <string>
35#include <vector>
36#include <array>
37
38namespace pugi
39{
40 class xml_node;
41}
42
43namespace tmx
44{
45 class Map;
46
52 class TMXLITE_EXPORT_API Tileset final
53 {
54 public:
55 explicit Tileset(const std::string& workingDir);
56
62 struct Tile final
63 {
64 std::uint32_t ID = 0;
65 std::array<std::int32_t, 4u> terrainIndices{};
66 std::uint32_t probability = 100;
67
71 struct Animation final
72 {
76 struct Frame final
77 {
78 std::uint32_t tileID = 0;
79 std::uint32_t duration = 0;
80
81 bool operator == (const Frame& other) const
82 {
83 return (this == &other) ||
84 (tileID == other.tileID && duration == other.duration);
85 }
86
87 bool operator != (const Frame& other) const
88 {
89 return !(*this == other);
90 }
91 };
92 std::vector<Frame> frames;
93 }animation;
94 std::vector<Property> properties;
95 ObjectGroup objectGroup;
96 std::string imagePath;
97 Vector2u imageSize;
102 std::string className;
103 };
104
109 struct Terrain final
110 {
111 std::string name;
112 std::uint32_t tileID = -1;
113 std::vector<Property> properties;
114 };
115
120 {
121 Unspecified,
122 TopLeft,
123 Top,
124 TopRight,
125 Left,
126 Center,
127 Right,
128 BottomLeft,
129 Bottom,
130 BottomRight
131 };
132
138 void parse(pugi::xml_node, Map*);
139
145 std::uint32_t getFirstGID() const { return m_firstGID; }
146
151 std::uint32_t getLastGID() const;
152
156 const std::string& getName() const { return m_name; }
157
161 const std::string& getClass() const { return m_class; }
162
167 const Vector2u& getTileSize() const { return m_tileSize; }
168
172 std::uint32_t getSpacing() const { return m_spacing; }
173
177 std::uint32_t getMargin() const { return m_margin; }
178
182 std::uint32_t getTileCount() const { return m_tileCount; }
183
188 std::uint32_t getColumnCount() const { return m_columnCount; }
189
197 ObjectAlignment getObjectAlignment() const { return m_objectAlignment; }
198
203 const Vector2u& getTileOffset() const { return m_tileOffset; }
204
209 const std::vector<Property>& getProperties() const { return m_properties; }
210
216 const std::string& getImagePath() const { return m_imagePath; }
217
221 const Vector2u& getImageSize() const { return m_imageSize; }
222
227 const Colour& getTransparencyColour() const { return m_transparencyColour; }
228
233 bool hasTransparency() const { return m_hasTransparency; }
234
239 const std::vector<Terrain>& getTerrainTypes() const { return m_terrainTypes; }
240
245 const std::vector<Tile>& getTiles() const { return m_tiles; }
246
252 bool hasTile(std::uint32_t id) const { return id >= m_firstGID && id <= getLastGID(); };
253
259 const Tile* getTile(std::uint32_t id) const;
260
261 private:
262
263 std::string m_workingDir;
264
265 std::uint32_t m_firstGID;
266 std::string m_source;
267 std::string m_name;
268 std::string m_class;
269 Vector2u m_tileSize;
270 std::uint32_t m_spacing;
271 std::uint32_t m_margin;
272 std::uint32_t m_tileCount;
273 std::uint32_t m_columnCount;
274 ObjectAlignment m_objectAlignment;
275 Vector2u m_tileOffset;
276
277 std::vector<Property> m_properties;
278 std::string m_imagePath;
279 Vector2u m_imageSize;
280 Colour m_transparencyColour;
281 bool m_hasTransparency;
282
283 std::vector<Terrain> m_terrainTypes;
284 std::vector<std::uint32_t> m_tileIndex;
285 std::vector<Tile> m_tiles;
286
287 void reset();
288
289 void parseOffsetNode(const pugi::xml_node&);
290 void parsePropertyNode(const pugi::xml_node&);
291 void parseTerrainNode(const pugi::xml_node&);
292 Tile& newTile(std::uint32_t ID);
293 void parseTileNode(const pugi::xml_node&, Map*);
294 void createMissingTile(std::uint32_t ID);
295 };
296}
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
Represents a Tileset node as loaded from a *.tmx format tile map via the tmx::Map class.
Definition Tileset.hpp:53
void parse(pugi::xml_node, Map *)
Attempts to parse the given xml node. If node parsing fails an error is printed in the console and th...
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:145
std::uint32_t getMargin() const
Returns the margin, in pixels, around each tile in the set.
Definition Tileset.hpp:177
ObjectAlignment
Declares the alignment of tile Objects.
Definition Tileset.hpp:120
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:216
const std::vector< Property > & getProperties() const
Returns a reference to the list of Property objects for this tile set.
Definition Tileset.hpp:209
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:252
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:161
ObjectAlignment getObjectAlignment() const
Returns the alignment of tile objects. The default value is ObjectAlignment::Unspecified for compatib...
Definition Tileset.hpp:197
const Vector2u & getTileSize() const
Returns the width and height of a tile in the tile set, in pixels.
Definition Tileset.hpp:167
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:239
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:227
bool hasTransparency() const
Returns true if the image used by this tileset specifically requests a colour to use as transparency.
Definition Tileset.hpp:233
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:203
std::uint32_t getSpacing() const
Returns the spacing, in pixels, between each tile in the set.
Definition Tileset.hpp:172
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:245
std::uint32_t getTileCount() const
Returns the number of tiles in the tile set.
Definition Tileset.hpp:182
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 ...
const std::string & getName() const
Returns the name of this tile set.
Definition Tileset.hpp:156
const Vector2u & getImageSize() const
Returns the size of the tile set image in pixels.
Definition Tileset.hpp:221
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:188
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:110
A frame within an animation.
Definition Tileset.hpp:77
a group of frames which make up an animation
Definition Tileset.hpp:72
Any tiles within a tile set which have special data associated with them such as animation or terrain...
Definition Tileset.hpp:63
Vector2u imagePosition
The position of the tile within the image.
Definition Tileset.hpp:101