Gamedev Framework (gf) 0.21.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 "CellTypes.h"
33#include "CoreApi.h"
34#include "Flags.h"
35#include "Id.h"
36#include "Path.h"
37#include "Rect.h"
38#include "Time.h"
39#include "Vector.h"
40
41namespace gf {
42#ifndef DOXYGEN_SHOULD_SKIP_THIS
43inline namespace v1 {
44#endif
49 enum class TmxRenderOrder {
50 RightDown,
51 RightUp,
52 LeftDown,
53 LeftUp,
54 };
55
61 class GF_CORE_API TmxProperties {
62 public:
66 TmxProperties() = default;
67
72
77
84 void addStringProperty(std::string name, std::string value);
85
92 void addIntProperty(std::string name, int value);
93
100 void addFloatProperty(std::string name, double value);
101
108 void addBoolProperty(std::string name, bool value);
109
116 void addColorProperty(std::string name, Color4u value);
117
124 void addFileProperty(std::string name, Path value);
125
133 std::string getStringProperty(const std::string& name, const std::string& def) const;
134
142 int getIntProperty(const std::string& name, int def) const;
143
151 double getFloatProperty(const std::string& name, double def) const;
152
160 bool getBoolProperty(const std::string& name, bool def) const;
161
169 Color4u getColorProperty(const std::string& name, const Color4u& def) const;
170
178 Path getFileProperty(const std::string& name, const Path& def) const;
179
180 private:
181 std::map<std::string, std::string> m_stringProps;
182 std::map<std::string, int> m_intProps;
183 std::map<std::string, double> m_floatProps;
184 std::map<std::string, bool> m_boolProps;
185 std::map<std::string, Color4u> m_colorProps;
186 std::map<std::string, Path> m_fileProps;
187 };
188
189 struct TmxLayers;
190
191 struct TmxTileLayer;
192 struct TmxObjectLayer;
193 struct TmxImageLayer;
194 struct TmxGroupLayer;
195
203 class GF_CORE_API TmxVisitor {
204 public:
208 virtual ~TmxVisitor();
209
216 virtual void visitTileLayer(const TmxLayers& map, const TmxTileLayer& layer);
217
224 virtual void visitObjectLayer(const TmxLayers& map, const TmxObjectLayer& layer);
225
232 virtual void visitImageLayer(const TmxLayers& map, const TmxImageLayer& layer);
233
240 virtual void visitGroupLayer(const TmxLayers& map, const TmxGroupLayer& layer);
241 };
242
252 struct GF_CORE_API TmxLayer {
256 virtual ~TmxLayer();
257
266 virtual void accept(const TmxLayers& map, TmxVisitor& visitor) const = 0;
267
269 std::string name;
270 double opacity;
271 bool visible;
273 };
274
279 struct GF_CORE_API TmxCell {
280 int gid;
282 };
283
288 struct GF_CORE_API TmxChunk {
291 std::vector<TmxCell> cells;
292 };
293
298 struct GF_CORE_API TmxTileLayer : public TmxLayer {
299 std::vector<TmxCell> cells;
300 std::vector<TmxChunk> chunks;
301
302 void accept(const TmxLayers& map, TmxVisitor& visitor) const override;
303 };
304
309 enum class TmxDrawOrder {
310 TopDown,
311 Index,
312 };
313
314
331 struct GF_CORE_API TmxObject {
335 virtual ~TmxObject();
336
340 enum Kind {
348 };
349
351
353
354 int id;
355 std::string name;
356 std::string type;
358 double rotation;
359 bool visible;
360 };
361
366 struct GF_CORE_API TmxRectangle : public TmxObject {
368 };
369
374 struct GF_CORE_API TmxEllipse : public TmxObject {
376 };
377
382 struct GF_CORE_API TmxTileObject : public TmxObject {
383 int gid;
385 };
386
393 struct GF_CORE_API TmxPolyline : public TmxObject {
394 std::vector<Vector2f> points;
395 };
396
403 struct GF_CORE_API TmxPolygon : public TmxObject {
404 std::vector<Vector2f> points;
405 };
406
411 struct GF_CORE_API TmxText : public TmxObject {
412 std::string text;
413
414 std::string fontFamily;
416 bool wrap;
418 bool bold;
419 bool italic;
422 bool kerning;
423
427 enum class HAlign {
428 Left,
429 Center,
430 Right,
431 };
432
434
438 enum class VAlign {
439 Top,
440 Center,
441 Bottom,
442 };
443
445 };
446
451 struct GF_CORE_API TmxPoint : public TmxObject {
452 };
453
458 struct GF_CORE_API TmxObjectLayer : public TmxLayer {
461 std::vector<std::unique_ptr<TmxObject>> objects;
462
463 void accept(const TmxLayers& map, TmxVisitor& visitor) const override;
464 };
465
470 struct GF_CORE_API TmxImage {
471 std::string format;
475 };
476
481 struct GF_CORE_API TmxImageLayer : public TmxLayer {
482 std::unique_ptr<TmxImage> image;
483
484 void accept(const TmxLayers& map, TmxVisitor& visitor) const override;
485 };
486
487
492 struct GF_CORE_API TmxGroupLayer : public TmxLayer {
493 std::vector<std::unique_ptr<TmxLayer>> layers;
494
495 void accept(const TmxLayers& map, TmxVisitor& visitor) const override;
496 };
497
504 struct GF_CORE_API TmxFrame {
507 };
508
513 struct GF_CORE_API TmxAnimation {
514 std::vector<TmxFrame> frames;
515 };
516
523 struct GF_CORE_API TmxTerrain {
525 std::string name;
526 int tile;
527 };
528
535 struct GF_CORE_API TmxTile {
537 int id;
538 std::string type;
539 std::array<int, 4> terrain;
541
542 std::unique_ptr<TmxImage> image;
543 std::unique_ptr<TmxObjectLayer> objects;
544 std::unique_ptr<TmxAnimation> animation;
545 };
546
551 struct GF_CORE_API TmxTileset {
553
555 std::string name;
558 int margin;
561
563
564 std::unique_ptr<TmxImage> image;
565 std::vector<TmxTerrain> terrains;
566 std::vector<TmxTile> tiles;
567
568 public:
575 const TmxTile *getTile(int id) const noexcept;
576
584 RectI getSubTexture(int id, Vector2i size) const noexcept;
585 };
586
591 struct GF_CORE_API TmxLayers {
593
594 std::string version;
595 std::string tiledVersion;
598
599 bool infinite;
602
606
608
610
611 std::vector<TmxTileset> tilesets;
612 std::vector<std::unique_ptr<TmxLayer>> layers;
613
614 public:
621 const TmxTileset *getTileSetFromGID(int gid) const noexcept;
622
628 void visitLayers(TmxVisitor& visitor) const;
629
635 bool loadFromFile(const Path& filename);
636 };
637
638#ifndef DOXYGEN_SHOULD_SKIP_THIS
639}
640#endif
641}
642
643#endif // GF_TMX_H
Represents a time value.
Definition: Time.h:65
The properties for TMX entities.
Definition: Tmx.h:61
bool getBoolProperty(const std::string &name, bool def) const
Get a boolean property.
double getFloatProperty(const std::string &name, double def) const
Get a float property.
void addColorProperty(std::string name, Color4u value)
Add a color property.
TmxProperties()=default
Default constructor.
Color4u getColorProperty(const std::string &name, const Color4u &def) const
Get a color property.
void addStringProperty(std::string name, std::string value)
Add a string property.
std::string getStringProperty(const std::string &name, const std::string &def) const
Get a string property.
Path getFileProperty(const std::string &name, const Path &def) const
Get a file property.
TmxProperties & operator=(TmxProperties &&)=default
Default move assignment.
void addFloatProperty(std::string name, double value)
Add a float property.
void addBoolProperty(std::string name, bool value)
Add a boolean property.
void addFileProperty(std::string name, Path value)
Add a file property.
void addIntProperty(std::string name, int value)
Add an integer property.
TmxProperties(TmxProperties &&)=default
Default move constructor.
int getIntProperty(const std::string &name, int def) const
Get an integer property.
A visitor for layers in the visitor pattern.
Definition: Tmx.h:203
virtual void visitTileLayer(const TmxLayers &map, const TmxTileLayer &layer)
Visit a tile layer.
virtual void visitGroupLayer(const TmxLayers &map, const TmxGroupLayer &layer)
Visit a group layer.
virtual ~TmxVisitor()
Destructor.
virtual void visitObjectLayer(const TmxLayers &map, const TmxObjectLayer &layer)
Visit an object layer.
virtual void visitImageLayer(const TmxLayers &map, const TmxImageLayer &layer)
Visit an image layer.
CellAxis
Cell axis for staggered or hexagonal maps.
Definition: CellTypes.h:48
CellIndex
Cell index for staggered or hexagonal maps.
Definition: CellTypes.h:37
CellOrientation
The orientation of the cells.
Definition: CellTypes.h:59
std::filesystem::path Path
A path in the filesystem.
Definition: Path.h:40
TmxRenderOrder
the render order of the tiles.
Definition: Tmx.h:49
TmxDrawOrder
The draw order of the objects.
Definition: Tmx.h:309
@ LeftUp
Left up order.
@ RightUp
Right up order.
@ LeftDown
Left down order.
@ RightDown
Right down order.
@ Index
Index order.
@ TopDown
Top-down order.
constexpr NoneType None
Constant to represent "none".
Definition: Types.h:45
@ Center
Centered alignment.
@ Right
Right alignement.
@ Left
Left alignement.
The namespace for gf classes.
Definition: Action.h:35
A tile animation.
Definition: Tmx.h:513
std::vector< TmxFrame > frames
The frames of the animation.
Definition: Tmx.h:514
A cell in a tile layer.
Definition: Tmx.h:279
int gid
The global id of the tile.
Definition: Tmx.h:280
A chunk in a tile layer (for infinite maps)
Definition: Tmx.h:288
Vector2i position
Definition: Tmx.h:289
Vector2i size
Definition: Tmx.h:290
std::vector< TmxCell > cells
Definition: Tmx.h:291
An ellipse object.
Definition: Tmx.h:374
Vector2f size
The size of the ellipse.
Definition: Tmx.h:375
A frame in a tile animation.
Definition: Tmx.h:504
int tileId
Definition: Tmx.h:505
Time duration
Definition: Tmx.h:506
A layer with other layers.
Definition: Tmx.h:492
std::vector< std::unique_ptr< TmxLayer > > layers
The other layers.
Definition: Tmx.h:493
void accept(const TmxLayers &map, TmxVisitor &visitor) const override
Accept function in the visitor pattern.
A reference to an image.
Definition: Tmx.h:470
Path source
The path to the image.
Definition: Tmx.h:472
Vector2i size
The size of the image.
Definition: Tmx.h:474
Color4u transparent
The transparent color.
Definition: Tmx.h:473
std::string format
The format of the image.
Definition: Tmx.h:471
A layer with an image.
Definition: Tmx.h:481
std::unique_ptr< TmxImage > image
The image of the layer.
Definition: Tmx.h:482
void accept(const TmxLayers &map, TmxVisitor &visitor) const override
Accept function in the visitor pattern.
A layer in the whole map.
Definition: Tmx.h:252
Vector2i offset
The offset of the layer.
Definition: Tmx.h:272
std::string name
The name of the layer.
Definition: Tmx.h:269
TmxProperties properties
The properties of the layer.
Definition: Tmx.h:268
bool visible
The visibility of the layer.
Definition: Tmx.h:271
virtual ~TmxLayer()
Destructor.
double opacity
The opacity of the layer.
Definition: Tmx.h:270
virtual void accept(const TmxLayers &map, TmxVisitor &visitor) const =0
Accept function in the visitor pattern.
A TMX map.
Definition: Tmx.h:591
Color4u backgroundColor
The background color.
Definition: Tmx.h:607
Vector2i tileSize
The size of the tiles.
Definition: Tmx.h:601
std::vector< TmxTileset > tilesets
The tilesets used in the map.
Definition: Tmx.h:611
int hexSideLength
The length of the side for hexagonal map.
Definition: Tmx.h:603
void visitLayers(TmxVisitor &visitor) const
Visit the layers with a visitor.
std::vector< std::unique_ptr< TmxLayer > > layers
The layers of the map.
Definition: Tmx.h:612
CellAxis cellAxis
The stagger axis for hexagonal map.
Definition: Tmx.h:604
const TmxTileset * getTileSetFromGID(int gid) const noexcept
Get the tileset corresponding to a global id.
TmxProperties properties
The properties of the map.
Definition: Tmx.h:592
Vector2i mapSize
The size of the map.
Definition: Tmx.h:600
bool infinite
Is the map infinite?
Definition: Tmx.h:599
std::string tiledVersion
The tiled version of the map.
Definition: Tmx.h:595
CellOrientation orientation
The orientation of the map.
Definition: Tmx.h:596
int nextObjectId
The next object id.
Definition: Tmx.h:609
std::string version
The version of the map.
Definition: Tmx.h:594
bool loadFromFile(const Path &filename)
Load a TMX file.
TmxRenderOrder renderOrder
The render order of the map.
Definition: Tmx.h:597
CellIndex cellIndex
The stagger index for hexagonal map.
Definition: Tmx.h:605
A geometrical object.
Definition: Tmx.h:331
bool visible
The visibility of the object.
Definition: Tmx.h:359
double rotation
The rotation of the object.
Definition: Tmx.h:358
Vector2f position
The position of the object.
Definition: Tmx.h:357
Kind
The kind of object.
Definition: Tmx.h:340
@ Rectangle
A rectangle object.
Definition: Tmx.h:341
@ Polyline
A polyline object.
Definition: Tmx.h:343
@ Tile
A tile object.
Definition: Tmx.h:345
@ Ellipse
An ellipse object.
Definition: Tmx.h:342
@ Text
A text object.
Definition: Tmx.h:346
@ Polygon
A polygon object.
Definition: Tmx.h:344
@ Point
A point object.
Definition: Tmx.h:347
std::string type
The type of the object.
Definition: Tmx.h:356
std::string name
The name of the object.
Definition: Tmx.h:355
int id
The id of the object.
Definition: Tmx.h:354
TmxProperties properties
The properties of the object.
Definition: Tmx.h:352
virtual ~TmxObject()
Destructor.
Kind kind
The kind of the object.
Definition: Tmx.h:350
A layer with objects.
Definition: Tmx.h:458
std::vector< std::unique_ptr< TmxObject > > objects
The objects of the layer.
Definition: Tmx.h:461
TmxDrawOrder drawOrder
The draw order of the objects.
Definition: Tmx.h:460
Color4u color
The color of the objects.
Definition: Tmx.h:459
void accept(const TmxLayers &map, TmxVisitor &visitor) const override
Accept function in the visitor pattern.
A point object.
Definition: Tmx.h:451
A polygon object.
Definition: Tmx.h:403
std::vector< Vector2f > points
The points of the polygon.
Definition: Tmx.h:404
A polyline object.
Definition: Tmx.h:393
std::vector< Vector2f > points
The points of the polyline.
Definition: Tmx.h:394
A rectangle object.
Definition: Tmx.h:366
Vector2f size
The size of the rectangle.
Definition: Tmx.h:367
A description of a kind of terrain on the map.
Definition: Tmx.h:523
int tile
The representing tile for the terrain.
Definition: Tmx.h:526
TmxProperties properties
The properties of the terrain.
Definition: Tmx.h:524
std::string name
The name of the terrain.
Definition: Tmx.h:525
A text object.
Definition: Tmx.h:411
HAlign
A horizontal alignment.
Definition: Tmx.h:427
HAlign halign
The horizontal alignment of the text.
Definition: Tmx.h:433
std::string text
The text of the object.
Definition: Tmx.h:412
Color4u color
The color of the text.
Definition: Tmx.h:417
bool bold
Is the text in bold?
Definition: Tmx.h:418
bool strikeout
Is the text striked out?
Definition: Tmx.h:421
VAlign valign
The vertical alignment of the text.
Definition: Tmx.h:444
bool underline
Is the text underlined?
Definition: Tmx.h:420
VAlign
A vertical alignment.
Definition: Tmx.h:438
std::string fontFamily
The font family.
Definition: Tmx.h:414
int sizeInPixels
The size of the text in pixel.
Definition: Tmx.h:415
bool italic
Is the text in italic?
Definition: Tmx.h:419
bool wrap
The wrap mode.
Definition: Tmx.h:416
bool kerning
Is the text using kerning?
Definition: Tmx.h:422
A rectangular part of a tileset.
Definition: Tmx.h:535
std::unique_ptr< TmxAnimation > animation
The animation data of the tile.
Definition: Tmx.h:544
TmxProperties properties
The properties of the tile.
Definition: Tmx.h:536
int id
The local id of the tile.
Definition: Tmx.h:537
std::string type
The type of the tile.
Definition: Tmx.h:538
std::unique_ptr< TmxObjectLayer > objects
The objects in the tile.
Definition: Tmx.h:543
std::array< int, 4 > terrain
The terrain if the corners (top-left, top-right, bottom-left, bottom-right)
Definition: Tmx.h:539
std::unique_ptr< TmxImage > image
The image of this tile.
Definition: Tmx.h:542
int probability
The probability of the tile.
Definition: Tmx.h:540
A layer with tiles in cells.
Definition: Tmx.h:298
std::vector< TmxChunk > chunks
The chunks of the layer.
Definition: Tmx.h:300
void accept(const TmxLayers &map, TmxVisitor &visitor) const override
Accept function in the visitor pattern.
std::vector< TmxCell > cells
The cells of the layer.
Definition: Tmx.h:299
An image put in the map identified by its global id.
Definition: Tmx.h:382
int gid
Definition: Tmx.h:383
Flags< Flip > flip
Definition: Tmx.h:384
A set of tiles in a single file (image or TSX file)
Definition: Tmx.h:551
Vector2i tileSize
The size of a tile in the tileset.
Definition: Tmx.h:556
int firstGid
The first global id of the tileset.
Definition: Tmx.h:554
const TmxTile * getTile(int id) const noexcept
Get the tile corresponding to an id.
int spacing
The spacing between tiles (in pixels)
Definition: Tmx.h:557
Vector2i offset
The offset of the tileset.
Definition: Tmx.h:562
std::unique_ptr< TmxImage > image
The image of the tileset.
Definition: Tmx.h:564
RectI getSubTexture(int id, Vector2i size) const noexcept
Get the rectangle of a tile corresponding to an id.
int tileCount
The number of tiles.
Definition: Tmx.h:559
std::vector< TmxTile > tiles
The tiles of the tileset.
Definition: Tmx.h:566
int columnCount
The number of columns.
Definition: Tmx.h:560
std::string name
The name of the tileset.
Definition: Tmx.h:555
TmxProperties properties
The properties of the tileset.
Definition: Tmx.h:552
int margin
The margin around tiles (in pixels)
Definition: Tmx.h:558
std::vector< TmxTerrain > terrains
The terrains of the tileset.
Definition: Tmx.h:565
A 4D vector.
Definition: Vector.h:852