FastEngine 0.9.4
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_tileset.hpp
1/*
2 * Copyright 2025 Guillaume Guillet
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef _FGE_C_TILESET_HPP_INCLUDED
18#define _FGE_C_TILESET_HPP_INCLUDED
19
20#include "FastEngine/fge_extern.hpp"
21#include "C_rect.hpp"
22#include "FastEngine/C_propertyList.hpp"
23#include "FastEngine/accessor/C_texture.hpp"
24#include "json.hpp"
25#include <optional>
26#include <set>
27
28namespace fge
29{
30
31using GlobalTileId = int32_t;
32using LocalTileId = int32_t;
33
42{
43 LocalTileId _id{0};
44 fge::RectInt _rect;
45 mutable std::vector<fge::RectInt> _collisionRects{};
46 mutable fge::PropertyList _properties{};
47};
48
49inline bool operator<(fge::TileData const& l, int r)
50{
51 return l._id < r;
52}
53inline bool operator<(int l, fge::TileData const& r)
54{
55 return l < r._id;
56}
57inline bool operator<(fge::TileData const& l, fge::TileData const& r)
58{
59 return l._id < r._id;
60}
61
69class FGE_API TileSet
70{
71public:
72 using TileListType = std::set<fge::TileData, std::less<>>;
73
74 TileSet() = default;
75 explicit TileSet(fge::Texture texture);
76 TileSet(fge::Texture texture, fge::Vector2i const& tileSize);
77 TileSet(fge::Texture texture, fge::Vector2i const& tileSize, fge::Vector2i const& offset);
78
82 void clearTiles();
83
89 void setName(std::string name);
95 [[nodiscard]] std::string const& getName() const;
96
102 [[nodiscard]] bool valid() const;
103
109 [[nodiscard]] fge::Texture const& getTexture() const;
118
124 [[nodiscard]] fge::Vector2i const& getTileSize() const;
132 void setTileSize(fge::Vector2i const& tileSize);
133
139 [[nodiscard]] fge::Vector2i const& getOffset() const;
145 void setOffset(fge::Vector2i const& offset);
146
152 [[nodiscard]] std::size_t getTileCount() const;
153
160 [[nodiscard]] fge::TileData const* getTile(LocalTileId id) const;
161
168 [[nodiscard]] LocalTileId getLocalId(fge::Vector2i const& position) const;
178 [[nodiscard]] LocalTileId getLocalId(GlobalTileId gid) const;
179 [[nodiscard]] GlobalTileId getGlobalId(LocalTileId id) const;
186 [[nodiscard]] bool containsGlobal(GlobalTileId gid) const;
187 [[nodiscard]] bool containsLocal(LocalTileId id) const;
195 void setFirstGid(GlobalTileId gid);
201 [[nodiscard]] GlobalTileId getFirstGid() const;
202
203 [[nodiscard]] TileListType::const_iterator begin() const;
204 [[nodiscard]] TileListType::const_iterator end() const;
205
214 void slice();
215
221 [[nodiscard]] int getColumns() const;
227 [[nodiscard]] int getRows() const;
228
235 [[nodiscard]] std::optional<fge::RectInt> getTextureRect(LocalTileId id) const;
242 [[nodiscard]] fge::RectInt computeTextureRect(LocalTileId id) const;
243
244 fge::TileSet& operator=(fge::Texture texture);
245
246private:
247 void setTile(fge::TileData tile);
248 void pushTile(fge::TileData tile);
249
250 std::string g_name;
251 fge::Texture g_texture;
252 fge::Vector2i g_tileSize;
253 fge::Vector2i g_offset{0, 0};
254 TileListType g_tiles;
255 GlobalTileId g_firstGid{1};
256 int g_columns{0};
257 int g_rows{0};
258};
259
260FGE_API void to_json(nlohmann::json& j, fge::TileSet const& p);
261FGE_API void from_json(nlohmann::json const& j, fge::TileSet& p);
262
263FGE_API void to_json(nlohmann::json& j, fge::TileData const& p);
264FGE_API void from_json(nlohmann::json const& j, fge::TileData& p);
265
266} // namespace fge
267
268#endif // _FGE_C_TILESET_HPP_INCLUDED
A class that map a string to a Property.
Definition C_propertyList.hpp:35
This class is a wrapper for the texture manager to allow easy manipulation.
Definition C_texture.hpp:36
A class that represent a set of tiles that can be used in a TileLayer.
Definition C_tileset.hpp:70
void setFirstGid(GlobalTileId gid)
Set the first global id of the tileset.
void setTileSize(fge::Vector2i const &tileSize)
Set the tile size of the TileSet.
void slice()
Slice the texture into tiles.
void clearTiles()
Clear the tiles.
fge::Vector2i const & getTileSize() const
Get the tile size of the TileSet.
fge::Vector2i const & getOffset() const
Get the offset in pixel of the TileSet.
std::optional< fge::RectInt > getTextureRect(LocalTileId id) const
Get the texture rectangle of a tile by its local id.
int getRows() const
Return the number of rows in the texture.
void setName(std::string name)
Set the name of the TileSet.
void setTexture(fge::Texture texture)
Set the texture of the TileSet.
int getColumns() const
Return the number of columns in the texture.
fge::RectInt computeTextureRect(LocalTileId id) const
Compute the supposed texture rectangle with a local id.
std::string const & getName() const
Get the name of the TileSet.
bool valid() const
Check if the TileSet have a valid Texture.
bool containsGlobal(GlobalTileId gid) const
Check if the global id is in the tileset.
std::size_t getTileCount() const
Get the total number of tiles in the TileSet.
fge::Texture const & getTexture() const
Get the texture of the TileSet.
LocalTileId getLocalId(fge::Vector2i const &position) const
Get the local id of a tile by its grid position.
fge::TileData const * getTile(LocalTileId id) const
Retrieve a tile by its local id.
void setOffset(fge::Vector2i const &offset)
Set the offset in pixel of the TileSet.
GlobalTileId getFirstGid() const
Get the first global id of the tileset.
LocalTileId getLocalId(GlobalTileId gid) const
Get the local id of a tile by its global id.
A tile structure that contain mostly the texture rectangle and data.
Definition C_tileset.hpp:42