Gamedev Framework (gf)  0.3.0
A C++11 framework for 2D games
VertexArray.h
1 /*
2  * Gamedev Framework (gf)
3  * Copyright (C) 2016 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  * Part of this file comes from SFML, with the same license:
22  * Copyright (C) 2007-2015 Laurent Gomila (laurent@sfml-dev.org)
23  */
24 #ifndef GF_VERTEX_ARRAY_H
25 #define GF_VERTEX_ARRAY_H
26 
27 #include <cstddef>
28 #include <vector>
29 
30 #include "Drawable.h"
31 #include "Portability.h"
32 #include "PrimitiveType.h"
33 #include "Rect.h"
34 #include "Vertex.h"
35 
36 namespace gf {
37 #ifndef DOXYGEN_SHOULD_SKIP_THIS
38 inline namespace v1 {
39 #endif
40 
41  /**
42  * @ingroup graphics
43  * @brief A set of primitives
44  *
45  * gf::VertexArray is a very simple wrapper around a dynamic
46  * array of vertices and a primitive type.
47  *
48  * It inherits gf::Drawable, but unlike other drawables it
49  * is not transformable.
50  *
51  * Example:
52  *
53  * ~~~{.cc}
54  * gf::VertexArray lines(gf::PrimitiveType::LinesStrip, 4);
55  * lines[0].position = gf::Vector2f(10, 0);
56  * lines[1].position = gf::Vector2f(20, 0);
57  * lines[2].position = gf::Vector2f(30, 5);
58  * lines[3].position = gf::Vector2f(40, 2);
59  *
60  * window.draw(lines);
61  * ~~~
62  *
63  * @sa gf::Vertex
64  */
65  class GF_API VertexArray : public Drawable {
66  public:
67  /**
68  * @brief Default constructor
69  *
70  * Creates an empty vertex array. The default primitive type is
71  * gf::PrimitiveType::Points.
72  */
74  : m_type(PrimitiveType::Points)
75  {
76 
77  }
78 
79  /**
80  * @brief Construct the vertex array with a type and an initial
81  * number of vertices
82  *
83  * @param type Type of primitives
84  * @param count Initial number of vertices in the array
85  */
86  VertexArray(PrimitiveType type, std::size_t count = 0)
87  : m_type(type)
88  , m_vertices(count)
89  {
90 
91  }
92 
93  /**
94  * @brief Return the vertex count
95  *
96  * @return Number of vertices in the array
97  */
98  std::size_t getVertexCount() const {
99  return m_vertices.size();
100  }
101 
102  /**
103  * @brief Return the vertex data
104  *
105  * @return A pointer to the vertices in the array
106  */
107  const Vertex *getVertexData() const {
108  return m_vertices.data();
109  }
110 
111  /**
112  * @brief Get a read-write access to a vertex by its index
113  *
114  * This function doesn't check `index`, it must be in range
115  * @f$ [0, n - 1]@f$ where @f$ n @f$ is the vertex count. The
116  * behavior is undefined otherwise.
117  *
118  * @param index Index of the vertex to get
119  *
120  * @return Reference to the index-th vertex
121  *
122  * @sa getVertexCount()
123  */
124  Vertex& operator[](std::size_t index) {
125  return m_vertices[index];
126  }
127 
128  /**
129  * @brief Get a read-only access to a vertex by its index
130  *
131  * This function doesn't check `index`, it must be in range
132  * @f$ [0, n - 1]@f$ where @f$ n @f$ is the vertex count. The
133  * behavior is undefined otherwise.
134  *
135  * @param index Index of the vertex to get
136  *
137  * @return Const reference to the index-th vertex
138  *
139  * @sa getVertexCount()
140  */
141  const Vertex& operator[](std::size_t index) const {
142  return m_vertices[index];
143  }
144 
145  /**
146  * @brief Check if the vertex array is empty
147  *
148  * @return True if the vertex array is empty
149  */
150  bool isEmpty() const {
151  return m_vertices.empty();
152  }
153 
154  /**
155  * @brief Clear the vertex array
156  *
157  * This function removes all the vertices from the array.
158  * It doesn't deallocate the corresponding memory, so that
159  * adding new vertices after clearing doesn't involve
160  * reallocating all the memory.
161  */
162  void clear() {
163  m_vertices.clear();
164  }
165 
166  /**
167  * @brief Resize the vertex array
168  *
169  * If `count` is greater than the current size, the previous
170  * vertices are kept and new (default-constructed) vertices are
171  * added.
172  *
173  * If `count` is less than the current size, existing vertices
174  * are removed from the array.
175  *
176  * @param count New size of the array (number of vertices)
177  */
178  void resize(std::size_t count) {
179  m_vertices.resize(count);
180  }
181 
182  /**
183  * @brief Increase the capacity of the vertex array
184  *
185  * @param capacity New capacity of the array
186  */
187  void reserve(std::size_t capacity) {
188  m_vertices.reserve(capacity);
189  }
190 
191  /**
192  * @brief Add a vertex to the array
193  *
194  * @param vertex The vertex to add
195  */
196  void append(const Vertex& vertex) {
197  m_vertices.push_back(vertex);
198  }
199 
200  /**
201  * @brief Set the type of primitives to draw
202  *
203  * This function defines how the vertices must be interpreted
204  * when it's time to draw them:
205  *
206  * - As points
207  * - As lines
208  * - As triangles
209  *
210  * The default primitive type is gf::PrimitiveType::Points.
211  *
212  * @param type Type of primitive
213  */
215  m_type = type;
216  }
217 
218  /**
219  * @brief Get the type of primitives drawn by the vertex array
220  *
221  * @return Primitive type
222  */
224  return m_type;
225  }
226 
227  /**
228  * @brief Compute the bounding rectangle of the vertex array
229  *
230  * This function returns the minimal axis-aligned rectangle
231  * that contains all the vertices of the array.
232  *
233  * @return Bounding rectangle of the vertex array
234  */
235  RectF getBounds() const;
236 
237  virtual void draw(RenderTarget& target, RenderStates states) override;
238 
239  private:
240  PrimitiveType m_type;
241  std::vector<Vertex> m_vertices;
242  };
243 
244 #ifndef DOXYGEN_SHOULD_SKIP_THIS
245 }
246 #endif
247 }
248 
249 #endif // GF_VERTEX_ARRAY_H
VertexArray(PrimitiveType type, std::size_t count=0)
Construct the vertex array with a type and an initial number of vertices.
Definition: VertexArray.h:86
A set of primitives.
Definition: VertexArray.h:65
PrimitiveType getPrimitiveType() const
Get the type of primitives drawn by the vertex array.
Definition: VertexArray.h:223
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:65
Define the states used for drawing to a RenderTarget.
Definition: RenderStates.h:82
A point associated with a color and a texture coordinate.
Definition: Vertex.h:75
PrimitiveType
Kind of primitives to render.
Definition: PrimitiveType.h:43
void reserve(std::size_t capacity)
Increase the capacity of the vertex array.
Definition: VertexArray.h:187
List of individual points.
Abstract base class for objects that can be drawn to a render window.
Definition: Drawable.h:79
Definition: Action.h:34
void resize(std::size_t count)
Resize the vertex array.
Definition: VertexArray.h:178
void append(const Vertex &vertex)
Add a vertex to the array.
Definition: VertexArray.h:196
bool isEmpty() const
Check if the vertex array is empty.
Definition: VertexArray.h:150
void setPrimitiveType(PrimitiveType type)
Set the type of primitives to draw.
Definition: VertexArray.h:214
const Vertex & operator[](std::size_t index) const
Get a read-only access to a vertex by its index.
Definition: VertexArray.h:141
Vertex & operator[](std::size_t index)
Get a read-write access to a vertex by its index.
Definition: VertexArray.h:124
std::size_t getVertexCount() const
Return the vertex count.
Definition: VertexArray.h:98
#define GF_API
Definition: Portability.h:35
void clear()
Clear the vertex array.
Definition: VertexArray.h:162
virtual void draw(RenderTarget &target, RenderStates states) override
Draw the object to a render target.
RectF getBounds() const
Compute the bounding rectangle of the vertex array.
VertexArray()
Default constructor.
Definition: VertexArray.h:73
const Vertex * getVertexData() const
Return the vertex data.
Definition: VertexArray.h:107