FastEngine 0.9.3
A multiplayer oriented 2D engine made with Vulkan.
Loading...
Searching...
No Matches
C_tilelayer.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_TILELAYER_HPP_INCLUDED
18#define _FGE_C_TILELAYER_HPP_INCLUDED
19
20#include "FastEngine/fge_extern.hpp"
21#include "FastEngine/C_matrix.hpp"
22#include "FastEngine/C_tileset.hpp"
23#include "FastEngine/graphic/C_color.hpp"
24#include "FastEngine/graphic/C_drawable.hpp"
25#include "FastEngine/graphic/C_transformable.hpp"
26#include "FastEngine/vulkan/C_vertexBuffer.hpp"
27#include "json.hpp"
28
29namespace fge
30{
31
32using TileId = int32_t;
33using TileSetList = std::vector<std::shared_ptr<fge::TileSet>>;
34
42#ifdef FGE_DEF_SERVER
43class FGE_API TileLayer : public fge::Transformable
44#else
45class FGE_API TileLayer : public fge::Transformable, public fge::Drawable
46#endif
47{
48public:
54 class FGE_API Tile
55 {
56 public:
57 Tile();
58
66 void setGid(TileId gid);
72 [[nodiscard]] TileId getGid() const;
73
79 void setPosition(fge::Vector2f const& position);
85 [[nodiscard]] fge::Vector2f const& getPosition() const;
86
92 void setColor(fge::Color const& color);
98 [[nodiscard]] fge::Color getColor() const;
99
107 void setTileSet(std::shared_ptr<fge::TileSet> tileSet);
113 [[nodiscard]] std::shared_ptr<fge::TileSet> const& getTileSet() const;
114
115 private:
116 void updatePositions();
117 void updateTexCoords();
118
119 TileId g_gid{0};
120 std::shared_ptr<fge::TileSet> g_tileSet;
121 fge::vulkan::VertexBuffer g_vertexBuffer;
122 fge::Vector2f g_position;
123
124 friend TileLayer;
125 };
126
127 TileLayer() = default;
128
129#ifndef FGE_DEF_SERVER
130 void draw(fge::RenderTarget& target, fge::RenderStates const& states) const override;
131#endif
132
136 void clear();
137
143 void setId(TileId id);
149 [[nodiscard]] TileId getId() const;
150
156 void setName(std::string name);
162 [[nodiscard]] std::string const& getName() const;
163
169 [[nodiscard]] fge::Matrix<TileLayer::Tile> const& getTiles() const;
178 void setGid(std::size_t x, std::size_t y, TileSetList const& tileSets, TileId gid);
186 void setGid(std::size_t x, std::size_t y, TileId gid);
193 void setGridSize(std::size_t x, std::size_t y);
194
200 void refreshTextures(TileSetList const& tileSets);
201
202private:
203 static std::shared_ptr<fge::TileSet> retrieveAssociatedTileSet(TileSetList const& tileSets, TileId gid);
204
205 TileId g_id{1};
206 std::string g_name;
208};
209
210FGE_API void to_json(nlohmann::json& j, fge::TileLayer const& p);
211FGE_API void from_json(nlohmann::json const& j, fge::TileLayer& p);
212
213} // namespace fge
214
215#endif // _FGE_C_TILELAYER_HPP_INCLUDED
Definition C_color.hpp:35
Definition C_drawable.hpp:36
A container to store a 2D matrix of any type.
Definition C_matrix.hpp:40
The RenderStates class contains all the information needed to render something.
Definition C_renderStates.hpp:412
Definition C_renderTarget.hpp:56
A tile that contain drawing information and its global id.
Definition C_tilelayer.hpp:55
void setTileSet(std::shared_ptr< fge::TileSet > tileSet)
Set the associated tileset pointer.
void setColor(fge::Color const &color)
Set the color of the tile.
fge::Vector2f const & getPosition() const
Get the local position of the tile.
std::shared_ptr< fge::TileSet > const & getTileSet() const
Get the associated tileset pointer.
void setGid(TileId gid)
Set the global id of the tile.
TileId getGid() const
Get the global id of the tile.
fge::Color getColor() const
Get the color of the tile.
void setPosition(fge::Vector2f const &position)
Set the local position of the tile.
A tile layer contain a matrix of global tile id and a list of TileSet.
Definition C_tilelayer.hpp:47
fge::Matrix< TileLayer::Tile > const & getTiles() const
Get the matrix of tiles.
TileId getId() const
Get the id of the layer.
void clear()
Clear the matrix of tiles.
void setGid(std::size_t x, std::size_t y, TileId gid)
Shortcut to set a global tile id.
void setId(TileId id)
Set the id of the layer (mostly for "Tiled" map editor compatibility)
std::string const & getName() const
Get the name of the layer.
void setName(std::string name)
Set the name of the layer.
void setGid(std::size_t x, std::size_t y, TileSetList const &tileSets, TileId gid)
Shortcut to set a global tile id and a new tileset.
void refreshTextures(TileSetList const &tileSets)
Refresh all tiles with a list of tilesets.
void setGridSize(std::size_t x, std::size_t y)
Set the tiles matrix size.
Definition C_transformable.hpp:34
Definition C_vertexBuffer.hpp:45