Gamedev Framework (gf) 1.2.0
A C++17 framework for 2D games
Grid.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_GRID_H
22#define GF_GRID_H
23
24#include <memory>
25
26#include "Cells.h"
27#include "GraphicsApi.h"
28#include "Transformable.h"
29#include "Vector.h"
30#include "VertexArray.h"
31#include "VertexBuffer.h"
32
33namespace gf {
34#ifndef DOXYGEN_SHOULD_SKIP_THIS
35inline namespace v1 {
36#endif
37
49 class GF_GRAPHICS_API Grid : public gf::Transformable {
50 public:
55
62 static Grid createOrthogonal(Vector2i gridSize, Vector2f cellSize);
63
72 static Grid createStaggered(Vector2i gridSize, Vector2f cellSize, CellAxis axis, CellIndex index);
73
83 static Grid createHexagonal(Vector2i gridSize, Vector2f cellSize, float sideLength, CellAxis axis, CellIndex index);
84
93 static Grid createHexagonal(Vector2i gridSize, float radius, CellAxis axis, CellIndex index);
94
100 void setGridSize(Vector2i gridSize);
101
107 Vector2i getGridSize() const noexcept {
108 return m_gridSize;
109 }
110
116 void setColor(const Color4f& color);
117
123 const Color4f& getColor() const noexcept {
124 return m_color;
125 }
126
132 void setSelectedColor(const Color4f& color);
133
139 const Color4f& getSelectedColor() const noexcept {
140 return m_selectedColor;
141 }
142
148 void setLineWidth(float lineWidth) noexcept {
149 m_lineWidth = lineWidth;
150 }
151
157 float getLineWidth() const noexcept {
158 return m_lineWidth;
159 }
160
161 void hover(Vector2f pointer);
162
163
176
187 void setAnchor(Anchor anchor);
188
198
199 void draw(RenderTarget& target, const RenderStates& states) override;
200
201 private:
202 Grid(Vector2i gridSize, std::unique_ptr<Cells> properties);
203
204 void updateGeometry();
205
206 private:
207 std::unique_ptr<Cells> m_properties;
208 Vector2i m_gridSize;
209 Color4f m_color;
210 float m_lineWidth;
211 VertexArray m_vertices;
212 Vector2i m_selected;
213 Color4f m_selectedColor;
214 VertexArray m_selectedVertices;
215 };
216
217#ifndef DOXYGEN_SHOULD_SKIP_THIS
218}
219#endif
220}
221
222#endif // GF_GRID_H
A grid of cells.
Definition: Grid.h:49
float getLineWidth() const noexcept
Get the width of the grid frame.
Definition: Grid.h:157
const Color4f & getColor() const noexcept
Get the color of the grid frame.
Definition: Grid.h:123
static Grid createHexagonal(Vector2i gridSize, float radius, CellAxis axis, CellIndex index)
Create a regular hexagonal grid.
static Grid createStaggered(Vector2i gridSize, Vector2f cellSize, CellAxis axis, CellIndex index)
Create a staggered grid.
static Grid createHexagonal(Vector2i gridSize, Vector2f cellSize, float sideLength, CellAxis axis, CellIndex index)
Create a hexagonal grid.
void hover(Vector2f pointer)
void draw(RenderTarget &target, const RenderStates &states) override
Draw the object to a render target.
Grid()
Constructor.
void setLineWidth(float lineWidth) noexcept
Set the width of the grid frame.
Definition: Grid.h:148
void setAnchor(Anchor anchor)
Set the anchor origin of the entity.
void setGridSize(Vector2i gridSize)
Set the grid size.
VertexBuffer commitGeometry() const
Create a buffer with the current geometry.
void setSelectedColor(const Color4f &color)
Set the color of the selected cell.
RectF getLocalBounds() const
Get the local bounding rectangle of the entity.
const Color4f & getSelectedColor() const noexcept
Get the color of the selected cell.
Definition: Grid.h:139
void setColor(const Color4f &color)
Set the color of the grid frame.
static Grid createOrthogonal(Vector2i gridSize, Vector2f cellSize)
Create an orthogonal grid.
Vector2i getGridSize() const noexcept
Get the grid size.
Definition: Grid.h:107
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:102
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
Anchor
An anchor of a box.
Definition: Anchor.h:38
The namespace for gf classes.
Define the states used for drawing to a RenderTarget.
Definition: RenderStates.h:82
A 4D vector.
Definition: Vector.h:852