FastEngine 0.9.3
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_tileset.hpp
1/*
2 * Copyright 2024 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 TileId = int32_t;
32
41{
42 TileId _id{0};
43 fge::RectInt _rect;
44 mutable fge::PropertyList _properties{};
45};
46
47inline bool operator<(fge::TileData const& l, int r)
48{
49 return l._id < r;
50}
51inline bool operator<(int l, fge::TileData const& r)
52{
53 return l < r._id;
54}
55inline bool operator<(fge::TileData const& l, fge::TileData const& r)
56{
57 return l._id < r._id;
58}
59
67class FGE_API TileSet
68{
69public:
70 using TileListType = std::set<fge::TileData, std::less<>>;
71
72 TileSet() = default;
73 TileSet(fge::Texture texture);
74 TileSet(fge::Texture texture, fge::Vector2i const& tileSize);
75 TileSet(fge::Texture texture, fge::Vector2i const& tileSize, fge::Vector2i const& offset);
76
80 void clearTiles();
81
87 void setName(std::string name);
93 [[nodiscard]] std::string const& getName() const;
94
100 [[nodiscard]] bool valid() const;
101
107 [[nodiscard]] fge::Texture const& getTexture() const;
116
122 [[nodiscard]] fge::Vector2i const& getTileSize() const;
130 void setTileSize(fge::Vector2i const& tileSize);
131
137 [[nodiscard]] fge::Vector2i const& getOffset() const;
143 void setOffset(fge::Vector2i const& offset);
144
150 [[nodiscard]] std::size_t getTileCount() const;
151
158 [[nodiscard]] fge::TileData const* getTile(TileId id) const;
159
166 [[nodiscard]] TileId getLocalId(fge::Vector2i const& position) const;
176 [[nodiscard]] TileId getLocalId(TileId gid) const;
183 [[nodiscard]] bool isGidContained(TileId gid) const;
191 void setFirstGid(TileId gid);
197 [[nodiscard]] TileId getFirstGid() const;
198
199 [[nodiscard]] TileListType::const_iterator begin() const;
200 [[nodiscard]] TileListType::const_iterator end() const;
201
210 void slice();
211
217 [[nodiscard]] int getColumns() const;
223 [[nodiscard]] int getRows() const;
224
231 [[nodiscard]] std::optional<fge::RectInt> getTextureRect(TileId id) const;
238 [[nodiscard]] fge::RectInt computeTextureRect(TileId id) const;
239
240 fge::TileSet& operator=(fge::Texture texture);
241
242private:
243 void setTile(fge::TileData tile);
244 void pushTile(fge::TileData tile);
245
246 std::string g_name;
247 fge::Texture g_texture;
248 fge::Vector2i g_tileSize;
249 fge::Vector2i g_offset{0, 0};
250 TileListType g_tiles;
251 TileId g_firstGid{1};
252 int g_columns{0};
253 int g_rows{0};
254};
255
256FGE_API void to_json(nlohmann::json& j, fge::TileSet const& p);
257FGE_API void from_json(nlohmann::json const& j, fge::TileSet& p);
258
259FGE_API void to_json(nlohmann::json& j, fge::TileData const& p);
260FGE_API void from_json(nlohmann::json const& j, fge::TileData& p);
261
262} // namespace fge
263
264#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:68
bool isGidContained(TileId gid) const
Check if the global id is in the tileset.
void setFirstGid(TileId 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::RectInt computeTextureRect(TileId id) const
Compute the supposed texture rectangle with a local id.
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.
int getRows() const
Return the number of rows in the texture.
void setName(std::string name)
Set the name of the TileSet.
fge::TileData const * getTile(TileId id) const
Retrieve a tile by its local id.
TileId getLocalId(fge::Vector2i const &position) const
Get the local id of a tile by its grid position.
TileId getFirstGid() const
Get the first global id 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.
TileId getLocalId(TileId gid) const
Get the local id of a tile by its global id.
std::string const & getName() const
Get the name of the TileSet.
bool valid() const
Check if the TileSet have a valid Texture.
std::size_t getTileCount() const
Get the total number of tiles in the TileSet.
std::optional< fge::RectInt > getTextureRect(TileId id) const
Get the texture rectangle of a tile by its local id.
fge::Texture const & getTexture() const
Get the texture of the TileSet.
void setOffset(fge::Vector2i const &offset)
Set the offset in pixel of the TileSet.
A tile structure that contain mostly the texture rectangle and data.
Definition C_tileset.hpp:41