Aegis2D
Modular C++26 2D game engine for networked games
Loading...
Searching...
No Matches
tile_map.hpp
Go to the documentation of this file.
1
3#pragma once
4
5#include <aegis2d/render/export.hpp>
8
9#include <cstdint>
10#include <filesystem>
11#include <functional>
12#include <map>
13#include <optional>
14#include <stdexcept>
15#include <string>
16#include <string_view>
17#include <variant>
18#include <vector>
19
20namespace aegis2d::render {
21
25class AEGIS2D_RENDER_EXPORT TileMapError : public std::runtime_error {
26public:
27 using std::runtime_error::runtime_error;
28};
29
32using TileProperty = std::variant<bool, std::int64_t, double, std::string>;
33
35using TileProperties = std::map<std::string, TileProperty, std::less<>>;
36
39inline constexpr std::uint32_t flip_horizontal = 0x8000'0000U;
41inline constexpr std::uint32_t flip_vertical = 0x4000'0000U;
43inline constexpr std::uint32_t flip_diagonal = 0x2000'0000U;
44
46inline constexpr std::uint32_t tile_id_mask = 0x0FFF'FFFFU;
47
50struct AEGIS2D_RENDER_EXPORT Tileset {
52 std::uint32_t first_gid = 1;
54 std::string name{};
56 std::filesystem::path image{};
58 TextureSize image_size{};
60 TextureSize tile_size{};
62 std::uint32_t tile_count = 0;
64 int columns = 0;
66 int margin = 0;
68 int spacing = 0;
70 TileProperties properties{};
71
76 [[nodiscard]] PixelRect tile(std::uint32_t index) const;
77};
78
80struct TileObject {
82 int id = 0;
84 std::string name{};
86 std::string type{};
92 float rotation = 0.0F;
94 std::optional<std::uint32_t> gid{};
96 bool visible = true;
99};
100
102struct MapLayer {
104 std::string name{};
107 std::vector<std::uint32_t> tiles{};
109 std::vector<TileObject> objects{};
111 bool visible = true;
113 float opacity = 1.0F;
118};
119
131struct AEGIS2D_RENDER_EXPORT TileMap {
133 int width = 0;
135 int height = 0;
137 TextureSize tile_size{};
139 std::vector<Tileset> tilesets{};
141 std::vector<MapLayer> layers{};
143 TileProperties properties{};
144
150 [[nodiscard]] static TileMap load(const std::filesystem::path& path);
151
155 [[nodiscard]] const Tileset* tileset(std::uint32_t gid) const noexcept;
156
160 [[nodiscard]] const MapLayer* layer(std::string_view name) const noexcept;
161};
162
163} // namespace aegis2d::render
Thrown when a map or one of its tilesets cannot be read or is broken.
Definition tile_map.hpp:25
OpenGL renderer: sprites, text and camera (client only).
Definition bitmap_font.hpp:16
constexpr std::uint32_t tile_id_mask
Bits of a global tile id that are the id, without the flip bits.
Definition tile_map.hpp:46
constexpr std::uint32_t flip_horizontal
Bit of a global tile id set when the tile is flipped left to right.
Definition tile_map.hpp:39
aegis2d::Vec2 Vec2
2D vector of floats, in logical pixels: the engine-wide aegis2d::Vec2.
Definition renderer.hpp:17
constexpr std::uint32_t flip_diagonal
Bit of a global tile id set when the tile is flipped along its diagonal.
Definition tile_map.hpp:43
constexpr std::uint32_t flip_vertical
Bit of a global tile id set when the tile is flipped top to bottom.
Definition tile_map.hpp:41
std::variant< bool, std::int64_t, double, std::string > TileProperty
Value of a custom property: bool, int (and object, its id), float, or string (and color,...
Definition tile_map.hpp:32
std::map< std::string, TileProperty, std::less<> > TileProperties
Custom properties of a map, layer, object or tileset, by name.
Definition tile_map.hpp:35
Backend-independent renderer interface and the sprite it draws.
Frames of a sprite sheet: the rectangles of a texture that a sprite shows in turn.
Layer of tiles or of objects.
Definition tile_map.hpp:102
bool visible
Shown in Tiled.
Definition tile_map.hpp:111
float opacity
From 0 (transparent) to 1.
Definition tile_map.hpp:113
TileProperties properties
Custom properties.
Definition tile_map.hpp:117
Vec2 offset
Offset in pixels.
Definition tile_map.hpp:115
std::string name
Name given in Tiled.
Definition tile_map.hpp:104
std::vector< std::uint32_t > tiles
For a tile layer, the global id of each cell (flip bits included), row by row from the top-left,...
Definition tile_map.hpp:107
std::vector< TileObject > objects
For an object layer, its objects in order; empty for a tile layer.
Definition tile_map.hpp:109
Rectangle of a texture, in whole pixels from its top-left corner.
Definition sprite_sheet.hpp:14
Width and height of a texture, in pixels.
Definition renderer.hpp:38
Orthogonal, finite map made in Tiled, saved as JSON.
Definition tile_map.hpp:131
const Tileset * tileset(std::uint32_t gid) const noexcept
Tileset of a tile.
static TileMap load(const std::filesystem::path &path)
Reads a map and its external tilesets.
const MapLayer * layer(std::string_view name) const noexcept
First layer of a name.
Shape placed on the map: a spawn point, a trigger area, a tile placed freely.
Definition tile_map.hpp:80
Vec2 size
Size in pixels; 0 for a point.
Definition tile_map.hpp:90
Vec2 position
Top-left corner in pixels (bottom-left for a tile object, as in Tiled).
Definition tile_map.hpp:88
std::string name
Name given in Tiled.
Definition tile_map.hpp:84
float rotation
Rotation in degrees, clockwise.
Definition tile_map.hpp:92
bool visible
Shown in Tiled.
Definition tile_map.hpp:96
TileProperties properties
Custom properties.
Definition tile_map.hpp:98
std::optional< std::uint32_t > gid
Global id of the tile it shows (flip bits included), if it is a tile object.
Definition tile_map.hpp:94
std::string type
Class given in Tiled (type before Tiled 1.9).
Definition tile_map.hpp:86
Image cut into tiles; its tiles are the global ids first_gid to first_gid + tile_count - 1.
Definition tile_map.hpp:50
PixelRect tile(std::uint32_t index) const
Rectangle of a tile in the image.