Gamedev Framework (gf)  0.6.0
A C++11 framework for 2D games
Grid.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016-2017 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 "Portability.h"
25 #include "Transformable.h"
26 #include "Vector.h"
27 #include "VertexArray.h"
28 #include "VertexBuffer.h"
29 
30 namespace gf {
31 #ifndef DOXYGEN_SHOULD_SKIP_THIS
32 inline namespace v1 {
33 #endif
34 
35  /**
36  * @ingroup graphics
37  * @brief A square grid
38  */
39  class GF_API SquareGrid : public gf::Transformable {
40  public:
41  /**
42  * @brief Constructor
43  *
44  * @param gridSize The size of the grid
45  * @param cellSize The size of a cell in the grid
46  * @param color The color of the grid frame
47  * @param lineWidth The width of the grid frame
48  */
49  SquareGrid(Vector2u gridSize, Vector2f cellSize, const Color4f& color, float lineWidth = 1.0f);
50 
51  /**
52  * @brief Set the grid size
53  *
54  * @param gridSize The new grid size
55  */
56  void setGridSize(Vector2u gridSize);
57 
58  /**
59  * @brief Get the grid size
60  *
61  * @returns The current grid size
62  */
63  Vector2u getGridSize() const noexcept {
64  return m_gridSize;
65  }
66 
67  /**
68  * @brief Set the cell size
69  *
70  * @param cellSize The new cell size
71  */
72  void setCellSize(Vector2f cellSize);
73 
74  /**
75  * @brief Get the cell size
76  *
77  * @returns The current cell size
78  */
79  Vector2f getCellSize() const noexcept {
80  return m_cellSize;
81  }
82 
83  /**
84  * @brief Set the color of the grid frame
85  *
86  * @param color The new color of the grid frame
87  */
88  void setColor(const Color4f& color);
89 
90  /**
91  * @brief Get the color of the grid frame
92  *
93  * @returns The current color of the grid frame
94  */
95  const Color4f& getColor() const noexcept {
96  return m_color;
97  }
98 
99  /**
100  * @brief Set the width of the grid frame
101  *
102  * @param lineWidth The new width of the grid frame
103  */
104  void setLineWidth(float lineWidth) noexcept {
105  m_lineWidth = lineWidth;
106  }
107 
108  /**
109  * @brief Get the width of the grid frame
110  *
111  * @returns The current width of the grid frame
112  */
113  float getLineWidth() const noexcept {
114  return m_lineWidth;
115  }
116 
117  /**
118  * @brief Get the local bounding rectangle of the entity
119  *
120  * The returned rectangle is in local coordinates, which means
121  * that it ignores the transformations (translation, rotation,
122  * scale, ...) that are applied to the entity.
123  * In other words, this function returns the bounds of the
124  * entity in the entity's coordinate system.
125  *
126  * @return Local bounding rectangle of the entity
127  */
128  RectF getLocalBounds() const;
129 
130  /**
131  * @brief Set the anchor origin of the entity
132  *
133  * Compute the origin of the entity based on the local bounds and
134  * the specified anchor. Internally, this function calls
135  * `Transformable::setOrigin()`.
136  *
137  * @param anchor The anchor of the entity
138  * @sa getLocalBounds(), Transformable::setOrigin()
139  */
140  void setAnchor(Anchor anchor);
141 
142  /**
143  * @brief Create a buffer with the current geometry
144  *
145  * The geometry is uploaded in the graphics memory so that it's faster
146  * to draw.
147  *
148  * @return A buffer with the current geometry
149  */
151 
152  virtual void draw(RenderTarget& target, RenderStates states) override;
153 
154  private:
155  void updateGeometry();
156 
157  private:
158  Vector2u m_gridSize;
159  Vector2f m_cellSize;
160  Color4f m_color;
161  float m_lineWidth;
162  VertexArray m_vertices;
163  };
164 
165 #ifndef DOXYGEN_SHOULD_SKIP_THIS
166 }
167 #endif
168 }
169 
170 #endif // GF_GRID_H
Decomposed transform defined by a position, a rotation and a scale.
Definition: Transformable.h:95
A set of primitives.
Definition: VertexArray.h:65
RectF getLocalBounds() const
Get the local bounding rectangle of the entity.
Vector2f getCellSize() const noexcept
Get the cell size.
Definition: Grid.h:79
float getLineWidth() const noexcept
Get the width of the grid frame.
Definition: Grid.h:113
void setLineWidth(float lineWidth) noexcept
Set the width of the grid frame.
Definition: Grid.h:104
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:66
Define the states used for drawing to a RenderTarget.
Definition: RenderStates.h:82
void setCellSize(Vector2f cellSize)
Set the cell size.
Data in the graphics memory.
Definition: VertexBuffer.h:70
void setAnchor(Anchor anchor)
Set the anchor origin of the entity.
VertexBuffer commitGeometry() const
Create a buffer with the current geometry.
The namespace for gf classes.
Definition: Action.h:34
A square grid.
Definition: Grid.h:39
void setColor(const Color4f &color)
Set the color of the grid frame.
virtual void draw(RenderTarget &target, RenderStates states) override
Draw the object to a render target.
const Color4f & getColor() const noexcept
Get the color of the grid frame.
Definition: Grid.h:95
Anchor
An anchor of a box.
Definition: Anchor.h:41
#define GF_API
Definition: Portability.h:35
SquareGrid(Vector2u gridSize, Vector2f cellSize, const Color4f &color, float lineWidth=1.0f)
Constructor.
Vector2u getGridSize() const noexcept
Get the grid size.
Definition: Grid.h:63
void setGridSize(Vector2u gridSize)
Set the grid size.