Gamedev Framework (gf)  0.9.0
A C++14 framework for 2D games
Tmx.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2018 Julien Bernard
4  *
5  * This software is provided 'as-is', without any express or implied
6  * warranty. In no event will the authors be held liable for any damages
7  * arising from the use of this software.
8  *
9  * Permission is granted to anyone to use this software for any purpose,
10  * including commercial applications, and to alter it and redistribute it
11  * freely, subject to the following restrictions:
12  *
13  * 1. The origin of this software must not be misrepresented; you must not
14  * claim that you wrote the original software. If you use this software
15  * in a product, an acknowledgment in the product documentation would be
16  * appreciated but is not required.
17  * 2. Altered source versions must be plainly marked as such, and must not be
18  * misrepresented as being the original software.
19  * 3. This notice may not be removed or altered from any source distribution.
20  */
21 #ifndef GF_TMX_H
22 #define GF_TMX_H
23 
24 #include <cstdint>
25 
26 #include <map>
27 #include <memory>
28 #include <string>
29 #include <vector>
30 
31 #include "Flags.h"
32 #include "Flip.h"
33 #include "Id.h"
34 #include "Path.h"
35 #include "Portability.h"
36 #include "Rect.h"
37 #include "Time.h"
38 #include "Vector.h"
39 
40 namespace gf {
41 #ifndef DOXYGEN_SHOULD_SKIP_THIS
42 inline namespace v1 {
43 #endif
44 
48  enum class TmxOrientation {
49  Unknown,
50  Orthogonal,
51  Isometric,
52  Staggered,
53  Hexagonal,
54  };
55 
60  enum class TmxStaggerIndex {
61  Odd,
62  Even,
63  };
64 
69  enum class TmxStaggerAxis {
70  X,
71  Y,
72  };
73 
78  enum class TmxRenderOrder {
79  RightDown,
80  RightUp,
81  LeftDown,
82  LeftUp,
83  };
84 
90  class GF_API TmxProperties {
91  public:
95  TmxProperties() = default;
96 
100  TmxProperties(TmxProperties&&) = default;
101 
105  TmxProperties& operator=(TmxProperties&&) = default;
106 
113  void addStringProperty(std::string name, std::string value);
114 
121  void addIntProperty(std::string name, int value);
122 
129  void addFloatProperty(std::string name, double value);
130 
137  void addBoolProperty(std::string name, bool value);
138 
145  void addColorProperty(std::string name, Color4u value);
146 
153  void addFileProperty(std::string name, Path value);
154 
162  std::string getStringProperty(const std::string& name, const std::string& def) const;
163 
171  int getIntProperty(const std::string& name, int def) const;
172 
180  double getFloatProperty(const std::string& name, double def) const;
181 
189  bool getBoolProperty(const std::string& name, bool def) const;
190 
198  Color4u getColorProperty(const std::string& name, const Color4u& def) const;
199 
207  Path getFileProperty(const std::string& name, const Path& def) const;
208 
209  private:
210  std::map<std::string, std::string> m_stringProps;
211  std::map<std::string, int> m_intProps;
212  std::map<std::string, double> m_floatProps;
213  std::map<std::string, bool> m_boolProps;
214  std::map<std::string, Color4u> m_colorProps;
215  std::map<std::string, Path> m_fileProps;
216  };
217 
218  struct TmxLayers;
219 
220  struct TmxTileLayer;
221  struct TmxObjectLayer;
222  struct TmxImageLayer;
223  struct TmxGroupLayer;
224 
232  class GF_API TmxVisitor {
233  public:
237  virtual ~TmxVisitor();
238 
245  virtual void visitTileLayer(const TmxLayers& map, const TmxTileLayer& layer);
246 
253  virtual void visitObjectLayer(const TmxLayers& map, const TmxObjectLayer& layer);
254 
261  virtual void visitImageLayer(const TmxLayers& map, const TmxImageLayer& layer);
262 
269  virtual void visitGroupLayer(const TmxLayers& map, const TmxGroupLayer& layer);
270  };
271 
281  struct GF_API TmxLayer {
285  virtual ~TmxLayer();
286 
295  virtual void accept(const TmxLayers& map, TmxVisitor& visitor) const = 0;
296 
298  std::string name;
299  double opacity;
300  bool visible;
302  };
303 
308  struct GF_API TmxCell {
309  unsigned gid;
310  Flags<Flip> flip = None;
311  };
312 
317  struct GF_API TmxTileLayer : public TmxLayer {
318  std::vector<TmxCell> cells;
319 
320  virtual void accept(const TmxLayers& map, TmxVisitor& visitor) const override;
321  };
322 
327  enum class TmxDrawOrder {
328  TopDown,
329  Index,
330  };
331 
332 
348  struct GF_API TmxObject {
352  virtual ~TmxObject();
353 
357  enum Kind {
364  };
365 
367 
369 
370  Id id;
371  std::string name;
372  std::string type;
374  double rotation;
375  bool visible;
376  };
377 
382  struct GF_API TmxRectangle : public TmxObject {
384  };
385 
390  struct GF_API TmxEllipse : public TmxObject {
392  };
393 
398  struct GF_API TmxTileObject : public TmxObject {
399  unsigned gid;
401  };
402 
409  struct GF_API TmxPolyline : public TmxObject {
410  std::vector<Vector2i> points;
411  };
412 
419  struct GF_API TmxPolygon : public TmxObject {
420  std::vector<Vector2i> points;
421  };
422 
427  struct GF_API TmxText : public TmxObject {
428  std::string text;
429 
430  std::string fontFamily;
431  unsigned sizeInPixels;
432  bool wrap;
434  bool bold;
435  bool italic;
436  bool underline;
437  bool strikeout;
438  bool kerning;
439 
443  enum class HAlign {
444  Left,
445  Center,
446  Right,
447  };
448 
450 
454  enum class VAlign {
455  Top,
456  Center,
457  Bottom,
458  };
459 
461  };
462 
467  struct GF_API TmxObjectLayer : public TmxLayer {
470  std::vector<std::unique_ptr<TmxObject>> objects;
471 
472  virtual void accept(const TmxLayers& map, TmxVisitor& visitor) const override;
473  };
474 
479  struct GF_API TmxImage {
480  std::string format;
484  };
485 
490  struct GF_API TmxImageLayer : public TmxLayer {
491  std::unique_ptr<TmxImage> image;
492 
493  virtual void accept(const TmxLayers& map, TmxVisitor& visitor) const override;
494  };
495 
496 
501  struct GF_API TmxGroupLayer : public TmxLayer {
502  std::vector<std::unique_ptr<TmxLayer>> layers;
503 
504  virtual void accept(const TmxLayers& map, TmxVisitor& visitor) const override;
505  };
506 
513  struct GF_API TmxFrame {
514  unsigned tileId;
516  };
517 
522  struct GF_API TmxAnimation {
523  std::vector<TmxFrame> frames;
524  };
525 
532  struct GF_API TmxTerrain {
534  std::string name;
535  unsigned tile;
536  };
537 
544  struct GF_API TmxTile {
546  unsigned id;
547  std::string type;
548  std::array<unsigned, 4> terrain;
549  unsigned probability;
550 
551  std::unique_ptr<TmxImage> image;
552  std::unique_ptr<TmxObjectLayer> objects;
553  std::unique_ptr<TmxAnimation> animation;
554  };
555 
560  struct GF_API TmxTileset {
562 
563  unsigned firstGid;
564  std::string name;
566  unsigned spacing;
567  unsigned margin;
568  unsigned tileCount;
569  unsigned columnCount;
570 
572 
573  std::unique_ptr<TmxImage> image;
574  std::vector<TmxTerrain> terrains;
575  std::vector<TmxTile> tiles;
576 
577  public:
584  const TmxTile *getTile(unsigned id) const noexcept;
585 
593  RectU getSubTexture(unsigned id, Vector2u size) const noexcept;
594 
595  };
596 
601  struct GF_API TmxLayers {
603 
604  std::string version;
605  std::string tiledVersion;
608 
611 
612  unsigned hexSideLength;
615 
617 
618  unsigned nextObjectId;
619 
620  std::vector<TmxTileset> tilesets;
621  std::vector<std::unique_ptr<TmxLayer>> layers;
622 
623  public:
624 
631  const TmxTileset *getTileSetFromGID(unsigned gid) const noexcept;
632 
638  void visitLayers(TmxVisitor& visitor) const;
639 
645  bool loadFromFile(const Path& filename);
646 
647  };
648 
649 #ifndef DOXYGEN_SHOULD_SKIP_THIS
650 }
651 #endif
652 }
653 
654 #endif // GF_TMX_H
Vector2u size
The size of the image.
Definition: Tmx.h:483
unsigned tileCount
The number of tiles.
Definition: Tmx.h:568
double opacity
The opacity of the layer.
Definition: Tmx.h:299
VAlign valign
The vertical alignment of the text.
Definition: Tmx.h:460
Flags< Flip > flip
Definition: Tmx.h:400
Vector2u size
The size of the ellipse.
Definition: Tmx.h:391
A layer with an image.
Definition: Tmx.h:490
bool bold
Is the text in bold?
Definition: Tmx.h:434
TmxStaggerIndex
Stagger index of the hexagonal map.
Definition: Tmx.h:60
unsigned columnCount
The number of columns.
Definition: Tmx.h:569
A visitor for layers in the visitor pattern.
Definition: Tmx.h:232
TmxProperties properties
The properties of the terrain.
Definition: Tmx.h:533
bool wrap
The wrap mode.
Definition: Tmx.h:432
std::string text
The text of the object.
Definition: Tmx.h:428
std::vector< TmxTerrain > terrains
The terrains of the tileset.
Definition: Tmx.h:574
No alignement.
A rectangle object.
Definition: Tmx.h:382
std::string name
The name of the object.
Definition: Tmx.h:371
std::vector< TmxFrame > frames
The frames of the animation.
Definition: Tmx.h:523
unsigned gid
The global id of the tile.
Definition: Tmx.h:309
unsigned sizeInPixels
The size of the text in pixel.
Definition: Tmx.h:431
Color4u backgroundColor
The background color.
Definition: Tmx.h:616
std::vector< std::unique_ptr< TmxObject > > objects
The objects of the layer.
Definition: Tmx.h:470
TmxDrawOrder
The draw order of the objects.
Definition: Tmx.h:327
Vector2u size
The size of the rectangle.
Definition: Tmx.h:383
Id id
The id of the object.
Definition: Tmx.h:370
TmxStaggerAxis staggerAxis
The stagger axis for hexagonal map.
Definition: Tmx.h:613
std::unique_ptr< TmxImage > image
The image of the layer.
Definition: Tmx.h:491
An ellipse object.
Definition: Tmx.h:390
A hexagonal orientation.
TmxDrawOrder drawOrder
The draw order of the objects.
Definition: Tmx.h:469
double rotation
The rotation of the object.
Definition: Tmx.h:374
unsigned spacing
The spacing between tiles (in pixels)
Definition: Tmx.h:566
uint64_t Id
An identifier.
Definition: Id.h:39
unsigned tile
The representing tile for the terrain.
Definition: Tmx.h:535
A layer with objects.
Definition: Tmx.h:467
An ellipse object.
Definition: Tmx.h:359
A layer in the whole map.
Definition: Tmx.h:281
Path source
The path to the image.
Definition: Tmx.h:481
std::string tiledVersion
The tiled version of the map.
Definition: Tmx.h:605
std::string name
The name of the tileset.
Definition: Tmx.h:564
A tile animation.
Definition: Tmx.h:522
A TMX map.
Definition: Tmx.h:601
bool kerning
Is the text using kerning?
Definition: Tmx.h:438
A tile object.
Definition: Tmx.h:362
std::vector< TmxTileset > tilesets
The tilesets used in the map.
Definition: Tmx.h:620
A staggered orientation.
Represents a time value.
Definition: Time.h:74
std::unique_ptr< TmxAnimation > animation
The animation data of the tile.
Definition: Tmx.h:553
std::vector< TmxTile > tiles
The tiles of the tileset.
Definition: Tmx.h:575
Vector2u position
The position of the object.
Definition: Tmx.h:373
std::string fontFamily
The font family.
Definition: Tmx.h:430
Color4u transparent
The transparent color.
Definition: Tmx.h:482
Vector2i offset
The offset of the layer.
Definition: Tmx.h:301
A text object.
Definition: Tmx.h:427
HAlign halign
The horizontal alignment of the text.
Definition: Tmx.h:449
std::string type
The type of the object.
Definition: Tmx.h:372
TmxStaggerAxis
Stagger axis of the hexagonal map.
Definition: Tmx.h:69
std::vector< std::unique_ptr< TmxLayer > > layers
The layers of the map.
Definition: Tmx.h:621
std::string name
The name of the terrain.
Definition: Tmx.h:534
std::string version
The version of the map.
Definition: Tmx.h:604
unsigned id
The local id of the tile.
Definition: Tmx.h:546
std::vector< std::unique_ptr< TmxLayer > > layers
The other layers.
Definition: Tmx.h:502
TmxRenderOrder renderOrder
The render order of the map.
Definition: Tmx.h:607
Kind
The kind of object.
Definition: Tmx.h:357
The Y button.
Vector2u mapSize
The size of the map.
Definition: Tmx.h:609
The namespace for gf classes.
Definition: Action.h:34
A polyline object.
Definition: Tmx.h:409
unsigned hexSideLength
The length of the side for hexagonal map.
Definition: Tmx.h:612
TmxProperties properties
The properties of the tile.
Definition: Tmx.h:545
unsigned probability
The probability of the tile.
Definition: Tmx.h:549
std::unique_ptr< TmxObjectLayer > objects
The objects in the tile.
Definition: Tmx.h:552
A frame in a tile animation.
Definition: Tmx.h:513
Vector2i offset
The offset of the tileset.
Definition: Tmx.h:571
TmxProperties properties
The properties of the tileset.
Definition: Tmx.h:561
An even stagger index.
TmxProperties properties
The properties of the map.
Definition: Tmx.h:602
Kind kind
The kind of the object.
Definition: Tmx.h:366
std::string type
The type of the tile.
Definition: Tmx.h:547
std::vector< Vector2i > points
The points of the polygon.
Definition: Tmx.h:420
Vector2u tileSize
The size of the tileset.
Definition: Tmx.h:565
TmxOrientation orientation
The orientation of the map.
Definition: Tmx.h:606
Color4u color
The color of the text.
Definition: Tmx.h:433
A description of a kind of terrain on the map.
Definition: Tmx.h:532
A rectangle object.
Definition: Tmx.h:358
The properties for TMX entities.
Definition: Tmx.h:90
bool visible
The visibility of the object.
Definition: Tmx.h:375
VAlign
A vertical alignment.
Definition: Tmx.h:454
An isometric orientation.
The X button.
A rectangular part of a tileset.
Definition: Tmx.h:544
TmxRenderOrder
the render order of the tiles.
Definition: Tmx.h:78
std::string format
The format of the image.
Definition: Tmx.h:480
HAlign
A horizontal alignment.
Definition: Tmx.h:443
boost::filesystem::path Path
A path in the filesystem.
Definition: Path.h:41
TmxStaggerIndex staggerIndex
The stagger index for hexagonal map.
Definition: Tmx.h:614
Color4u color
The color of the objects.
Definition: Tmx.h:468
A polygon object.
Definition: Tmx.h:361
A geometrical object.
Definition: Tmx.h:348
A cell in a tile layer.
Definition: Tmx.h:308
std::unique_ptr< TmxImage > image
The image of this tile.
Definition: Tmx.h:551
Left alignement.
A text object.
Definition: Tmx.h:363
unsigned margin
The margin around tiles (in pixels)
Definition: Tmx.h:567
unsigned tileId
Definition: Tmx.h:514
TmxOrientation
The orientation of the map.
Definition: Tmx.h:48
TmxProperties properties
The properties of the object.
Definition: Tmx.h:368
A set of tiles in a single file (image or TSX file)
Definition: Tmx.h:560
unsigned nextObjectId
The next object id.
Definition: Tmx.h:618
Right alignement.
unsigned firstGid
The first global id of the tileset.
Definition: Tmx.h:563
unsigned gid
Definition: Tmx.h:399
An image put in the map identified by its global id.
Definition: Tmx.h:398
Vector2u tileSize
The size of the tiles.
Definition: Tmx.h:610
bool italic
Is the text in italic?
Definition: Tmx.h:435
A layer with tiles in cells.
Definition: Tmx.h:317
std::array< unsigned, 4 > terrain
The terrain if the corners (top-left, top-right, bottom-left, bottom-right)
Definition: Tmx.h:548
std::unique_ptr< TmxImage > image
The image of the tileset.
Definition: Tmx.h:573
std::vector< TmxCell > cells
The cells of the layer.
Definition: Tmx.h:318
A polyline object.
Definition: Tmx.h:360
A layer with other layers.
Definition: Tmx.h:501
bool strikeout
Is the text striked out?
Definition: Tmx.h:437
Time duration
Definition: Tmx.h:515
std::string name
The name of the layer.
Definition: Tmx.h:298
A odd stagger index.
A polygon object.
Definition: Tmx.h:419
Centered alignment.
bool visible
The visibility of the layer.
Definition: Tmx.h:300
TmxProperties properties
The properties of the layer.
Definition: Tmx.h:297
bool underline
Is the text underlined?
Definition: Tmx.h:436
std::vector< Vector2i > points
The points of the polyline.
Definition: Tmx.h:410
An orthogonal orientation.
A reference to an image.
Definition: Tmx.h:479