Gamedev Framework (gf)  0.17.0
A C++14 framework for 2D games
Grid.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2019 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 "Hexagon.h"
25 #include "Portability.h"
26 #include "Transformable.h"
27 #include "Vector.h"
28 #include "VertexArray.h"
29 #include "VertexBuffer.h"
30 
31 namespace gf {
32 #ifndef DOXYGEN_SHOULD_SKIP_THIS
33 inline namespace v1 {
34 #endif
35 
40  class GF_API SquareGrid : public gf::Transformable {
41  public:
50  SquareGrid(Vector2i gridSize, Vector2f cellSize, const Color4f& color, float lineWidth = 1.0f);
51 
57  void setGridSize(Vector2i gridSize);
58 
64  Vector2i getGridSize() const noexcept {
65  return m_gridSize;
66  }
67 
73  void setCellSize(Vector2f cellSize);
74 
80  Vector2f getCellSize() const noexcept {
81  return m_cellSize;
82  }
83 
89  void setColor(const Color4f& color);
90 
96  const Color4f& getColor() const noexcept {
97  return m_color;
98  }
99 
105  void setLineWidth(float lineWidth) noexcept {
106  m_lineWidth = lineWidth;
107  }
108 
114  float getLineWidth() const noexcept {
115  return m_lineWidth;
116  }
117 
129  RectF getLocalBounds() const;
130 
141  void setAnchor(Anchor anchor);
142 
151  VertexBuffer commitGeometry() const;
152 
153  virtual void draw(RenderTarget& target, const RenderStates& states) override;
154 
155  private:
156  void updateGeometry();
157 
158  private:
159  Vector2i m_gridSize;
160  Vector2f m_cellSize;
161  Color4f m_color;
162  float m_lineWidth;
163  VertexArray m_vertices;
164  };
165 
170  class GF_API HexagonGrid : public gf::Transformable {
171  public:
184  HexagonGrid(MapCellAxis axis, MapCellIndex index, Vector2i gridSize, float radius, const Color4f& color, float lineWidth = 1.0f);
185 
191  void setGridSize(Vector2i gridSize);
192 
198  Vector2i getGridSize() const noexcept {
199  return m_gridSize;
200  }
201 
207  void setRadius(float radius);
208 
214  float getRadius() const noexcept {
215  return m_radius;
216  }
217 
223  Vector2f getHexagonSize() const noexcept {
224  return m_helper.getHexagonSize(m_radius);
225  }
226 
232  void setColor(const Color4f& color);
233 
239  const Color4f& getColor() const noexcept {
240  return m_color;
241  }
242 
248  void setLineWidth(float lineWidth) noexcept {
249  m_lineWidth = lineWidth;
250  }
251 
257  float getLineWidth() const noexcept {
258  return m_lineWidth;
259  }
260 
272  RectF getLocalBounds() const;
273 
284  void setAnchor(Anchor anchor);
285 
294  VertexBuffer commitGeometry() const;
295 
296  virtual void draw(RenderTarget& target, const RenderStates& states) override;
297 
298  private:
299  void updateGeometry();
300 
301  private:
302  Vector2i m_gridSize;
303  float m_radius;
304  HexagonHelper m_helper;
305  Color4f m_color;
306  float m_lineWidth;
307  VertexArray m_vertices;
308  };
309 
310 #ifndef DOXYGEN_SHOULD_SKIP_THIS
311 }
312 #endif
313 }
314 
315 #endif // GF_GRID_H
Decomposed transform defined by a position, a rotation and a scale.
Definition: Transformable.h:95
Vector2i getGridSize() const noexcept
Get the grid size.
Definition: Grid.h:64
A set of primitives.
Definition: VertexArray.h:65
Vector2f getCellSize() const noexcept
Get the cell size.
Definition: Grid.h:80
void setLineWidth(float lineWidth) noexcept
Set the width of the grid frame.
Definition: Grid.h:248
float getLineWidth() const noexcept
Get the width of the grid frame.
Definition: Grid.h:114
void setLineWidth(float lineWidth) noexcept
Set the width of the grid frame.
Definition: Grid.h:105
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:90
Define the states used for drawing to a RenderTarget.
Definition: RenderStates.h:82
Data in the graphics memory.
Definition: VertexBuffer.h:77
float getRadius() const noexcept
Get the radius of hexagon.
Definition: Grid.h:214
The namespace for gf classes.
Definition: Action.h:35
A square grid.
Definition: Grid.h:40
MapCellIndex
Map cell index in a map celled or hexagonal map.
Definition: MapCell.h:35
A 4D vector.
Definition: Vector.h:852
A helper for computing coordinates in a hexagonal map.
Definition: Hexagon.h:40
float getLineWidth() const noexcept
Get the width of the grid frame.
Definition: Grid.h:257
const Color4f & getColor() const noexcept
Get the color of the grid frame.
Definition: Grid.h:96
Vector2i getGridSize() const noexcept
Get the grid size.
Definition: Grid.h:198
Anchor
An anchor of a box.
Definition: Anchor.h:38
A hexagonal grid.
Definition: Grid.h:170
MapCellAxis
Map cell axis in a map celled.
Definition: MapCell.h:46
Vector2f getHexagonSize() const noexcept
Get the hexagon size.
Definition: Grid.h:223
const Color4f & getColor() const noexcept
Get the color of the grid frame.
Definition: Grid.h:239