Gamedev Framework (gf)  0.19.0
A C++17 framework for 2D games
Tmx.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2021 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 <array>
27 #include <map>
28 #include <memory>
29 #include <string>
30 #include <vector>
31 
32 #include "CoreApi.h"
33 #include "Flags.h"
34 #include "Id.h"
35 #include "Path.h"
36 #include "Rect.h"
37 #include "Stagger.h"
38 #include "TileTypes.h"
39 #include "Time.h"
40 #include "Vector.h"
41 
42 namespace gf {
43 #ifndef DOXYGEN_SHOULD_SKIP_THIS
44 inline namespace v1 {
45 #endif
46 
50  enum class TmxRenderOrder {
51  RightDown,
52  RightUp,
53  LeftDown,
54  LeftUp,
55  };
56 
62  class GF_CORE_API TmxProperties {
63  public:
67  TmxProperties() = default;
68 
72  TmxProperties(TmxProperties&&) = default;
73 
77  TmxProperties& operator=(TmxProperties&&) = default;
78 
85  void addStringProperty(std::string name, std::string value);
86 
93  void addIntProperty(std::string name, int value);
94 
101  void addFloatProperty(std::string name, double value);
102 
109  void addBoolProperty(std::string name, bool value);
110 
117  void addColorProperty(std::string name, Color4u value);
118 
125  void addFileProperty(std::string name, Path value);
126 
134  std::string getStringProperty(const std::string& name, const std::string& def) const;
135 
143  int getIntProperty(const std::string& name, int def) const;
144 
152  double getFloatProperty(const std::string& name, double def) const;
153 
161  bool getBoolProperty(const std::string& name, bool def) const;
162 
170  Color4u getColorProperty(const std::string& name, const Color4u& def) const;
171 
179  Path getFileProperty(const std::string& name, const Path& def) const;
180 
181  private:
182  std::map<std::string, std::string> m_stringProps;
183  std::map<std::string, int> m_intProps;
184  std::map<std::string, double> m_floatProps;
185  std::map<std::string, bool> m_boolProps;
186  std::map<std::string, Color4u> m_colorProps;
187  std::map<std::string, Path> m_fileProps;
188  };
189 
190  struct TmxLayers;
191 
192  struct TmxTileLayer;
193  struct TmxObjectLayer;
194  struct TmxImageLayer;
195  struct TmxGroupLayer;
196 
204  class GF_CORE_API TmxVisitor {
205  public:
209  virtual ~TmxVisitor();
210 
217  virtual void visitTileLayer(const TmxLayers& map, const TmxTileLayer& layer);
218 
225  virtual void visitObjectLayer(const TmxLayers& map, const TmxObjectLayer& layer);
226 
233  virtual void visitImageLayer(const TmxLayers& map, const TmxImageLayer& layer);
234 
241  virtual void visitGroupLayer(const TmxLayers& map, const TmxGroupLayer& layer);
242  };
243 
253  struct GF_CORE_API TmxLayer {
257  virtual ~TmxLayer();
258 
267  virtual void accept(const TmxLayers& map, TmxVisitor& visitor) const = 0;
268 
270  std::string name;
271  double opacity;
272  bool visible;
274  };
275 
280  struct GF_CORE_API TmxCell {
281  int gid;
282  Flags<Flip> flip = None;
283  };
284 
289  struct GF_CORE_API TmxChunk {
292  std::vector<TmxCell> cells;
293  };
294 
299  struct GF_CORE_API TmxTileLayer : public TmxLayer {
300  std::vector<TmxCell> cells;
301  std::vector<TmxChunk> chunks;
302 
303  void accept(const TmxLayers& map, TmxVisitor& visitor) const override;
304  };
305 
310  enum class TmxDrawOrder {
311  TopDown,
312  Index,
313  };
314 
315 
332  struct GF_CORE_API TmxObject {
336  virtual ~TmxObject();
337 
341  enum Kind {
349  };
350 
352 
354 
355  int id;
356  std::string name;
357  std::string type;
359  double rotation;
360  bool visible;
361  };
362 
367  struct GF_CORE_API TmxRectangle : public TmxObject {
369  };
370 
375  struct GF_CORE_API TmxEllipse : public TmxObject {
377  };
378 
383  struct GF_CORE_API TmxTileObject : public TmxObject {
384  int gid;
386  };
387 
394  struct GF_CORE_API TmxPolyline : public TmxObject {
395  std::vector<Vector2f> points;
396  };
397 
404  struct GF_CORE_API TmxPolygon : public TmxObject {
405  std::vector<Vector2f> points;
406  };
407 
412  struct GF_CORE_API TmxText : public TmxObject {
413  std::string text;
414 
415  std::string fontFamily;
417  bool wrap;
419  bool bold;
420  bool italic;
421  bool underline;
422  bool strikeout;
423  bool kerning;
424 
428  enum class HAlign {
429  Left,
430  Center,
431  Right,
432  };
433 
435 
439  enum class VAlign {
440  Top,
441  Center,
442  Bottom,
443  };
444 
446  };
447 
452  struct GF_CORE_API TmxPoint : public TmxObject {
453  };
454 
459  struct GF_CORE_API TmxObjectLayer : public TmxLayer {
462  std::vector<std::unique_ptr<TmxObject>> objects;
463 
464  void accept(const TmxLayers& map, TmxVisitor& visitor) const override;
465  };
466 
471  struct GF_CORE_API TmxImage {
472  std::string format;
476  };
477 
482  struct GF_CORE_API TmxImageLayer : public TmxLayer {
483  std::unique_ptr<TmxImage> image;
484 
485  void accept(const TmxLayers& map, TmxVisitor& visitor) const override;
486  };
487 
488 
493  struct GF_CORE_API TmxGroupLayer : public TmxLayer {
494  std::vector<std::unique_ptr<TmxLayer>> layers;
495 
496  void accept(const TmxLayers& map, TmxVisitor& visitor) const override;
497  };
498 
505  struct GF_CORE_API TmxFrame {
506  int tileId;
508  };
509 
514  struct GF_CORE_API TmxAnimation {
515  std::vector<TmxFrame> frames;
516  };
517 
524  struct GF_CORE_API TmxTerrain {
526  std::string name;
527  int tile;
528  };
529 
536  struct GF_CORE_API TmxTile {
538  int id;
539  std::string type;
540  std::array<int, 4> terrain;
542 
543  std::unique_ptr<TmxImage> image;
544  std::unique_ptr<TmxObjectLayer> objects;
545  std::unique_ptr<TmxAnimation> animation;
546  };
547 
552  struct GF_CORE_API TmxTileset {
554 
555  int firstGid;
556  std::string name;
558  int spacing;
559  int margin;
560  int tileCount;
562 
564 
565  std::unique_ptr<TmxImage> image;
566  std::vector<TmxTerrain> terrains;
567  std::vector<TmxTile> tiles;
568 
569  public:
576  const TmxTile *getTile(int id) const noexcept;
577 
585  RectI getSubTexture(int id, Vector2i size) const noexcept;
586  };
587 
592  struct GF_CORE_API TmxLayers {
594 
595  std::string version;
596  std::string tiledVersion;
599 
600  bool infinite;
603 
607 
609 
611 
612  std::vector<TmxTileset> tilesets;
613  std::vector<std::unique_ptr<TmxLayer>> layers;
614 
615  public:
622  const TmxTileset *getTileSetFromGID(int gid) const noexcept;
623 
629  void visitLayers(TmxVisitor& visitor) const;
630 
636  bool loadFromFile(const Path& filename);
637  };
638 
639 #ifndef DOXYGEN_SHOULD_SKIP_THIS
640 }
641 #endif
642 }
643 
644 #endif // GF_TMX_H
MapCellAxis mapCellAxis
The stagger axis for hexagonal map.
Definition: Tmx.h:605
double opacity
The opacity of the layer.
Definition: Tmx.h:271
VAlign valign
The vertical alignment of the text.
Definition: Tmx.h:445
Vector2i tileSize
The size of a tile in the tileset.
Definition: Tmx.h:557
Flags< Flip > flip
Definition: Tmx.h:385
A layer with an image.
Definition: Tmx.h:482
MapCellIndex mapCellIndex
The stagger index for hexagonal map.
Definition: Tmx.h:606
Vector2i size
Definition: Tmx.h:291
bool bold
Is the text in bold?
Definition: Tmx.h:419
int spacing
The spacing between tiles (in pixels)
Definition: Tmx.h:558
int gid
The global id of the tile.
Definition: Tmx.h:281
A visitor for layers in the visitor pattern.
Definition: Tmx.h:204
TmxProperties properties
The properties of the terrain.
Definition: Tmx.h:525
bool wrap
The wrap mode.
Definition: Tmx.h:417
Left alignement.
std::string text
The text of the object.
Definition: Tmx.h:413
std::vector< TmxTerrain > terrains
The terrains of the tileset.
Definition: Tmx.h:566
A rectangle object.
Definition: Tmx.h:367
std::string name
The name of the object.
Definition: Tmx.h:356
A chunk in a tile layer (for infinite maps)
Definition: Tmx.h:289
std::vector< Vector2f > points
The points of the polygon.
Definition: Tmx.h:405
std::vector< TmxFrame > frames
The frames of the animation.
Definition: Tmx.h:515
Color4u backgroundColor
The background color.
Definition: Tmx.h:608
std::vector< std::unique_ptr< TmxObject > > objects
The objects of the layer.
Definition: Tmx.h:462
std::unique_ptr< TmxImage > image
The image of the layer.
Definition: Tmx.h:483
An ellipse object.
Definition: Tmx.h:375
Vector2i mapSize
The size of the map.
Definition: Tmx.h:601
TmxDrawOrder drawOrder
The draw order of the objects.
Definition: Tmx.h:461
double rotation
The rotation of the object.
Definition: Tmx.h:359
TileOrientation
The orientation of the tile.
Definition: TileTypes.h:37
std::vector< Vector2f > points
The points of the polyline.
Definition: Tmx.h:395
A layer with objects.
Definition: Tmx.h:459
An ellipse object.
Definition: Tmx.h:343
A layer in the whole map.
Definition: Tmx.h:253
Path source
The path to the image.
Definition: Tmx.h:473
std::string tiledVersion
The tiled version of the map.
Definition: Tmx.h:596
std::string name
The name of the tileset.
Definition: Tmx.h:556
A tile animation.
Definition: Tmx.h:514
A TMX map.
Definition: Tmx.h:592
bool kerning
Is the text using kerning?
Definition: Tmx.h:423
TmxRenderOrder
the render order of the tiles.
Definition: Tmx.h:50
A tile object.
Definition: Tmx.h:346
std::vector< TmxTileset > tilesets
The tilesets used in the map.
Definition: Tmx.h:612
Represents a time value.
Definition: Time.h:65
int sizeInPixels
The size of the text in pixel.
Definition: Tmx.h:416
std::unique_ptr< TmxAnimation > animation
The animation data of the tile.
Definition: Tmx.h:545
std::vector< TmxTile > tiles
The tiles of the tileset.
Definition: Tmx.h:567
int id
The id of the object.
Definition: Tmx.h:355
int probability
The probability of the tile.
Definition: Tmx.h:541
Vector2i position
Definition: Tmx.h:290
std::string fontFamily
The font family.
Definition: Tmx.h:415
Color4u transparent
The transparent color.
Definition: Tmx.h:474
Vector2i offset
The offset of the layer.
Definition: Tmx.h:273
int tileCount
The number of tiles.
Definition: Tmx.h:560
A text object.
Definition: Tmx.h:412
HAlign halign
The horizontal alignment of the text.
Definition: Tmx.h:434
std::filesystem::path Path
A path in the filesystem.
Definition: Path.h:40
std::string type
The type of the object.
Definition: Tmx.h:357
std::vector< std::unique_ptr< TmxLayer > > layers
The layers of the map.
Definition: Tmx.h:613
std::string name
The name of the terrain.
Definition: Tmx.h:526
std::string version
The version of the map.
Definition: Tmx.h:595
std::vector< std::unique_ptr< TmxLayer > > layers
The other layers.
Definition: Tmx.h:494
TmxRenderOrder renderOrder
The render order of the map.
Definition: Tmx.h:598
Kind
The kind of object.
Definition: Tmx.h:341
Centered alignment.
int tileId
Definition: Tmx.h:506
A point object.
Definition: Tmx.h:452
int nextObjectId
The next object id.
Definition: Tmx.h:610
The namespace for gf classes.
Definition: Action.h:35
A polyline object.
Definition: Tmx.h:394
TmxProperties properties
The properties of the tile.
Definition: Tmx.h:537
std::unique_ptr< TmxObjectLayer > objects
The objects in the tile.
Definition: Tmx.h:544
int firstGid
The first global id of the tileset.
Definition: Tmx.h:555
A frame in a tile animation.
Definition: Tmx.h:505
Vector2i offset
The offset of the tileset.
Definition: Tmx.h:563
TmxProperties properties
The properties of the tileset.
Definition: Tmx.h:553
TmxProperties properties
The properties of the map.
Definition: Tmx.h:593
Kind kind
The kind of the object.
Definition: Tmx.h:351
std::string type
The type of the tile.
Definition: Tmx.h:539
int gid
Definition: Tmx.h:384
A 4D vector.
Definition: Vector.h:852
Color4u color
The color of the text.
Definition: Tmx.h:418
int margin
The margin around tiles (in pixels)
Definition: Tmx.h:559
std::array< int, 4 > terrain
The terrain if the corners (top-left, top-right, bottom-left, bottom-right)
Definition: Tmx.h:540
A description of a kind of terrain on the map.
Definition: Tmx.h:524
A rectangle object.
Definition: Tmx.h:342
The properties for TMX entities.
Definition: Tmx.h:62
bool visible
The visibility of the object.
Definition: Tmx.h:360
VAlign
A vertical alignment.
Definition: Tmx.h:439
A rectangular part of a tileset.
Definition: Tmx.h:536
MapCellIndex
Map cell index in a map celled or hexagonal map.
Definition: MapCell.h:35
std::string format
The format of the image.
Definition: Tmx.h:472
int id
The local id of the tile.
Definition: Tmx.h:538
HAlign
A horizontal alignment.
Definition: Tmx.h:428
Color4u color
The color of the objects.
Definition: Tmx.h:460
Right alignement.
TileOrientation orientation
The orientation of the map.
Definition: Tmx.h:597
A polygon object.
Definition: Tmx.h:345
A geometrical object.
Definition: Tmx.h:332
A cell in a tile layer.
Definition: Tmx.h:280
std::unique_ptr< TmxImage > image
The image of this tile.
Definition: Tmx.h:543
A text object.
Definition: Tmx.h:347
Vector2f size
The size of the rectangle.
Definition: Tmx.h:368
MapCellAxis
Map cell axis in a map celled.
Definition: MapCell.h:46
TmxProperties properties
The properties of the object.
Definition: Tmx.h:353
A set of tiles in a single file (image or TSX file)
Definition: Tmx.h:552
No alignement.
int hexSideLength
The length of the side for hexagonal map.
Definition: Tmx.h:604
An image put in the map identified by its global id.
Definition: Tmx.h:383
int columnCount
The number of columns.
Definition: Tmx.h:561
Vector2f size
The size of the ellipse.
Definition: Tmx.h:376
TmxDrawOrder
The draw order of the objects.
Definition: Tmx.h:310
bool italic
Is the text in italic?
Definition: Tmx.h:420
A layer with tiles in cells.
Definition: Tmx.h:299
std::unique_ptr< TmxImage > image
The image of the tileset.
Definition: Tmx.h:565
std::vector< TmxCell > cells
The cells of the layer.
Definition: Tmx.h:300
A polyline object.
Definition: Tmx.h:344
A layer with other layers.
Definition: Tmx.h:493
bool strikeout
Is the text striked out?
Definition: Tmx.h:422
Vector2i tileSize
The size of the tiles.
Definition: Tmx.h:602
std::vector< TmxCell > cells
Definition: Tmx.h:292
Time duration
Definition: Tmx.h:507
std::string name
The name of the layer.
Definition: Tmx.h:270
bool infinite
Is the map infinite?
Definition: Tmx.h:600
A point object.
Definition: Tmx.h:348
A polygon object.
Definition: Tmx.h:404
int tile
The representing tile for the terrain.
Definition: Tmx.h:527
bool visible
The visibility of the layer.
Definition: Tmx.h:272
std::vector< TmxChunk > chunks
The chunks of the layer.
Definition: Tmx.h:301
TmxProperties properties
The properties of the layer.
Definition: Tmx.h:269
bool underline
Is the text underlined?
Definition: Tmx.h:421
Vector2f position
The position of the object.
Definition: Tmx.h:358
Vector2i size
The size of the image.
Definition: Tmx.h:475
A reference to an image.
Definition: Tmx.h:471