tmxlite 1.0.0
lightweight parse for Tiled maps
Map.hpp
1/*********************************************************************
2Matt Marchant 2016 -2021
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/Tileset.hpp>
31#include <tmxlite/Layer.hpp>
32#include <tmxlite/Property.hpp>
33#include <tmxlite/Types.hpp>
34#include <tmxlite/Object.hpp>
35
36#include <string>
37#include <vector>
38#include <map>
39#include <unordered_map>
40
41namespace tmx
42{
46 struct TMXLITE_EXPORT_API Version
47 {
48 //major/minor are apparently reserved by gcc
49 std::uint16_t upper;
50 std::uint16_t lower;
51 Version(std::uint16_t maj = 0, std::uint16_t min = 0)
52 : upper(maj), lower(min) {}
53 };
54
55 enum class Orientation
56 {
57 Orthogonal,
58 Isometric,
59 Staggered,
60 Hexagonal,
61 None
62 };
63
64 enum class RenderOrder
65 {
66 RightDown,
67 RightUp,
68 LeftDown,
69 LeftUp,
70 None
71 };
72
73 enum class StaggerAxis
74 {
75 X, Y, None
76 };
77
78 enum class StaggerIndex
79 {
80 Even, Odd, None
81 };
82
93 class TMXLITE_EXPORT_API Map final
94 {
95 public:
96
97 Map();
98 ~Map() = default;
99 Map(const Map&) = delete;
100 Map& operator = (const Map&) = delete;
101 Map(Map&&) = default;
102 Map& operator = (Map&&) = default;
103
110 bool load(const std::string&);
111
119 bool loadFromString(const std::string& data, const std::string& workingDir);
120
125 const Version& getVersion() const { return m_version; }
126
131 Orientation getOrientation() const { return m_orientation; }
132
137 RenderOrder getRenderOrder() const { return m_renderOrder; }
138
142 const Vector2u& getTileCount() const { return m_tileCount; }
143
149 const Vector2u& getTileSize() const { return m_tileSize; }
150
154 FloatRect getBounds() const { return FloatRect(0.f, 0.f, static_cast<float>(m_tileCount.x * m_tileSize.x), static_cast<float>(m_tileCount.y * m_tileSize.y)); }
155
164 float getHexSideLength() const { return m_hexSideLength; }
165
171 StaggerAxis getStaggerAxis() const { return m_staggerAxis; }
172
178 StaggerIndex getStaggerIndex() const { return m_staggerIndex; }
179
183 const Colour& getBackgroundColour() const { return m_backgroundColour; }
184
188 const std::vector<Tileset>& getTilesets() const { return m_tilesets; }
189
196 const std::vector<Layer::Ptr>& getLayers() const { return m_layers; }
197
201 const std::string& getClass() const { return m_class; }
202
206 const std::vector<Property>& getProperties() const { return m_properties; }
207
211 const std::map<std::uint32_t, Tileset::Tile>& getAnimatedTiles() const { return m_animTiles; }
212
217 const std::string& getWorkingDirectory() const { return m_workingDirectory; }
218
222 std::unordered_map<std::string, Object>& getTemplateObjects() { return m_templateObjects; }
223 const std::unordered_map<std::string, Object>& getTemplateObjects() const { return m_templateObjects; }
224
231 std::unordered_map<std::string, Tileset>& getTemplateTilesets() { return m_templateTilesets; }
232 const std::unordered_map<std::string, Tileset>& getTemplateTilesets() const { return m_templateTilesets; }
233
241 bool isInfinite() const { return m_infinite; }
242
243 /*
244 \brief Returns the origin of each layer's parallax offset value
245 */
246 Vector2f getParallaxOrigin() const { return m_parallaxOrigin; }
247
248 private:
249 Version m_version;
250 std::string m_class;
251 Orientation m_orientation;
252 RenderOrder m_renderOrder;
253 bool m_infinite;
254
255 Vector2u m_tileCount;
256 Vector2u m_tileSize;
257
258 float m_hexSideLength;
259 StaggerAxis m_staggerAxis;
260 StaggerIndex m_staggerIndex;
261
262 Vector2f m_parallaxOrigin;
263
264 Colour m_backgroundColour;
265
266 std::string m_workingDirectory;
267
268 std::vector<Tileset> m_tilesets;
269 std::vector<Layer::Ptr> m_layers;
270 std::vector<Property> m_properties;
271 std::map<std::uint32_t, Tileset::Tile> m_animTiles;
272
273 std::unordered_map<std::string, Object> m_templateObjects;
274 std::unordered_map<std::string, Tileset> m_templateTilesets;
275
276 bool parseMapNode(const pugi::xml_node&);
277
278 //always returns false so we can return this
279 //on load failure
280 bool reset();
281 };
282}
Parser for TMX format tile maps. This class can be used to parse the XML format tile maps created wit...
Definition Map.hpp:94
bool load(const std::string &)
Attempts to parse the tilemap at the given location.
bool loadFromString(const std::string &data, const std::string &workingDir)
Loads a map from a document stored in a string.
FloatRect getBounds() const
Returns the bounds of the map.
Definition Map.hpp:154
const Vector2u & getTileSize() const
Returns the size of the tile grid in this map. Actual tile sizes may vary and will be extended / shru...
Definition Map.hpp:149
StaggerIndex getStaggerIndex() const
Stagger Index of the loaded map. If a Staggered or Hexagonal map is loaded this returns whether the e...
Definition Map.hpp:178
StaggerAxis getStaggerAxis() const
Stagger axis of the map. If either a Staggered or Hexagonal tile map is loaded this returns which axi...
Definition Map.hpp:171
const std::vector< Tileset > & getTilesets() const
Returns a reference to the vector of tile sets used by the map.
Definition Map.hpp:188
const std::string & getClass() const
Returns the class of the Map, as defined in the editor Tiled 1.9+.
Definition Map.hpp:201
std::unordered_map< std::string, Tileset > & getTemplateTilesets()
Returns an unordered_map of tilesets used by templated objects. If Object::getTilesetName() is not em...
Definition Map.hpp:231
Orientation getOrientation() const
Returns the orientation of the map if one is loaded, else returns None.
Definition Map.hpp:131
float getHexSideLength() const
Returns the length of an edge of a tile if a Hexagonal map is loaded. The length returned is in pixel...
Definition Map.hpp:164
const std::vector< Layer::Ptr > & getLayers() const
Returns a reference to the vector containing the layer data. Layers are pointer-to-baseclass,...
Definition Map.hpp:196
const std::map< std::uint32_t, Tileset::Tile > & getAnimatedTiles() const
Returns a Hashmap of all animated tiles accessible by TileID.
Definition Map.hpp:211
const Colour & getBackgroundColour() const
Returns the background colour of the map.
Definition Map.hpp:183
std::unordered_map< std::string, Object > & getTemplateObjects()
Returns an unordered_map of template objects indexed by file name.
Definition Map.hpp:222
const std::vector< Property > & getProperties() const
Returns a vector of Property objects loaded by the map.
Definition Map.hpp:206
const Version & getVersion() const
Returns the version of the tile map last parsed. If no tile map has yet been parsed the version will ...
Definition Map.hpp:125
const std::string & getWorkingDirectory() const
Returns the current working directory of the map. Images and other resources are loaded relative to t...
Definition Map.hpp:217
RenderOrder getRenderOrder() const
Returns the RenderOrder of the map if one is loaded, else returns None.
Definition Map.hpp:137
const Vector2u & getTileCount() const
Returns the tile count of the map in the X and Y directions.
Definition Map.hpp:142
bool isInfinite() const
Returns true if this is in infinite tile map. Infinite maps store their tile data in for tile layers ...
Definition Map.hpp:241
Contains the red, green, blue and alpha values of a colour in the range 0 - 255.
Definition Types.hpp:111
Holds the xml version of the loaded map.
Definition Map.hpp:47