]> git.localhorst.tv Git - blank.git/blob - src/graphics/SpriteMesh.hpp
glm backwards compatibility
[blank.git] / src / graphics / SpriteMesh.hpp
1 #ifndef BLANK_GRPAHICS_SPRITEMESH_HPP_
2 #define BLANK_GRPAHICS_SPRITEMESH_HPP_
3
4 #include "glm.hpp"
5 #include "VertexArray.hpp"
6
7 #include <vector>
8 #include <GL/glew.h>
9
10
11 namespace blank {
12
13 class SpriteMesh {
14
15 public:
16         using Position = glm::vec3;
17         using TexCoord = glm::vec2;
18         using Index = unsigned short;
19
20         using Positions = std::vector<Position>;
21         using TexCoords = std::vector<TexCoord>;
22         using Indices = std::vector<Index>;
23
24         enum Attribute {
25                 ATTRIB_VERTEX,
26                 ATTRIB_TEXCOORD,
27                 ATTRIB_INDEX,
28                 ATTRIB_COUNT,
29         };
30
31         struct Buffer {
32
33                 Positions vertices;
34                 TexCoords coords;
35                 Indices indices;
36
37                 void Clear() noexcept {
38                         vertices.clear();
39                         coords.clear();
40                         indices.clear();
41                 }
42
43                 void Reserve(size_t p, size_t i) {
44                         vertices.reserve(p);
45                         coords.reserve(p);
46                         indices.reserve(i);
47                 }
48
49                 void LoadRect(
50                         float w, float h,
51                         const glm::vec2 &pivot = glm::vec2(0.0f),
52                         const glm::vec2 &tex_begin = glm::vec2(0.0f),
53                         const glm::vec2 &tex_end = glm::vec2(1.0f, 1.0f)
54                 );
55
56         };
57
58         using VAO = VertexArray<ATTRIB_COUNT>;
59
60 public:
61         void Update(const Buffer &) noexcept;
62
63         bool Empty() const noexcept {
64                 return vao.Empty();
65         }
66
67         void Draw() const noexcept {
68                 vao.DrawTriangleElements();
69         }
70
71 private:
72         VAO vao;
73
74 };
75
76 }
77
78 #endif