tmxlite
lightweight Tiled tmx map parser for C++
Tileset.hpp
1 /*********************************************************************
2 Matt Marchant 2016
3 http://trederia.blogspot.com
4 
5 tmxlite - Zlib license.
6 
7 This software is provided 'as-is', without any express or
8 implied warranty. In no event will the authors be held
9 liable for any damages arising from the use of this software.
10 
11 Permission is granted to anyone to use this software for any purpose,
12 including commercial applications, and to alter it and redistribute
13 it freely, subject to the following restrictions:
14 
15 1. The origin of this software must not be misrepresented;
16 you must not claim that you wrote the original software.
17 If you use this software in a product, an acknowledgment
18 in the product documentation would be appreciated but
19 is not required.
20 
21 2. Altered source versions must be plainly marked as such,
22 and must not be misrepresented as being the original software.
23 
24 3. This notice may not be removed or altered from any
25 source distribution.
26 *********************************************************************/
27 
28 #ifndef TMXLITE_TILESET_HPP_
29 #define TMXLITE_TILESET_HPP_
30 
31 #include <tmxlite/Config.hpp>
32 #include <tmxlite/Property.hpp>
33 #include <tmxlite/ObjectGroup.hpp>
34 
35 #include <string>
36 #include <vector>
37 #include <array>
38 
39 namespace pugi
40 {
41  class xml_node;
42 }
43 
44 namespace tmx
45 {
51  class TMXLITE_EXPORT_API Tileset final
52  {
53  public:
54  explicit Tileset(const std::string& workingDir);
55  ~Tileset() = default;
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  std::vector<Frame> frames;
82  }animation;
83  std::vector<Property> properties;
84  ObjectGroup objectGroup;
85  std::string imagePath;
86  Vector2u imageSize;
91  std::string type;
92  };
93 
98  struct Terrain final
99  {
100  std::string name;
101  std::uint32_t tileID = -1;
102  std::vector<Property> properties;
103  };
104 
110  void parse(pugi::xml_node);
116  std::uint32_t getFirstGID() const { return m_firstGID; }
121  std::uint32_t getLastGID() const { return m_firstGID + getTileCount() - 1; }
125  const std::string& getName() const { return m_name; }
130  const Vector2u& getTileSize() const { return m_tileSize; }
134  std::uint32_t getSpacing() const { return m_spacing; }
138  std::uint32_t getMargin() const { return m_margin; }
142  std::uint32_t getTileCount() const { return m_tileCount; }
147  std::uint32_t getColumnCount() const { return m_columnCount; }
152  const Vector2u& getTileOffset() const { return m_tileOffset; }
157  const std::vector<Property>& getProperties() const { return m_properties; }
163  const std::string getImagePath() const { return m_imagePath; }
168  const Colour& getTransparencyColour() const { return m_transparencyColour; }
173  bool hasTransparency() const { return m_hasTransparency; }
178  const std::vector<Terrain>& getTerrainTypes() const { return m_terrainTypes; }
183  const std::vector<Tile>& getTiles() const { return m_tiles; }
184 
190  bool hasTile(std::uint32_t id) const { return id >= m_firstGID && id <= getLastGID(); };
191 
192 
198  const Tile* getTile(std::uint32_t id) const;
199 
200  private:
201 
202  std::string m_workingDir;
203 
204  std::uint32_t m_firstGID;
205  std::string m_source;
206  std::string m_name;
207  Vector2u m_tileSize;
208  std::uint32_t m_spacing;
209  std::uint32_t m_margin;
210  std::uint32_t m_tileCount;
211  std::uint32_t m_columnCount;
212  Vector2u m_tileOffset;
213 
214  std::vector<Property> m_properties;
215  std::string m_imagePath;
216  Colour m_transparencyColour;
217  bool m_hasTransparency;
218 
219  std::vector<Terrain> m_terrainTypes;
220  std::vector<Tile> m_tiles;
221 
222  void reset();
223 
224  void parseOffsetNode(const pugi::xml_node&);
225  void parsePropertyNode(const pugi::xml_node&);
226  void parseTerrainNode(const pugi::xml_node&);
227  void parseTileNode(const pugi::xml_node&);
228  void createMissingTile(std::uint32_t ID);
229  };
230 }
231 
232 #endif //TMXLITE_TILESET_HPP_
Definition: Log.hpp:58
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:116
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:168
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:163
const std::vector< Property > & getProperties() const
Returns a reference to the list of Property objects for this tile set.
Definition: Tileset.hpp:157
Terrain information with which one or more tiles may be associated.
Definition: Tileset.hpp:98
Contains the red, green, blue and alpha values of a colour in the range 0 - 255.
Definition: Types.hpp:109
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:183
Any tiles within a tile set which have special data associated with them such as animation or terrain...
Definition: Tileset.hpp:62
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:152
Represents a Tileset node as loaded from a *.tmx format tile map via the tmx::Map class...
Definition: Tileset.hpp:51
std::uint32_t getSpacing() const
Returns the spacing, in pixels, between each tile in the set.
Definition: Tileset.hpp:134
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...
Definition: Tileset.hpp:121
Vector2u imagePosition
The position of the tile within the image.
Definition: Tileset.hpp:90
a group of frames which make up an animation
Definition: Tileset.hpp:71
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:147
const Vector2u & getTileSize() const
Returns the width and height of a tile in the tile set, in pixels.
Definition: Tileset.hpp:130
const std::string & getName() const
Returns the name of this tile set.
Definition: Tileset.hpp:125
std::uint32_t getTileCount() const
Returns the number of tiles in the tile set.
Definition: Tileset.hpp:142
std::uint32_t getMargin() const
Returns the margin, in pixels, around each tile in the set.
Definition: Tileset.hpp:138
ObjectGroup layers contain a series of Objects which may be made up of shapes or images.
Definition: ObjectGroup.hpp:43
Definition: Layer.hpp:39
bool hasTransparency() const
Returns true if the image used by this tileset specifically requests a colour to use as transparency...
Definition: Tileset.hpp:173
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:178
A frame within an animation.
Definition: Tileset.hpp:76
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:190