Gamedev Framework (gf)  0.3.0
A C++11 framework for 2D games
VertexBuffer.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 #ifndef GF_VERTEX_BUFFER_H
22 #define GF_VERTEX_BUFFER_H
23 
24 #include <cstddef>
25 #include <cstdint>
26 
27 #include "Portability.h"
28 #include "PrimitiveType.h"
29 
30 namespace gf {
31 #ifndef DOXYGEN_SHOULD_SKIP_THIS
32 inline namespace v1 {
33 #endif
34 
35  struct Vertex;
36 
37  /**
38  * @ingroup graphics
39  * @brief Data in the graphics memory
40  *
41  * A vertex buffer is a buffer that resides directly in the graphics memory.
42  * The advantage is that the draw operations are faster than uploading data
43  * each time. The drawback is that the data must not change, i.e. data can
44  * be loaded only once.
45  *
46  * In gf, a vertex buffer can be used directly. But the main usage is for
47  * drawable entities that can upload the final geometry and give the
48  * corresponding vertex buffer.
49  *
50  * Usage:
51  *
52  * ~~~{.cc}
53  * gf::Texture texture;
54  *
55  * gf::Sprite sprite;
56  *
57  * sprite.setTexture(texture);
58  * sprite.setTextureRect({ 0.5f, 0.5f, 0.25f, 0.25f });
59  * gf::VertexBuffer buffer = sprite.commitGeometry();
60  *
61  * gf::BufferedGeometry geometry;
62  * geometry.setBuffer(buffer);
63  * geometry.setTexture(texture);
64  *
65  * renderer.draw(geometry);
66  * ~~~
67  *
68  * @sa gf::BufferedGeometry
69  */
71  public:
72  /**
73  * @brief Default constructor
74  */
75  VertexBuffer();
76 
77  /**
78  * @brief Destructor.
79  */
80  ~VertexBuffer();
81 
82  /**
83  * @brief Deleted copy constructor
84  */
85  VertexBuffer(const VertexBuffer&) = delete;
86 
87  /**
88  * @brief Deleted copy assignment
89  */
90  VertexBuffer& operator=(const VertexBuffer&) = delete;
91 
92  /**
93  * @brief Move constructor
94  */
95  VertexBuffer(VertexBuffer&& other);
96 
97  /**
98  * @brief Move assignment
99  */
101 
102  /**
103  * @brief Load an array of vertices
104  *
105  * @param vertices Pointer to the vertices
106  * @param count Number of vertices in the array
107  * @param type Type of primitives to draw
108  */
109  void load(const Vertex *vertices, std::size_t count, PrimitiveType type);
110 
111  /**
112  * @brief Load an array of vertices and their indices
113  *
114  * @param vertices Pointer to the vertices
115  * @param indices Pointer to the indices
116  * @param count Number of indices in the array
117  * @param type Type of primitives to draw
118  */
119  void load(const Vertex *vertices, const uint16_t *indices, std::size_t count, PrimitiveType type);
120 
121  /**
122  * @brief Check if there is an array buffer
123  *
124  * @return True if an array buffer is defined
125  */
126  bool hasArrayBuffer() const {
127  return m_vbo != 0;
128  }
129 
130  /**
131  * @brief Check if there is an element array buffer
132  *
133  * @return True if an array element buffer is defined
134  */
135  bool hasElementArrayBuffer() const {
136  return m_ebo != 0;
137  }
138 
139  /**
140  * @brief Get the count of vertices or indices
141  *
142  * This function returns the `count` parameter given in
143  * VertexBuffer::load().
144  *
145  * @return The count of vertices or indices
146  * @sa load()
147  */
148  std::size_t getCount() const {
149  return m_count;
150  }
151 
152  /**
153  * @brief Get the primitive type of the data in the buffer
154  *
155  * This function returns the `type` parameter given in
156  * VertexBuffer::load().
157  *
158  * @return The primitive type
159  * @sa load()
160  */
162  return m_type;
163  }
164 
165  /**
166  * @brief Binds a vertex buffer
167  *
168  * This function is for internal use only.
169  *
170  * @param buffer A pointer to a buffer or `nullptr`
171  */
172  static void bind(const VertexBuffer *buffer);
173 
174  private:
175  unsigned m_vbo;
176  unsigned m_ebo;
177  std::size_t m_count;
178  PrimitiveType m_type;
179  };
180 
181 #ifndef DOXYGEN_SHOULD_SKIP_THIS
182 }
183 #endif
184 }
185 
186 #endif // GF_VERTEX_BUFFER_H
bool hasElementArrayBuffer() const
Check if there is an element array buffer.
Definition: VertexBuffer.h:135
VertexBuffer & operator=(VertexBuffer &&other)
Move assignment.
A point associated with a color and a texture coordinate.
Definition: Vertex.h:75
static void bind(const VertexBuffer *buffer)
Binds a vertex buffer.
PrimitiveType
Kind of primitives to render.
Definition: PrimitiveType.h:43
Data in the graphics memory.
Definition: VertexBuffer.h:70
void load(const Vertex *vertices, std::size_t count, PrimitiveType type)
Load an array of vertices.
VertexBuffer(VertexBuffer &&other)
Move constructor.
PrimitiveType getPrimitiveType() const
Get the primitive type of the data in the buffer.
Definition: VertexBuffer.h:161
bool hasArrayBuffer() const
Check if there is an array buffer.
Definition: VertexBuffer.h:126
std::size_t getCount() const
Get the count of vertices or indices.
Definition: VertexBuffer.h:148
VertexBuffer()
Default constructor.
VertexBuffer(const VertexBuffer &)=delete
Deleted copy constructor.
Definition: Action.h:34
~VertexBuffer()
Destructor.
VertexBuffer & operator=(const VertexBuffer &)=delete
Deleted copy assignment.
#define GF_API
Definition: Portability.h:35
void load(const Vertex *vertices, const uint16_t *indices, std::size_t count, PrimitiveType type)
Load an array of vertices and their indices.