Gamedev Framework (gf) 1.2.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
143 void setTilesetSmooth(bool smooth = true);
144
161 void setTile(Vector2i position, std::size_t tileset, int tile, Flags<Flip> flip = None);
162
170 int getTile(Vector2i position) const;
171
179 Flags<Flip> getFlip(Vector2i position) const;
180
188 std::size_t getTileTileset(Vector2i position) const;
189
193 void clear();
194
209
220 void setAnchor(Anchor anchor);
221
231
232 virtual void draw(RenderTarget& target, const RenderStates& states) override;
233
234 private:
235 TileLayer(Vector2i layerSize, CellOrientation orientation, std::unique_ptr<Cells> properties);
236
237 private:
238 struct Cell {
239 std::size_t tileset = std::size_t(-1);
240 int tile = NoTile;
241 Flags<Flip> flip = gf::None;
242 };
243
244 struct Sheet {
245 Tileset tileset;
246 VertexArray vertices;
247 };
248
249 private:
250 void fillVertexArray(std::vector<Sheet>& sheets, RectI rect) const;
251 void updateGeometry();
252 RectI computeOffsets() const;
253
254 private:
255 CellOrientation m_orientation;
256 std::unique_ptr<Cells> m_properties;
257
258 Vector2i m_layerSize;
259
260 std::vector<Sheet> m_sheets;
261 RectI m_rect;
262
263 Array2D<Cell> m_tiles;
264 };
265
266#ifndef DOXYGEN_SHOULD_SKIP_THIS
267}
268#endif
269}
270
271#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.
void setTilesetSmooth(bool smooth=true)
Enable or disable the smooth filter on the texture of tilesets.
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:509
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