Gamedev Framework (gf) 1.2.0
A C++17 framework for 2D games
VertexArray.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 * 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 "GraphicsApi.h"
32#include "PrimitiveType.h"
33#include "Rect.h"
34#include "Vertex.h"
35
36namespace gf {
37#ifndef DOXYGEN_SHOULD_SKIP_THIS
38inline namespace v1 {
39#endif
40
65 class GF_GRAPHICS_API VertexArray : public Drawable {
66 public:
74 : m_type(PrimitiveType::Points)
75 {
76
77 }
78
86 VertexArray(PrimitiveType type, std::size_t count = 0)
87 : m_type(type)
88 , m_vertices(count)
89 {
90
91 }
92
98 std::size_t getVertexCount() const {
99 return m_vertices.size();
100 }
101
107 const Vertex *getVertexData() const {
108 return m_vertices.data();
109 }
110
124 Vertex& operator[](std::size_t index) {
125 return m_vertices[index];
126 }
127
141 const Vertex& operator[](std::size_t index) const {
142 return m_vertices[index];
143 }
144
152 const Vertex *begin() const {
153 return m_vertices.data();
154 }
155
163 const Vertex *end() const {
164 return m_vertices.data() + m_vertices.size();
165 }
166
175 return m_vertices.data();
176 }
177
186 return m_vertices.data() + m_vertices.size();
187 }
188
194 bool isEmpty() const {
195 return m_vertices.empty();
196 }
197
206 void clear() {
207 m_vertices.clear();
208 }
209
222 void resize(std::size_t count) {
223 m_vertices.resize(count);
224 }
225
231 void reserve(std::size_t capacity) {
232 m_vertices.reserve(capacity);
233 }
234
240 void append(const Vertex& vertex) {
241 m_vertices.push_back(vertex);
242 }
243
249 template<typename ... V>
250 void appendAll(V&& ... vertices) {
251 (append(std::forward<V>(vertices)), ...);
252 }
253
269 m_type = type;
270 }
271
278 return m_type;
279 }
280
290
291 virtual void draw(RenderTarget& target, const RenderStates& states) override;
292
293 private:
294 PrimitiveType m_type;
295 std::vector<Vertex> m_vertices;
296 };
297
298#ifndef DOXYGEN_SHOULD_SKIP_THIS
299}
300#endif
301}
302
303#endif // GF_VERTEX_ARRAY_H
Abstract base class for objects that can be drawn to a render window.
Definition: Drawable.h:57
Base class for all render targets (window, texture, ...)
Definition: RenderTarget.h:102
A set of primitives.
Definition: VertexArray.h:65
const Vertex * end() const
Get an iterator past the last element.
Definition: VertexArray.h:163
PrimitiveType getPrimitiveType() const
Get the type of primitives drawn by the vertex array.
Definition: VertexArray.h:277
const Vertex * begin() const
Get an iterator to the first element.
Definition: VertexArray.h:152
Vertex & operator[](std::size_t index)
Get a read-write access to a vertex by its index.
Definition: VertexArray.h:124
Vertex * begin()
Get an iterator to the first element.
Definition: VertexArray.h:174
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
VertexArray()
Default constructor.
Definition: VertexArray.h:73
void appendAll(V &&... vertices)
Append a sequence of vertices.
Definition: VertexArray.h:250
Vertex * end()
Get an iterator past the last element.
Definition: VertexArray.h:185
void append(const Vertex &vertex)
Add a vertex to the array.
Definition: VertexArray.h:240
const Vertex & operator[](std::size_t index) const
Get a read-only access to a vertex by its index.
Definition: VertexArray.h:141
void setPrimitiveType(PrimitiveType type)
Set the type of primitives to draw.
Definition: VertexArray.h:268
std::size_t getVertexCount() const
Return the vertex count.
Definition: VertexArray.h:98
RectF getBounds() const
Compute the bounding rectangle of the vertex array.
void clear()
Clear the vertex array.
Definition: VertexArray.h:206
virtual void draw(RenderTarget &target, const RenderStates &states) override
Draw the object to a render target.
const Vertex * getVertexData() const
Return the vertex data.
Definition: VertexArray.h:107
bool isEmpty() const
Check if the vertex array is empty.
Definition: VertexArray.h:194
void resize(std::size_t count)
Resize the vertex array.
Definition: VertexArray.h:222
void reserve(std::size_t capacity)
Increase the capacity of the vertex array.
Definition: VertexArray.h:231
PrimitiveType
Kind of primitives to render.
Definition: PrimitiveType.h:43
@ Points
List of individual points.
The namespace for gf classes.
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