Gamedev Framework (gf) 1.2.0
A C++17 framework for 2D games
Tmx.h
1/*
2 * Gamedev Framework (gf)
3 * Copyright (C) 2016-2022 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 <variant>
31#include <vector>
32
33#include "CellTypes.h"
34#include "CoreApi.h"
35#include "Flags.h"
36#include "Id.h"
37#include "Path.h"
38#include "Rect.h"
39#include "Time.h"
40#include "Vector.h"
41
42namespace gf {
43#ifndef DOXYGEN_SHOULD_SKIP_THIS
44inline namespace v1 {
45#endif
50 enum class TmxRenderOrder {
51 RightDown,
52 RightUp,
53 LeftDown,
54 LeftUp,
55 };
56
62 class GF_CORE_API TmxProperties {
63 public:
70 void addStringProperty(std::string name, std::string value);
71
78 void addIntProperty(std::string name, int value);
79
86 void addFloatProperty(std::string name, double value);
87
94 void addBoolProperty(std::string name, bool value);
95
102 void addColorProperty(std::string name, Color4u value);
103
110 void addFileProperty(std::string name, Path value);
111
118 void addObjectProperty(std::string name, Id value);
119
126 void addClassProperty(std::string name, TmxProperties value);
127
135 std::string getStringProperty(const std::string& name, const std::string& def) const;
136
144 int getIntProperty(const std::string& name, int def) const;
145
153 double getFloatProperty(const std::string& name, double def) const;
154
162 bool getBoolProperty(const std::string& name, bool def) const;
163
171 Color4u getColorProperty(const std::string& name, const Color4u& def) const;
172
180 Path getFileProperty(const std::string& name, const Path& def) const;
181
189 Id getObjectProperty(const std::string& name, Id def) const;
190
198 TmxProperties getClassProperty(const std::string& name, const TmxProperties& def) const;
199
200 private:
201 using Value = std::variant<bool, int, double, std::string, Color4u, Path, Id, TmxProperties>;
202 std::map<std::string, Value> m_props;
203 };
204
205 struct TmxLayers;
206
207 struct TmxTileLayer;
208 struct TmxObjectLayer;
209 struct TmxImageLayer;
210 struct TmxGroupLayer;
211
219 class GF_CORE_API TmxVisitor {
220 public:
224 virtual ~TmxVisitor();
225
232 virtual void visitTileLayer(const TmxLayers& map, const TmxTileLayer& layer);
233
240 virtual void visitObjectLayer(const TmxLayers& map, const TmxObjectLayer& layer);
241
248 virtual void visitImageLayer(const TmxLayers& map, const TmxImageLayer& layer);
249
256 virtual void visitGroupLayer(const TmxLayers& map, const TmxGroupLayer& layer);
257 };
258
268 struct GF_CORE_API TmxLayer {
272 virtual ~TmxLayer();
273
282 virtual void accept(const TmxLayers& map, TmxVisitor& visitor) const = 0;
283
285 std::string name;
286 double opacity;
287 bool visible;
289 };
290
295 struct GF_CORE_API TmxCell {
296 uint32_t gid;
298 };
299
304 struct GF_CORE_API TmxChunk {
307 std::vector<TmxCell> cells;
308 };
309
314 struct GF_CORE_API TmxTileLayer : public TmxLayer {
315 std::vector<TmxCell> cells;
316 std::vector<TmxChunk> chunks;
317
318 void accept(const TmxLayers& map, TmxVisitor& visitor) const override;
319 };
320
325 enum class TmxDrawOrder {
326 TopDown,
327 Index,
328 };
329
330
347 struct GF_CORE_API TmxObject {
351 virtual ~TmxObject();
352
356 enum Kind {
364 };
365
367
369
370 int id;
371 std::string name;
372 std::string type;
374 double rotation;
375 bool visible;
376 };
377
382 struct GF_CORE_API TmxRectangle : public TmxObject {
384 };
385
390 struct GF_CORE_API TmxEllipse : public TmxObject {
392 };
393
398 struct GF_CORE_API TmxTileObject : public TmxObject {
399 uint32_t gid;
401 };
402
409 struct GF_CORE_API TmxPolyline : public TmxObject {
410 std::vector<Vector2f> points;
411 };
412
419 struct GF_CORE_API TmxPolygon : public TmxObject {
420 std::vector<Vector2f> points;
421 };
422
427 struct GF_CORE_API TmxText : public TmxObject {
428 std::string text;
429
430 std::string fontFamily;
432 bool wrap;
434 bool bold;
435 bool italic;
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_CORE_API TmxPoint : public TmxObject {
468 };
469
474 struct GF_CORE_API TmxObjectLayer : public TmxLayer {
475#ifdef _MSC_VER
476 // stupid MSVC!
477 TmxObjectLayer() = default;
478 TmxObjectLayer(const TmxObjectLayer&) = delete;
479 TmxObjectLayer& operator=(const TmxObjectLayer&) = delete;
480#endif
481
484 std::vector<std::unique_ptr<TmxObject>> objects;
485
486 void accept(const TmxLayers& map, TmxVisitor& visitor) const override;
487 };
488
493 struct GF_CORE_API TmxImage {
494 std::string format;
498 };
499
504 struct GF_CORE_API TmxImageLayer : public TmxLayer {
505 std::unique_ptr<TmxImage> image;
506
507 void accept(const TmxLayers& map, TmxVisitor& visitor) const override;
508 };
509
510
515 struct GF_CORE_API TmxGroupLayer : public TmxLayer {
516#ifdef _MSC_VER
517 // stupid MSVC!
518 TmxGroupLayer() = default;
519 TmxGroupLayer(const TmxGroupLayer&) = delete;
520 TmxGroupLayer& operator=(const TmxGroupLayer&) = delete;
521#endif
522
523 std::vector<std::unique_ptr<TmxLayer>> layers;
524
525 void accept(const TmxLayers& map, TmxVisitor& visitor) const override;
526 };
527
534 struct GF_CORE_API TmxFrame {
537 };
538
543 struct GF_CORE_API TmxAnimation {
544 std::vector<TmxFrame> frames;
545 };
546
553 struct GF_CORE_API TmxTile {
555 int id;
556 std::string type;
558
559 std::unique_ptr<TmxImage> image;
560 std::unique_ptr<TmxObjectLayer> objects;
561 std::unique_ptr<TmxAnimation> animation;
562 };
563
570 struct GF_CORE_API TmxWangColor {
572 std::string name;
574 int tile;
576 };
577
584 struct GF_CORE_API TmxWangTile {
585 static constexpr std::size_t Top = 0;
586 static constexpr std::size_t TopRight = 1;
587 static constexpr std::size_t Right = 2;
588 static constexpr std::size_t BottomRight = 3;
589 static constexpr std::size_t Bottom = 4;
590 static constexpr std::size_t BottomLeft = 5;
591 static constexpr std::size_t Left = 6;
592 static constexpr std::size_t TopLeft = 7;
593
594 int tileid;
595 std::array<int, 8> wangid;
596 };
597
604 struct GF_CORE_API TmxWangSet {
606 std::string name;
607 int tile;
608
609 std::vector<TmxWangColor> colors;
610 std::vector<TmxWangTile> tiles;
611 };
612
617 struct GF_CORE_API TmxTileset {
619
620 uint32_t firstGid;
621 std::string name;
624 int margin;
627
629
630 std::unique_ptr<TmxImage> image;
631 std::vector<TmxTile> tiles;
632 std::vector<TmxWangSet> wangsets;
633
640 const TmxTile *getTile(int id) const noexcept;
641
649 RectI getSubTexture(int id, Vector2i size) const noexcept;
650 };
651
656 struct GF_CORE_API TmxLayers {
657#ifdef _MSC_VER
658 // stupid MSVC!
659 TmxLayers() = default;
660 TmxLayers(const TmxLayers&) = delete;
661 TmxLayers& operator=(const TmxLayers&) = delete;
662#endif
663
665
666 std::string version;
667 std::string tiledVersion;
670
671 bool infinite;
674
678
680
682
683 std::vector<TmxTileset> tilesets;
684 std::vector<std::unique_ptr<TmxLayer>> layers;
685
686 public:
693 const TmxTileset *getTileSetFromGID(uint32_t gid) const noexcept;
694
700 void visitLayers(TmxVisitor& visitor) const;
701
707 bool loadFromFile(const Path& filename);
708 };
709
710#ifndef DOXYGEN_SHOULD_SKIP_THIS
711}
712#endif
713}
714
715#endif // GF_TMX_H
Represents a time value.
Definition: Time.h:65
The properties for TMX entities.
Definition: Tmx.h:62
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 getClassProperty(const std::string &name, const TmxProperties &def) const
Get a class property.
Color4u getColorProperty(const std::string &name, const Color4u &def) const
Get a color property.
Id getObjectProperty(const std::string &name, Id def) const
Get an object 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.
void addFloatProperty(std::string name, double value)
Add a float property.
void addBoolProperty(std::string name, bool value)
Add a boolean property.
void addClassProperty(std::string name, TmxProperties value)
Add a class property.
void addFileProperty(std::string name, Path value)
Add a file property.
void addIntProperty(std::string name, int value)
Add an integer property.
int getIntProperty(const std::string &name, int def) const
Get an integer property.
void addObjectProperty(std::string name, Id value)
Add an object property.
A visitor for layers in the visitor pattern.
Definition: Tmx.h:219
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:50
TmxDrawOrder
The draw order of the objects.
Definition: Tmx.h:325
@ LeftUp
Left up order.
@ RightUp
Right up order.
@ LeftDown
Left down order.
@ RightDown
Right down order.
@ Index
Index order.
@ TopDown
Top-down order.
uint64_t Id
An identifier.
Definition: Id.h:37
constexpr NoneType None
Constant to represent "none".
Definition: Types.h:45
@ TopRight
Top-right.
@ BottomRight
Bottom-right.
@ BottomLeft
Bottom-left.
@ TopLeft
Top-left.
@ Center
Centered alignment.
@ Right
Right alignement.
@ Left
Left alignement.
The namespace for gf classes.
A tile animation.
Definition: Tmx.h:543
std::vector< TmxFrame > frames
The frames of the animation.
Definition: Tmx.h:544
A cell in a tile layer.
Definition: Tmx.h:295
uint32_t gid
The global id of the tile.
Definition: Tmx.h:296
A chunk in a tile layer (for infinite maps)
Definition: Tmx.h:304
Vector2i position
Definition: Tmx.h:305
Vector2i size
Definition: Tmx.h:306
std::vector< TmxCell > cells
Definition: Tmx.h:307
An ellipse object.
Definition: Tmx.h:390
Vector2f size
The size of the ellipse.
Definition: Tmx.h:391
A frame in a tile animation.
Definition: Tmx.h:534
int tileId
Definition: Tmx.h:535
Time duration
Definition: Tmx.h:536
A layer with other layers.
Definition: Tmx.h:515
std::vector< std::unique_ptr< TmxLayer > > layers
The other layers.
Definition: Tmx.h:523
void accept(const TmxLayers &map, TmxVisitor &visitor) const override
Accept function in the visitor pattern.
A reference to an image.
Definition: Tmx.h:493
Path source
The path to the image.
Definition: Tmx.h:495
Vector2i size
The size of the image.
Definition: Tmx.h:497
Color4u transparent
The transparent color.
Definition: Tmx.h:496
std::string format
The format of the image.
Definition: Tmx.h:494
A layer with an image.
Definition: Tmx.h:504
std::unique_ptr< TmxImage > image
The image of the layer.
Definition: Tmx.h:505
void accept(const TmxLayers &map, TmxVisitor &visitor) const override
Accept function in the visitor pattern.
A layer in the whole map.
Definition: Tmx.h:268
Vector2i offset
The offset of the layer.
Definition: Tmx.h:288
std::string name
The name of the layer.
Definition: Tmx.h:285
TmxProperties properties
The properties of the layer.
Definition: Tmx.h:284
bool visible
The visibility of the layer.
Definition: Tmx.h:287
virtual ~TmxLayer()
Destructor.
double opacity
The opacity of the layer.
Definition: Tmx.h:286
virtual void accept(const TmxLayers &map, TmxVisitor &visitor) const =0
Accept function in the visitor pattern.
A TMX map.
Definition: Tmx.h:656
Color4u backgroundColor
The background color.
Definition: Tmx.h:679
Vector2i tileSize
The size of the tiles.
Definition: Tmx.h:673
std::vector< TmxTileset > tilesets
The tilesets used in the map.
Definition: Tmx.h:683
int hexSideLength
The length of the side for hexagonal map.
Definition: Tmx.h:675
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:684
CellAxis cellAxis
The stagger axis for hexagonal map.
Definition: Tmx.h:676
TmxProperties properties
The properties of the map.
Definition: Tmx.h:664
Vector2i mapSize
The size of the map.
Definition: Tmx.h:672
bool infinite
Is the map infinite?
Definition: Tmx.h:671
std::string tiledVersion
The tiled version of the map.
Definition: Tmx.h:667
CellOrientation orientation
The orientation of the map.
Definition: Tmx.h:668
int nextObjectId
The next object id.
Definition: Tmx.h:681
std::string version
The version of the map.
Definition: Tmx.h:666
bool loadFromFile(const Path &filename)
Load a TMX file.
TmxRenderOrder renderOrder
The render order of the map.
Definition: Tmx.h:669
const TmxTileset * getTileSetFromGID(uint32_t gid) const noexcept
Get the tileset corresponding to a global id.
CellIndex cellIndex
The stagger index for hexagonal map.
Definition: Tmx.h:677
A geometrical object.
Definition: Tmx.h:347
bool visible
The visibility of the object.
Definition: Tmx.h:375
double rotation
The rotation of the object.
Definition: Tmx.h:374
Vector2f position
The position of the object.
Definition: Tmx.h:373
Kind
The kind of object.
Definition: Tmx.h:356
@ Rectangle
A rectangle object.
Definition: Tmx.h:357
@ Polyline
A polyline object.
Definition: Tmx.h:359
@ Tile
A tile object.
Definition: Tmx.h:361
@ Ellipse
An ellipse object.
Definition: Tmx.h:358
@ Text
A text object.
Definition: Tmx.h:362
@ Polygon
A polygon object.
Definition: Tmx.h:360
@ Point
A point object.
Definition: Tmx.h:363
std::string type
The type of the object.
Definition: Tmx.h:372
std::string name
The name of the object.
Definition: Tmx.h:371
int id
The id of the object.
Definition: Tmx.h:370
TmxProperties properties
The properties of the object.
Definition: Tmx.h:368
virtual ~TmxObject()
Destructor.
Kind kind
The kind of the object.
Definition: Tmx.h:366
A layer with objects.
Definition: Tmx.h:474
std::vector< std::unique_ptr< TmxObject > > objects
The objects of the layer.
Definition: Tmx.h:484
TmxDrawOrder drawOrder
The draw order of the objects.
Definition: Tmx.h:483
Color4u color
The color of the objects.
Definition: Tmx.h:482
void accept(const TmxLayers &map, TmxVisitor &visitor) const override
Accept function in the visitor pattern.
A point object.
Definition: Tmx.h:467
A polygon object.
Definition: Tmx.h:419
std::vector< Vector2f > points
The points of the polygon.
Definition: Tmx.h:420
A polyline object.
Definition: Tmx.h:409
std::vector< Vector2f > points
The points of the polyline.
Definition: Tmx.h:410
A rectangle object.
Definition: Tmx.h:382
Vector2f size
The size of the rectangle.
Definition: Tmx.h:383
A text object.
Definition: Tmx.h:427
HAlign
A horizontal alignment.
Definition: Tmx.h:443
HAlign halign
The horizontal alignment of the text.
Definition: Tmx.h:449
std::string text
The text of the object.
Definition: Tmx.h:428
Color4u color
The color of the text.
Definition: Tmx.h:433
bool bold
Is the text in bold?
Definition: Tmx.h:434
bool strikeout
Is the text striked out?
Definition: Tmx.h:437
VAlign valign
The vertical alignment of the text.
Definition: Tmx.h:460
bool underline
Is the text underlined?
Definition: Tmx.h:436
VAlign
A vertical alignment.
Definition: Tmx.h:454
std::string fontFamily
The font family.
Definition: Tmx.h:430
int sizeInPixels
The size of the text in pixel.
Definition: Tmx.h:431
bool italic
Is the text in italic?
Definition: Tmx.h:435
bool wrap
The wrap mode.
Definition: Tmx.h:432
bool kerning
Is the text using kerning?
Definition: Tmx.h:438
A rectangular part of a tileset.
Definition: Tmx.h:553
std::unique_ptr< TmxAnimation > animation
The animation data of the tile.
Definition: Tmx.h:561
TmxProperties properties
The properties of the tile.
Definition: Tmx.h:554
int id
The local id of the tile.
Definition: Tmx.h:555
std::string type
The type of the tile.
Definition: Tmx.h:556
std::unique_ptr< TmxObjectLayer > objects
The objects in the tile.
Definition: Tmx.h:560
std::unique_ptr< TmxImage > image
The image of this tile.
Definition: Tmx.h:559
int probability
The probability of the tile.
Definition: Tmx.h:557
A layer with tiles in cells.
Definition: Tmx.h:314
std::vector< TmxChunk > chunks
The chunks of the layer.
Definition: Tmx.h:316
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:315
An image put in the map identified by its global id.
Definition: Tmx.h:398
uint32_t gid
Definition: Tmx.h:399
Flags< Flip > flip
Definition: Tmx.h:400
A set of tiles in a single file (image or TSX file)
Definition: Tmx.h:617
Vector2i tileSize
The size of a tile in the tileset.
Definition: Tmx.h:622
uint32_t firstGid
The first global id of the tileset.
Definition: Tmx.h:620
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:623
Vector2i offset
The offset of the tileset.
Definition: Tmx.h:628
std::unique_ptr< TmxImage > image
The image of the tileset.
Definition: Tmx.h:630
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:625
std::vector< TmxTile > tiles
The tiles of the tileset.
Definition: Tmx.h:631
int columnCount
The number of columns.
Definition: Tmx.h:626
std::vector< TmxWangSet > wangsets
The wang set of the tileset.
Definition: Tmx.h:632
std::string name
The name of the tileset.
Definition: Tmx.h:621
TmxProperties properties
The properties of the tileset.
Definition: Tmx.h:618
int margin
The margin around tiles (in pixels)
Definition: Tmx.h:624
A wang color.
Definition: Tmx.h:570
int probability
The probability of the tile.
Definition: Tmx.h:575
Color4u color
The color of the wang color.
Definition: Tmx.h:573
TmxProperties properties
The properties of the wang color.
Definition: Tmx.h:571
std::string name
The name of the wang color.
Definition: Tmx.h:572
int tile
The id of the tile representing the color.
Definition: Tmx.h:574
A wang set.
Definition: Tmx.h:604
std::string name
The name of the wang set.
Definition: Tmx.h:606
TmxProperties properties
The properties of the wang set.
Definition: Tmx.h:605
std::vector< TmxWangTile > tiles
Definition: Tmx.h:610
int tile
the id of the tile representing the wang set
Definition: Tmx.h:607
std::vector< TmxWangColor > colors
Definition: Tmx.h:609
A wang tile.
Definition: Tmx.h:584
std::array< int, 8 > wangid
the wang colors of the corners and edges (top, top-right, right, bottom-right, bottom,...
Definition: Tmx.h:595
int tileid
The id of the tile.
Definition: Tmx.h:594
A 4D vector.
Definition: Vector.h:852