Gamedev Framework (gf)  0.14.0
A C++14 framework for 2D games
Tmx.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2019 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 "Stagger.h"
38 #include "Time.h"
39 #include "Vector.h"
40 
41 namespace gf {
42 #ifndef DOXYGEN_SHOULD_SKIP_THIS
43 inline namespace v1 {
44 #endif
45 
49  enum class TmxOrientation {
50  Unknown,
51  Orthogonal,
52  Isometric,
53  Staggered,
54  Hexagonal,
55  };
56 
61  enum class TmxRenderOrder {
62  RightDown,
63  RightUp,
64  LeftDown,
65  LeftUp,
66  };
67 
73  class GF_API TmxProperties {
74  public:
78  TmxProperties() = default;
79 
83  TmxProperties(TmxProperties&&) = default;
84 
88  TmxProperties& operator=(TmxProperties&&) = default;
89 
96  void addStringProperty(std::string name, std::string value);
97 
104  void addIntProperty(std::string name, int value);
105 
112  void addFloatProperty(std::string name, double value);
113 
120  void addBoolProperty(std::string name, bool value);
121 
128  void addColorProperty(std::string name, Color4u value);
129 
136  void addFileProperty(std::string name, Path value);
137 
145  std::string getStringProperty(const std::string& name, const std::string& def) const;
146 
154  int getIntProperty(const std::string& name, int def) const;
155 
163  double getFloatProperty(const std::string& name, double def) const;
164 
172  bool getBoolProperty(const std::string& name, bool def) const;
173 
181  Color4u getColorProperty(const std::string& name, const Color4u& def) const;
182 
190  Path getFileProperty(const std::string& name, const Path& def) const;
191 
192  private:
193  std::map<std::string, std::string> m_stringProps;
194  std::map<std::string, int> m_intProps;
195  std::map<std::string, double> m_floatProps;
196  std::map<std::string, bool> m_boolProps;
197  std::map<std::string, Color4u> m_colorProps;
198  std::map<std::string, Path> m_fileProps;
199  };
200 
201  struct TmxLayers;
202 
203  struct TmxTileLayer;
204  struct TmxObjectLayer;
205  struct TmxImageLayer;
206  struct TmxGroupLayer;
207 
215  class GF_API TmxVisitor {
216  public:
220  virtual ~TmxVisitor();
221 
228  virtual void visitTileLayer(const TmxLayers& map, const TmxTileLayer& layer);
229 
236  virtual void visitObjectLayer(const TmxLayers& map, const TmxObjectLayer& layer);
237 
244  virtual void visitImageLayer(const TmxLayers& map, const TmxImageLayer& layer);
245 
252  virtual void visitGroupLayer(const TmxLayers& map, const TmxGroupLayer& layer);
253  };
254 
264  struct GF_API TmxLayer {
268  virtual ~TmxLayer();
269 
278  virtual void accept(const TmxLayers& map, TmxVisitor& visitor) const = 0;
279 
281  std::string name;
282  double opacity;
283  bool visible;
285  };
286 
291  struct GF_API TmxCell {
292  int gid;
293  Flags<Flip> flip = None;
294  };
295 
300  struct GF_API TmxChunk {
303  std::vector<TmxCell> cells;
304  };
305 
310  struct GF_API TmxTileLayer : public TmxLayer {
311  std::vector<TmxCell> cells;
312  std::vector<TmxChunk> chunks;
313 
314  void accept(const TmxLayers& map, TmxVisitor& visitor) const override;
315  };
316 
321  enum class TmxDrawOrder {
322  TopDown,
323  Index,
324  };
325 
326 
342  struct GF_API TmxObject {
346  virtual ~TmxObject();
347 
351  enum Kind {
359  };
360 
362 
364 
365  int id;
366  std::string name;
367  std::string type;
369  double rotation;
370  bool visible;
371  };
372 
377  struct GF_API TmxRectangle : public TmxObject {
379  };
380 
385  struct GF_API TmxEllipse : public TmxObject {
387  };
388 
393  struct GF_API TmxTileObject : public TmxObject {
394  int gid;
396  };
397 
404  struct GF_API TmxPolyline : public TmxObject {
405  std::vector<Vector2f> points;
406  };
407 
414  struct GF_API TmxPolygon : public TmxObject {
415  std::vector<Vector2f> points;
416  };
417 
422  struct GF_API TmxText : public TmxObject {
423  std::string text;
424 
425  std::string fontFamily;
427  bool wrap;
429  bool bold;
430  bool italic;
431  bool underline;
432  bool strikeout;
433  bool kerning;
434 
438  enum class HAlign {
439  Left,
440  Center,
441  Right,
442  };
443 
445 
449  enum class VAlign {
450  Top,
451  Center,
452  Bottom,
453  };
454 
456  };
457 
462  struct GF_API TmxPoint : public TmxObject {
463  };
464 
469  struct GF_API TmxObjectLayer : public TmxLayer {
472  std::vector<std::unique_ptr<TmxObject>> objects;
473 
474  void accept(const TmxLayers& map, TmxVisitor& visitor) const override;
475  };
476 
481  struct GF_API TmxImage {
482  std::string format;
486  };
487 
492  struct GF_API TmxImageLayer : public TmxLayer {
493  std::unique_ptr<TmxImage> image;
494 
495  void accept(const TmxLayers& map, TmxVisitor& visitor) const override;
496  };
497 
498 
503  struct GF_API TmxGroupLayer : public TmxLayer {
504  std::vector<std::unique_ptr<TmxLayer>> layers;
505 
506  void accept(const TmxLayers& map, TmxVisitor& visitor) const override;
507  };
508 
515  struct GF_API TmxFrame {
516  int tileId;
518  };
519 
524  struct GF_API TmxAnimation {
525  std::vector<TmxFrame> frames;
526  };
527 
534  struct GF_API TmxTerrain {
536  std::string name;
537  int tile;
538  };
539 
546  struct GF_API TmxTile {
548  int id;
549  std::string type;
550  std::array<int, 4> terrain;
552 
553  std::unique_ptr<TmxImage> image;
554  std::unique_ptr<TmxObjectLayer> objects;
555  std::unique_ptr<TmxAnimation> animation;
556  };
557 
562  struct GF_API TmxTileset {
564 
565  int firstGid;
566  std::string name;
568  int spacing;
569  int margin;
570  int tileCount;
572 
574 
575  std::unique_ptr<TmxImage> image;
576  std::vector<TmxTerrain> terrains;
577  std::vector<TmxTile> tiles;
578 
579  public:
586  const TmxTile *getTile(int id) const noexcept;
587 
595  RectI getSubTexture(int id, Vector2i size) const noexcept;
596  };
597 
602  struct GF_API TmxLayers {
604 
605  std::string version;
606  std::string tiledVersion;
609 
610  bool infinite;
613 
617 
619 
621 
622  std::vector<TmxTileset> tilesets;
623  std::vector<std::unique_ptr<TmxLayer>> layers;
624 
625  public:
632  const TmxTileset *getTileSetFromGID(int gid) const noexcept;
633 
639  void visitLayers(TmxVisitor& visitor) const;
640 
646  bool loadFromFile(const Path& filename);
647  };
648 
649 #ifndef DOXYGEN_SHOULD_SKIP_THIS
650 }
651 #endif
652 }
653 
654 #endif // GF_TMX_H
double opacity
The opacity of the layer.
Definition: Tmx.h:282
VAlign valign
The vertical alignment of the text.
Definition: Tmx.h:455
Vector2i tileSize
The size of a tile in the tileset.
Definition: Tmx.h:567
Flags< Flip > flip
Definition: Tmx.h:395
A layer with an image.
Definition: Tmx.h:492
Vector2i size
Definition: Tmx.h:302
bool bold
Is the text in bold?
Definition: Tmx.h:429
int spacing
The spacing between tiles (in pixels)
Definition: Tmx.h:568
StaggerAxis staggerAxis
The stagger axis for hexagonal map.
Definition: Tmx.h:615
int gid
The global id of the tile.
Definition: Tmx.h:292
A visitor for layers in the visitor pattern.
Definition: Tmx.h:215
TmxProperties properties
The properties of the terrain.
Definition: Tmx.h:535
bool wrap
The wrap mode.
Definition: Tmx.h:427
std::string text
The text of the object.
Definition: Tmx.h:423
std::vector< TmxTerrain > terrains
The terrains of the tileset.
Definition: Tmx.h:576
No alignement.
A rectangle object.
Definition: Tmx.h:377
std::string name
The name of the object.
Definition: Tmx.h:366
A chunk in a tile layer (for infinite maps)
Definition: Tmx.h:300
std::vector< Vector2f > points
The points of the polygon.
Definition: Tmx.h:415
std::vector< TmxFrame > frames
The frames of the animation.
Definition: Tmx.h:525
Color4u backgroundColor
The background color.
Definition: Tmx.h:618
std::vector< std::unique_ptr< TmxObject > > objects
The objects of the layer.
Definition: Tmx.h:472
TmxDrawOrder
The draw order of the objects.
Definition: Tmx.h:321
std::unique_ptr< TmxImage > image
The image of the layer.
Definition: Tmx.h:493
An ellipse object.
Definition: Tmx.h:385
A hexagonal orientation.
Vector2i mapSize
The size of the map.
Definition: Tmx.h:611
TmxDrawOrder drawOrder
The draw order of the objects.
Definition: Tmx.h:471
double rotation
The rotation of the object.
Definition: Tmx.h:369
std::vector< Vector2f > points
The points of the polyline.
Definition: Tmx.h:405
A layer with objects.
Definition: Tmx.h:469
StaggerAxis
Stagger axis in a staggered or hexagonal map.
Definition: Stagger.h:48
An ellipse object.
Definition: Tmx.h:353
A layer in the whole map.
Definition: Tmx.h:264
Path source
The path to the image.
Definition: Tmx.h:483
std::string tiledVersion
The tiled version of the map.
Definition: Tmx.h:606
std::string name
The name of the tileset.
Definition: Tmx.h:566
A tile animation.
Definition: Tmx.h:524
A TMX map.
Definition: Tmx.h:602
bool kerning
Is the text using kerning?
Definition: Tmx.h:433
A tile object.
Definition: Tmx.h:356
std::vector< TmxTileset > tilesets
The tilesets used in the map.
Definition: Tmx.h:622
A staggered orientation.
Represents a time value.
Definition: Time.h:74
int sizeInPixels
The size of the text in pixel.
Definition: Tmx.h:426
std::unique_ptr< TmxAnimation > animation
The animation data of the tile.
Definition: Tmx.h:555
std::vector< TmxTile > tiles
The tiles of the tileset.
Definition: Tmx.h:577
int id
The id of the object.
Definition: Tmx.h:365
int probability
The probability of the tile.
Definition: Tmx.h:551
Vector2i position
Definition: Tmx.h:301
std::string fontFamily
The font family.
Definition: Tmx.h:425
Color4u transparent
The transparent color.
Definition: Tmx.h:484
Vector2i offset
The offset of the layer.
Definition: Tmx.h:284
int tileCount
The number of tiles.
Definition: Tmx.h:570
A text object.
Definition: Tmx.h:422
HAlign halign
The horizontal alignment of the text.
Definition: Tmx.h:444
std::string type
The type of the object.
Definition: Tmx.h:367
std::vector< std::unique_ptr< TmxLayer > > layers
The layers of the map.
Definition: Tmx.h:623
std::string name
The name of the terrain.
Definition: Tmx.h:536
std::string version
The version of the map.
Definition: Tmx.h:605
std::vector< std::unique_ptr< TmxLayer > > layers
The other layers.
Definition: Tmx.h:504
TmxRenderOrder renderOrder
The render order of the map.
Definition: Tmx.h:608
Kind
The kind of object.
Definition: Tmx.h:351
int tileId
Definition: Tmx.h:516
A point object.
Definition: Tmx.h:462
int nextObjectId
The next object id.
Definition: Tmx.h:620
The namespace for gf classes.
Definition: Action.h:35
A polyline object.
Definition: Tmx.h:404
TmxProperties properties
The properties of the tile.
Definition: Tmx.h:547
std::unique_ptr< TmxObjectLayer > objects
The objects in the tile.
Definition: Tmx.h:554
int firstGid
The first global id of the tileset.
Definition: Tmx.h:565
A frame in a tile animation.
Definition: Tmx.h:515
Vector2i offset
The offset of the tileset.
Definition: Tmx.h:573
TmxProperties properties
The properties of the tileset.
Definition: Tmx.h:563
TmxProperties properties
The properties of the map.
Definition: Tmx.h:603
Kind kind
The kind of the object.
Definition: Tmx.h:361
std::string type
The type of the tile.
Definition: Tmx.h:549
int gid
Definition: Tmx.h:394
A 4D vector.
Definition: Vector.h:838
TmxOrientation orientation
The orientation of the map.
Definition: Tmx.h:607
Color4u color
The color of the text.
Definition: Tmx.h:428
int margin
The margin around tiles (in pixels)
Definition: Tmx.h:569
std::array< int, 4 > terrain
The terrain if the corners (top-left, top-right, bottom-left, bottom-right)
Definition: Tmx.h:550
A description of a kind of terrain on the map.
Definition: Tmx.h:534
A rectangle object.
Definition: Tmx.h:352
The properties for TMX entities.
Definition: Tmx.h:73
bool visible
The visibility of the object.
Definition: Tmx.h:370
VAlign
A vertical alignment.
Definition: Tmx.h:449
An isometric orientation.
A rectangular part of a tileset.
Definition: Tmx.h:546
StaggerIndex staggerIndex
The stagger index for hexagonal map.
Definition: Tmx.h:616
TmxRenderOrder
the render order of the tiles.
Definition: Tmx.h:61
std::string format
The format of the image.
Definition: Tmx.h:482
int id
The local id of the tile.
Definition: Tmx.h:548
HAlign
A horizontal alignment.
Definition: Tmx.h:438
boost::filesystem::path Path
A path in the filesystem.
Definition: Path.h:44
Color4u color
The color of the objects.
Definition: Tmx.h:470
A polygon object.
Definition: Tmx.h:355
A geometrical object.
Definition: Tmx.h:342
A cell in a tile layer.
Definition: Tmx.h:291
std::unique_ptr< TmxImage > image
The image of this tile.
Definition: Tmx.h:553
Left alignement.
A text object.
Definition: Tmx.h:357
Vector2f size
The size of the rectangle.
Definition: Tmx.h:378
TmxOrientation
The orientation of the map.
Definition: Tmx.h:49
TmxProperties properties
The properties of the object.
Definition: Tmx.h:363
A set of tiles in a single file (image or TSX file)
Definition: Tmx.h:562
int hexSideLength
The length of the side for hexagonal map.
Definition: Tmx.h:614
Right alignement.
An image put in the map identified by its global id.
Definition: Tmx.h:393
int columnCount
The number of columns.
Definition: Tmx.h:571
Vector2f size
The size of the ellipse.
Definition: Tmx.h:386
bool italic
Is the text in italic?
Definition: Tmx.h:430
A layer with tiles in cells.
Definition: Tmx.h:310
std::unique_ptr< TmxImage > image
The image of the tileset.
Definition: Tmx.h:575
std::vector< TmxCell > cells
The cells of the layer.
Definition: Tmx.h:311
A polyline object.
Definition: Tmx.h:354
A layer with other layers.
Definition: Tmx.h:503
bool strikeout
Is the text striked out?
Definition: Tmx.h:432
Vector2i tileSize
The size of the tiles.
Definition: Tmx.h:612
StaggerIndex
Stagger index in a staggered or hexagonal map.
Definition: Stagger.h:37
std::vector< TmxCell > cells
Definition: Tmx.h:303
Time duration
Definition: Tmx.h:517
std::string name
The name of the layer.
Definition: Tmx.h:281
bool infinite
Is the map infinite?
Definition: Tmx.h:610
A point object.
Definition: Tmx.h:358
A polygon object.
Definition: Tmx.h:414
Centered alignment.
int tile
The representing tile for the terrain.
Definition: Tmx.h:537
bool visible
The visibility of the layer.
Definition: Tmx.h:283
std::vector< TmxChunk > chunks
The chunks of the layer.
Definition: Tmx.h:312
TmxProperties properties
The properties of the layer.
Definition: Tmx.h:280
bool underline
Is the text underlined?
Definition: Tmx.h:431
Vector2f position
The position of the object.
Definition: Tmx.h:368
An orthogonal orientation.
Vector2i size
The size of the image.
Definition: Tmx.h:485
A reference to an image.
Definition: Tmx.h:481