Gamedev Framework (gf)  0.11.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 {
365  };
366 
368 
370 
371  int id;
372  std::string name;
373  std::string type;
375  double rotation;
376  bool visible;
377  };
378 
383  struct GF_API TmxRectangle : public TmxObject {
385  };
386 
391  struct GF_API TmxEllipse : public TmxObject {
393  };
394 
399  struct GF_API TmxTileObject : public TmxObject {
400  unsigned gid;
402  };
403 
410  struct GF_API TmxPolyline : public TmxObject {
411  std::vector<Vector2f> points;
412  };
413 
420  struct GF_API TmxPolygon : public TmxObject {
421  std::vector<Vector2f> points;
422  };
423 
428  struct GF_API TmxText : public TmxObject {
429  std::string text;
430 
431  std::string fontFamily;
432  unsigned sizeInPixels;
433  bool wrap;
435  bool bold;
436  bool italic;
437  bool underline;
438  bool strikeout;
439  bool kerning;
440 
444  enum class HAlign {
445  Left,
446  Center,
447  Right,
448  };
449 
451 
455  enum class VAlign {
456  Top,
457  Center,
458  Bottom,
459  };
460 
462  };
463 
468  struct GF_API TmxPoint : public TmxObject {
469  };
470 
475  struct GF_API TmxObjectLayer : public TmxLayer {
478  std::vector<std::unique_ptr<TmxObject>> objects;
479 
480  virtual void accept(const TmxLayers& map, TmxVisitor& visitor) const override;
481  };
482 
487  struct GF_API TmxImage {
488  std::string format;
492  };
493 
498  struct GF_API TmxImageLayer : public TmxLayer {
499  std::unique_ptr<TmxImage> image;
500 
501  virtual void accept(const TmxLayers& map, TmxVisitor& visitor) const override;
502  };
503 
504 
509  struct GF_API TmxGroupLayer : public TmxLayer {
510  std::vector<std::unique_ptr<TmxLayer>> layers;
511 
512  virtual void accept(const TmxLayers& map, TmxVisitor& visitor) const override;
513  };
514 
521  struct GF_API TmxFrame {
522  unsigned tileId;
524  };
525 
530  struct GF_API TmxAnimation {
531  std::vector<TmxFrame> frames;
532  };
533 
540  struct GF_API TmxTerrain {
542  std::string name;
543  unsigned tile;
544  };
545 
552  struct GF_API TmxTile {
554  unsigned id;
555  std::string type;
556  std::array<unsigned, 4> terrain;
557  unsigned probability;
558 
559  std::unique_ptr<TmxImage> image;
560  std::unique_ptr<TmxObjectLayer> objects;
561  std::unique_ptr<TmxAnimation> animation;
562  };
563 
568  struct GF_API TmxTileset {
570 
571  unsigned firstGid;
572  std::string name;
574  unsigned spacing;
575  unsigned margin;
576  unsigned tileCount;
577  unsigned columnCount;
578 
580 
581  std::unique_ptr<TmxImage> image;
582  std::vector<TmxTerrain> terrains;
583  std::vector<TmxTile> tiles;
584 
585  public:
592  const TmxTile *getTile(unsigned id) const noexcept;
593 
601  RectU getSubTexture(unsigned id, Vector2u size) const noexcept;
602 
603  };
604 
609  struct GF_API TmxLayers {
611 
612  std::string version;
613  std::string tiledVersion;
616 
619 
620  unsigned hexSideLength;
623 
625 
626  unsigned nextObjectId;
627 
628  std::vector<TmxTileset> tilesets;
629  std::vector<std::unique_ptr<TmxLayer>> layers;
630 
631  public:
632 
639  const TmxTileset *getTileSetFromGID(unsigned gid) const noexcept;
640 
646  void visitLayers(TmxVisitor& visitor) const;
647 
653  bool loadFromFile(const Path& filename);
654 
655  };
656 
657 #ifndef DOXYGEN_SHOULD_SKIP_THIS
658 }
659 #endif
660 }
661 
662 #endif // GF_TMX_H
Vector2u size
The size of the image.
Definition: Tmx.h:491
unsigned tileCount
The number of tiles.
Definition: Tmx.h:576
double opacity
The opacity of the layer.
Definition: Tmx.h:299
VAlign valign
The vertical alignment of the text.
Definition: Tmx.h:461
Flags< Flip > flip
Definition: Tmx.h:401
A layer with an image.
Definition: Tmx.h:498
bool bold
Is the text in bold?
Definition: Tmx.h:435
TmxStaggerIndex
Stagger index of the hexagonal map.
Definition: Tmx.h:60
unsigned columnCount
The number of columns.
Definition: Tmx.h:577
A visitor for layers in the visitor pattern.
Definition: Tmx.h:232
TmxProperties properties
The properties of the terrain.
Definition: Tmx.h:541
bool wrap
The wrap mode.
Definition: Tmx.h:433
std::string text
The text of the object.
Definition: Tmx.h:429
std::vector< TmxTerrain > terrains
The terrains of the tileset.
Definition: Tmx.h:582
No alignement.
A rectangle object.
Definition: Tmx.h:383
std::string name
The name of the object.
Definition: Tmx.h:372
std::vector< Vector2f > points
The points of the polygon.
Definition: Tmx.h:421
std::vector< TmxFrame > frames
The frames of the animation.
Definition: Tmx.h:531
unsigned gid
The global id of the tile.
Definition: Tmx.h:309
unsigned sizeInPixels
The size of the text in pixel.
Definition: Tmx.h:432
Color4u backgroundColor
The background color.
Definition: Tmx.h:624
std::vector< std::unique_ptr< TmxObject > > objects
The objects of the layer.
Definition: Tmx.h:478
TmxDrawOrder
The draw order of the objects.
Definition: Tmx.h:327
TmxStaggerAxis staggerAxis
The stagger axis for hexagonal map.
Definition: Tmx.h:621
std::unique_ptr< TmxImage > image
The image of the layer.
Definition: Tmx.h:499
An ellipse object.
Definition: Tmx.h:391
A hexagonal orientation.
TmxDrawOrder drawOrder
The draw order of the objects.
Definition: Tmx.h:477
double rotation
The rotation of the object.
Definition: Tmx.h:375
unsigned spacing
The spacing between tiles (in pixels)
Definition: Tmx.h:574
unsigned tile
The representing tile for the terrain.
Definition: Tmx.h:543
std::vector< Vector2f > points
The points of the polyline.
Definition: Tmx.h:411
A layer with objects.
Definition: Tmx.h:475
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:489
std::string tiledVersion
The tiled version of the map.
Definition: Tmx.h:613
std::string name
The name of the tileset.
Definition: Tmx.h:572
A tile animation.
Definition: Tmx.h:530
A TMX map.
Definition: Tmx.h:609
bool kerning
Is the text using kerning?
Definition: Tmx.h:439
A tile object.
Definition: Tmx.h:362
std::vector< TmxTileset > tilesets
The tilesets used in the map.
Definition: Tmx.h:628
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:561
std::vector< TmxTile > tiles
The tiles of the tileset.
Definition: Tmx.h:583
int id
The id of the object.
Definition: Tmx.h:371
std::string fontFamily
The font family.
Definition: Tmx.h:431
Color4u transparent
The transparent color.
Definition: Tmx.h:490
Vector2i offset
The offset of the layer.
Definition: Tmx.h:301
A text object.
Definition: Tmx.h:428
HAlign halign
The horizontal alignment of the text.
Definition: Tmx.h:450
std::string type
The type of the object.
Definition: Tmx.h:373
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:629
std::string name
The name of the terrain.
Definition: Tmx.h:542
std::string version
The version of the map.
Definition: Tmx.h:612
unsigned id
The local id of the tile.
Definition: Tmx.h:554
std::vector< std::unique_ptr< TmxLayer > > layers
The other layers.
Definition: Tmx.h:510
TmxRenderOrder renderOrder
The render order of the map.
Definition: Tmx.h:615
Kind
The kind of object.
Definition: Tmx.h:357
The Y button.
A point object.
Definition: Tmx.h:468
Vector2u mapSize
The size of the map.
Definition: Tmx.h:617
The namespace for gf classes.
Definition: Action.h:35
A polyline object.
Definition: Tmx.h:410
unsigned hexSideLength
The length of the side for hexagonal map.
Definition: Tmx.h:620
TmxProperties properties
The properties of the tile.
Definition: Tmx.h:553
unsigned probability
The probability of the tile.
Definition: Tmx.h:557
std::unique_ptr< TmxObjectLayer > objects
The objects in the tile.
Definition: Tmx.h:560
A frame in a tile animation.
Definition: Tmx.h:521
Vector2i offset
The offset of the tileset.
Definition: Tmx.h:579
TmxProperties properties
The properties of the tileset.
Definition: Tmx.h:569
An even stagger index.
TmxProperties properties
The properties of the map.
Definition: Tmx.h:610
Kind kind
The kind of the object.
Definition: Tmx.h:367
std::string type
The type of the tile.
Definition: Tmx.h:555
Vector2u tileSize
The size of the tileset.
Definition: Tmx.h:573
TmxOrientation orientation
The orientation of the map.
Definition: Tmx.h:614
Color4u color
The color of the text.
Definition: Tmx.h:434
A description of a kind of terrain on the map.
Definition: Tmx.h:540
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:376
VAlign
A vertical alignment.
Definition: Tmx.h:455
An isometric orientation.
The X button.
A rectangular part of a tileset.
Definition: Tmx.h:552
TmxRenderOrder
the render order of the tiles.
Definition: Tmx.h:78
std::string format
The format of the image.
Definition: Tmx.h:488
HAlign
A horizontal alignment.
Definition: Tmx.h:444
boost::filesystem::path Path
A path in the filesystem.
Definition: Path.h:44
TmxStaggerIndex staggerIndex
The stagger index for hexagonal map.
Definition: Tmx.h:622
Color4u color
The color of the objects.
Definition: Tmx.h:476
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:559
Left alignement.
A text object.
Definition: Tmx.h:363
Vector2f size
The size of the rectangle.
Definition: Tmx.h:384
unsigned margin
The margin around tiles (in pixels)
Definition: Tmx.h:575
unsigned tileId
Definition: Tmx.h:522
TmxOrientation
The orientation of the map.
Definition: Tmx.h:48
TmxProperties properties
The properties of the object.
Definition: Tmx.h:369
A set of tiles in a single file (image or TSX file)
Definition: Tmx.h:568
unsigned nextObjectId
The next object id.
Definition: Tmx.h:626
Right alignement.
unsigned firstGid
The first global id of the tileset.
Definition: Tmx.h:571
unsigned gid
Definition: Tmx.h:400
An image put in the map identified by its global id.
Definition: Tmx.h:399
Vector2f size
The size of the ellipse.
Definition: Tmx.h:392
Vector2u tileSize
The size of the tiles.
Definition: Tmx.h:618
bool italic
Is the text in italic?
Definition: Tmx.h:436
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:556
std::unique_ptr< TmxImage > image
The image of the tileset.
Definition: Tmx.h:581
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:509
bool strikeout
Is the text striked out?
Definition: Tmx.h:438
Time duration
Definition: Tmx.h:523
std::string name
The name of the layer.
Definition: Tmx.h:298
A odd stagger index.
A point object.
Definition: Tmx.h:364
A polygon object.
Definition: Tmx.h:420
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:437
Vector2f position
The position of the object.
Definition: Tmx.h:374
An orthogonal orientation.
A reference to an image.
Definition: Tmx.h:487