Gamedev Framework (gf) 0.22.0
A C++17 framework for 2D games
TileLayer.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_TILE_LAYER_H
22#define GF_TILE_LAYER_H
23
24#include <memory>
25#include <vector>
26
27#include "Array2D.h"
28#include "Cells.h"
29#include "CellTypes.h"
30#include "Flags.h"
31#include "GraphicsApi.h"
32#include "Tileset.h"
33#include "Transformable.h"
34#include "VertexArray.h"
35#include "VertexBuffer.h"
36
37namespace gf {
38#ifndef DOXYGEN_SHOULD_SKIP_THIS
39inline namespace v1 {
40#endif
41
42 class Texture;
43
58 class GF_GRAPHICS_API TileLayer : public Transformable {
59 public:
63 static constexpr int NoTile = -1;
64
69
76 static TileLayer createOrthogonal(Vector2i layerSize, Vector2i tileSize);
77
86 static TileLayer createStaggered(Vector2i layerSize, Vector2i tileSize, CellAxis axis, CellIndex index);
87
97 static TileLayer createHexagonal(Vector2i layerSize, Vector2i tileSize, int sideLength, CellAxis axis, CellIndex index);
98
105 return m_tiles.getSize();
106 }
107
118 std::size_t createTilesetId();
119
126 Tileset& getTileset(std::size_t id);
127
134 const Tileset& getTileset(std::size_t id) const;
135
136
153 void setTile(Vector2i position, std::size_t tileset, int tile, Flags<Flip> flip = None);
154
162 int getTile(Vector2i position) const;
163
171 Flags<Flip> getFlip(Vector2i position) const;
172
180 std::size_t getTileTileset(Vector2i position) const;
181
185 void clear();
186
201
212 void setAnchor(Anchor anchor);
213
223
224 virtual void draw(RenderTarget& target, const RenderStates& states) override;
225
226 private:
227 TileLayer(Vector2i layerSize, CellOrientation orientation, std::unique_ptr<Cells> properties);
228
229 private:
230 struct Cell {
231 std::size_t tileset = std::size_t(-1);
232 int tile = NoTile;
233 Flags<Flip> flip = gf::None;
234 };
235
236 struct Sheet {
237 Tileset tileset;
238 VertexArray vertices;
239 };
240
241 private:
242 void fillVertexArray(std::vector<Sheet>& sheets, RectI rect) const;
243 void updateGeometry();
244 RectI computeOffsets() const;
245
246 private:
247 CellOrientation m_orientation;
248 std::unique_ptr<Cells> m_properties;
249
250 Vector2i m_layerSize;
251
252 std::vector<Sheet> m_sheets;
253 RectI m_rect;
254
255 Array2D<Cell> m_tiles;
256 };
257
258#ifndef DOXYGEN_SHOULD_SKIP_THIS
259}
260#endif
261}
262
263#endif // GF_TILE_LAYER_H
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:102
A tile layer.
Definition: TileLayer.h:58
std::size_t createTilesetId()
Create a tileset id.
Tileset & getTileset(std::size_t id)
Get a tileset with a tileset id.
static TileLayer createOrthogonal(Vector2i layerSize, Vector2i tileSize)
Create an orthogonal tile layer.
const Tileset & getTileset(std::size_t id) const
Get a tileset with a tileset id.
Vector2i getMapSize() const
Get the size of the layer.
Definition: TileLayer.h:104
void setAnchor(Anchor anchor)
Set the anchor origin of the entity.
void setTile(Vector2i position, std::size_t tileset, int tile, Flags< Flip > flip=None)
Set a tile.
Flags< Flip > getFlip(Vector2i position) const
Get the flip properties of a tile.
virtual void draw(RenderTarget &target, const RenderStates &states) override
Draw the object to a render target.
RectF getLocalBounds() const
Get the local bounding rectangle of the layer.
VertexBuffer commitGeometry() const
Create a buffer with the current geometry.
TileLayer()
Constructor.
static TileLayer createHexagonal(Vector2i layerSize, Vector2i tileSize, int sideLength, CellAxis axis, CellIndex index)
Create a hexagonal tile layer.
static TileLayer createStaggered(Vector2i layerSize, Vector2i tileSize, CellAxis axis, CellIndex index)
Create a staggered tile layer.
std::size_t getTileTileset(Vector2i position) const
Get the tileset property of a tile.
void clear()
Remove all the tiles.
int getTile(Vector2i position) const
Get a tile.
A tileset.
Definition: Tileset.h:48
Decomposed transform defined by a position, a rotation and a scale.
Definition: Transformable.h:95
A set of primitives.
Definition: VertexArray.h:65
Data in the graphics memory.
Definition: VertexBuffer.h:81
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
GF_CORE_API Orientation orientation(float angle)
Get an orientation from an angle.
Rect< int > RectI
A int rectangle.
Definition: Rect.h:524
Vector< int, 2 > Vector2i
A int vector with 2 components.
Definition: Vector.h:1165
constexpr NoneType None
Constant to represent "none".
Definition: Types.h:45
Anchor
An anchor of a box.
Definition: Anchor.h:38
@ Texture
A GPU texture.
The namespace for gf classes.
Define the states used for drawing to a RenderTarget.
Definition: RenderStates.h:82