tmxparser  2.1.0
 All Classes Functions Variables Pages
TmxMap.h
1 //-----------------------------------------------------------------------------
2 // TmxMap.h
3 //
4 // Copyright (c) 2010-2014, Tamir Atias
5 // All rights reserved.
6 //
7 // Redistribution and use in source and binary forms, with or without
8 // modification, are permitted provided that the following conditions are met:
9 // * Redistributions of source code must retain the above copyright
10 // notice, this list of conditions and the following disclaimer.
11 // * Redistributions in binary form must reproduce the above copyright
12 // notice, this list of conditions and the following disclaimer in the
13 // documentation and/or other materials provided with the distribution.
14 //
15 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
16 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
17 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
18 // DISCLAIMED. IN NO EVENT SHALL TAMIR ATIAS BE LIABLE FOR ANY
19 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
20 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
21 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
22 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
23 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
24 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 //
26 // Author: Tamir Atias
27 //-----------------------------------------------------------------------------
28 #pragma once
29 
30 #include <vector>
31 #include <string>
32 
33 #include "TmxPropertySet.h"
34 
35 namespace Tmx
36 {
37  class Layer;
38  class TileLayer;
39  class ImageLayer;
40  class ObjectGroup;
41  class GroupLayer;
42  class Tileset;
43 
44  //-------------------------------------------------------------------------
46  //-------------------------------------------------------------------------
47  enum MapError
48  {
50  TMX_COULDNT_OPEN = 0x01,
51 
54  TMX_PARSING_ERROR = 0x02,
55 
57  TMX_INVALID_FILE_SIZE = 0x04
58  };
59 
60  //-------------------------------------------------------------------------
62  //-------------------------------------------------------------------------
63  enum MapOrientation
64  {
66  TMX_MO_ORTHOGONAL = 0x01,
67 
69  TMX_MO_ISOMETRIC = 0x02,
70 
72  TMX_MO_STAGGERED = 0x03,
73 
75  TMX_MO_HEXAGONAL = 0x04
76  };
77 
78  //-------------------------------------------------------------------------
81  //-------------------------------------------------------------------------
82  enum MapRenderOrder
83  {
84  TMX_RIGHT_DOWN = 0x01,
85  TMX_RIGHT_UP = 0x02,
86  TMX_LEFT_DOWN = 0x03,
87  TMX_LEFT_UP= 0x03
88  };
89 
90  //-------------------------------------------------------------------------
92  //-------------------------------------------------------------------------
93  enum MapStaggerAxis
94  {
95  TMX_SA_NONE = 0x00, // if the map is not hexagonal
96  TMX_SA_X = 0x01,
97  TMX_SA_Y = 0x02
98  };
99 
100  //-------------------------------------------------------------------------
102  //-------------------------------------------------------------------------
103  enum MapStaggerIndex
104  {
105  TMX_SI_NONE = 0x00, // if the map is not hexagonal
106  TMX_SI_EVEN = 0x01,
107  TMX_SI_ODD = 0x02
108  };
109 
110  //-------------------------------------------------------------------------
114  //-------------------------------------------------------------------------
115  class Map
116  {
117  private:
118  // Prevent copy constructor.
119  Map(const Map &_map);
120 
121  public:
122  Map();
123  ~Map();
124 
127  void ParseFile(const std::string &fileName);
128 
130  void ParseText(const std::string &text);
131 
133  const std::string &GetFilename() const { return file_name; }
134 
136  const std::string &GetFilepath() const { return file_path; }
137 
139  Tmx::Color GetBackgroundColor() const { return background_color; }
140 
142  double GetVersion() const { return version; }
143 
145  Tmx::MapOrientation GetOrientation() const { return orientation; }
146 
148  Tmx::MapRenderOrder GetRenderOrder() const { return render_order; }
149 
151  Tmx::MapStaggerAxis GetStaggerAxis() const { return stagger_axis; }
152 
154  Tmx::MapStaggerIndex GetStaggerIndex() const { return stagger_index; }
155 
157  int GetWidth() const { return width; }
158 
160  int GetHeight() const { return height; }
161 
163  int GetTileWidth() const { return tile_width; }
164 
166  int GetTileHeight() const { return tile_height; }
167 
169  int GetNextObjectId() const { return next_object_id; }
170 
172  int GetHexsideLength() const { return hexside_length; }
173 
175  const Tmx::Layer *GetLayer(int index) const { return layers.at(index); }
176 
178  int GetNumLayers() const { return layers.size(); }
179 
181  const std::vector< Tmx::Layer* > &GetLayers() const { return layers; }
182 
184  const Tmx::TileLayer *GetTileLayer(int index) const { return tile_layers.at(index); }
185 
187  int GetNumTileLayers() const { return tile_layers.size(); }
188 
190  const std::vector< Tmx::TileLayer* > &GetTileLayers() const { return tile_layers; }
191 
193  const Tmx::ObjectGroup *GetObjectGroup(int index) const { return object_groups.at(index); }
194 
196  int GetNumObjectGroups() const { return object_groups.size(); }
197 
199  const std::vector< Tmx::ObjectGroup* > &GetObjectGroups() const { return object_groups; }
200 
202  const Tmx::ImageLayer *GetImageLayer(int index) const { return image_layers.at(index); }
203 
205  int GetNumImageLayers() const { return image_layers.size(); }
206 
208  const std::vector< Tmx::ImageLayer* > &GetImageLayers() const { return image_layers; }
209 
210  const Tmx::GroupLayer *GetGroupLayer(int index) const { return group_layers.at(index); }
211  int GetNumGroupLayers() const { return group_layers.size(); }
212  const std::vector< Tmx::GroupLayer* > &GetGroupLayers() const { return group_layers; }
213 
215  int FindTilesetIndex(int gid) const;
216 
218  const Tmx::Tileset *FindTileset(int gid) const;
219 
221  const Tmx::Tileset *GetTileset(int index) const { return tilesets.at(index); }
222 
224  int GetNumTilesets() const { return tilesets.size(); }
225 
227  const std::vector< Tmx::Tileset* > &GetTilesets() const { return tilesets; }
228 
230  bool HasError() const { return has_error; }
231 
233  const std::string &GetErrorText() const { return error_text; }
234 
236  unsigned char GetErrorCode() const { return error_code; }
237 
239  const Tmx::PropertySet &GetProperties() const { return properties; }
240 
241  private:
242  std::string file_name;
243  std::string file_path;
244 
245  Tmx::Color background_color;
246 
247  double version;
248  Tmx::MapOrientation orientation;
249  Tmx::MapRenderOrder render_order;
250  Tmx::MapStaggerAxis stagger_axis;
251  Tmx::MapStaggerIndex stagger_index;
252 
253  int width;
254  int height;
255  int tile_width;
256  int tile_height;
257  int next_object_id;
258  int hexside_length;
259 
260  std::vector< Tmx::Layer* > layers;
261  std::vector< Tmx::TileLayer* > tile_layers;
262  std::vector< Tmx::ImageLayer* > image_layers;
263  std::vector< Tmx::ObjectGroup* > object_groups;
264  std::vector< Tmx::GroupLayer* > group_layers;
265  std::vector< Tmx::Tileset* > tilesets;
266 
267  bool has_error;
268  unsigned char error_code;
269  std::string error_text;
270 
271  Tmx::PropertySet properties;
272 
273  // Parse a 'map' node.
274  void Parse(tinyxml2::XMLNode *mapNode);
275  };
276 }
const std::string & GetFilepath() const
Get a path to the directory of the map file if any.
Definition: TmxMap.h:136
Base class for other layer types.
Definition: TmxLayer.h:54
A class used for holding groups of layers to create a layer heirarchy.
Definition: TmxGroupLayer.h:40
A class used for holding a list of objects.
Definition: TmxObjectGroup.h:48
const std::vector< Tmx::Layer * > & GetLayers() const
Get the whole layers collection.
Definition: TmxMap.h:181
Tmx::Color GetBackgroundColor() const
Get the background color of the map file. If unset, return a fully transparent color.
Definition: TmxMap.h:139
const std::vector< Tmx::ImageLayer * > & GetImageLayers() const
Get the whole collection of image layers.
Definition: TmxMap.h:208
void ParseText(const std::string &text)
Parse text containing TMX formatted XML.
Definition: TmxMap.cpp:167
Used for storing information about the tile ids for every tile layer.
Definition: TmxTileLayer.h:68
A class used for storing information about each of the tilesets.
Definition: TmxTileset.h:51
const Tmx::PropertySet & GetProperties() const
Get the property set.
Definition: TmxMap.h:239
const Tmx::Tileset * GetTileset(int index) const
Get a tileset by an index.
Definition: TmxMap.h:221
const Tmx::ObjectGroup * GetObjectGroup(int index) const
Get the object group at a certain index.
Definition: TmxMap.h:193
Tmx::MapStaggerAxis GetStaggerAxis() const
Get the stagger axis of the map.
Definition: TmxMap.h:151
void ParseFile(const std::string &fileName)
Read a file and parse it.
Definition: TmxMap.cpp:134
int GetNumImageLayers() const
Get the amount of image layers.
Definition: TmxMap.h:205
const std::vector< Tmx::TileLayer * > & GetTileLayers() const
Get the whole collection of tile layers.
Definition: TmxMap.h:190
int GetNumLayers() const
Get the amount of layers.
Definition: TmxMap.h:178
int GetTileHeight() const
Get the height of a tile, in pixels.
Definition: TmxMap.h:166
int GetNumObjectGroups() const
Get the amount of object groups.
Definition: TmxMap.h:196
This class is the root class of the parser.
Definition: TmxMap.h:115
This class contains a map of properties.
Definition: TmxPropertySet.h:47
int GetTileWidth() const
Get the width of a tile, in pixels.
Definition: TmxMap.h:163
int GetHexsideLength() const
Get the hexside length.
Definition: TmxMap.h:172
A class used for holding information about a background image.
Definition: TmxImageLayer.h:41
Tmx::MapOrientation GetOrientation() const
Get the orientation of the map.
Definition: TmxMap.h:145
bool HasError() const
Get whether there was an error or not.
Definition: TmxMap.h:230
const std::string & GetFilename() const
Get the filename used to read the map.
Definition: TmxMap.h:133
int GetNumTilesets() const
Get the amount of tilesets.
Definition: TmxMap.h:224
int GetHeight() const
Get the height of the map, in tiles.
Definition: TmxMap.h:160
unsigned char GetErrorCode() const
Get a number that identifies the error. (TMX_ preceded constants)
Definition: TmxMap.h:236
const Tmx::ImageLayer * GetImageLayer(int index) const
Get the image layer at a certain index.
Definition: TmxMap.h:202
Tmx::MapRenderOrder GetRenderOrder() const
Get the render order of the map.
Definition: TmxMap.h:148
A class used for storing information about a color.
Definition: TmxColor.h:38
int GetWidth() const
Get the width of the map, in tiles.
Definition: TmxMap.h:157
const Tmx::Layer * GetLayer(int index) const
Get the layer at a certain index.
Definition: TmxMap.h:175
int GetNextObjectId() const
Get the next object id.
Definition: TmxMap.h:169
int GetNumTileLayers() const
Get the amount of tile layers.
Definition: TmxMap.h:187
const std::vector< Tmx::Tileset * > & GetTilesets() const
Get the collection of tilesets.
Definition: TmxMap.h:227
const Tmx::Tileset * FindTileset(int gid) const
Find a tileset for a specific gid.
Definition: TmxMap.cpp:203
double GetVersion() const
Get the version of the map.
Definition: TmxMap.h:142
int FindTilesetIndex(int gid) const
Find the tileset index for a tileset using a tile gid.
Definition: TmxMap.cpp:186
const std::vector< Tmx::ObjectGroup * > & GetObjectGroups() const
Get the whole collection of object groups.
Definition: TmxMap.h:199
const Tmx::TileLayer * GetTileLayer(int index) const
Get the tile layer at a certain index.
Definition: TmxMap.h:184
const std::string & GetErrorText() const
Get an error string containing the error in text format.
Definition: TmxMap.h:233
Tmx::MapStaggerIndex GetStaggerIndex() const
Get the stagger index of the map.
Definition: TmxMap.h:154