Yet Another Game Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
spritebatch.h
Go to the documentation of this file.
1 
10 #pragma once
11 
12 #include "../data/vertex.h"
13 
14 #include <glad/glad.h>
15 #include <glm/glm.hpp>
16 
17 #include <vector>
18 
19 namespace yage
20 {
21 
22 namespace details
23 {
24 
25 struct RenderBatch {
26  GLint offset;
27  GLsizei num_vertices;
28  GLuint texture;
29 
30  RenderBatch(GLint offset_i, GLsizei num_vertices_i, GLuint texture_i)
31  : offset(offset_i), num_vertices(num_vertices_i), texture(texture_i)
32  {
33  }
34 };
35 
38 struct Glyph {
39  GLuint texture;
40  float depth;
45 
46  Glyph(GLuint texture_i, float depth_i, const Vertex &top_left_i,
47  const Vertex &top_right_i, const Vertex &bottom_right_i,
48  const Vertex &bottom_left_i)
49  : texture(texture_i), depth(depth_i), top_left(top_left_i),
50  top_right(top_right_i), bottom_right(bottom_right_i),
51  bottom_left(bottom_left_i)
52  {
53  }
54 };
55 
56 } // namespace details
57 
59 {
60 public:
61  static const int NUM_VERTICES = 6;
62 
63 private:
64  GLuint vao_;
65  GLuint vbo_;
66  std::vector<details::Glyph> glyphs_;
67  std::vector<details::Glyph *> glyph_ptrs_;
68  std::vector<details::RenderBatch> render_batches_;
69 
70 public:
71  SpriteBatch();
72  SpriteBatch(const SpriteBatch &) = delete;
73  SpriteBatch(SpriteBatch &&) = delete;
74  ~SpriteBatch();
75 
76  SpriteBatch &operator=(const SpriteBatch &) = delete;
77  SpriteBatch &operator=(SpriteBatch &&) = delete;
78 
79  // initialize vaos and vbos
80  void begin();
81  void end();
82 
83  // adds a sprite to the sprite batch to be rendered later
84  void draw(const glm::vec4 &destination_rect, const glm::vec4 &uv_rect,
85  GLuint texture, const Colour &colour, float depth);
86  // render the batch
87  void render();
88 
89 private:
90  void createRenderBatches();
91  void sortGlyphs();
92 };
93 
94 } // namespace yage
void draw(const glm::vec4 &destination_rect, const glm::vec4 &uv_rect, GLuint texture, const Colour &colour, float depth)
Definition: spritebatch.cpp:76
GLuint texture
Definition: spritebatch.h:28
void end()
Definition: spritebatch.cpp:70
Definition: vertex.h:49
Glyph(GLuint texture_i, float depth_i, const Vertex &top_left_i, const Vertex &top_right_i, const Vertex &bottom_right_i, const Vertex &bottom_left_i)
Definition: spritebatch.h:46
Definition: spritebatch.h:58
GLuint texture
Definition: spritebatch.h:39
void begin()
Definition: spritebatch.cpp:63
Vertex bottom_left
Definition: spritebatch.h:44
void render()
Definition: spritebatch.cpp:106
Vertex bottom_right
Definition: spritebatch.h:43
SpriteBatch & operator=(const SpriteBatch &)=delete
Vertex top_left
Definition: spritebatch.h:41
RenderBatch(GLint offset_i, GLsizei num_vertices_i, GLuint texture_i)
Definition: spritebatch.h:30
GLint offset
Definition: spritebatch.h:26
static const int NUM_VERTICES
Definition: spritebatch.h:61
Definition: spritebatch.h:25
SpriteBatch()
Definition: spritebatch.cpp:23
float depth
Definition: spritebatch.h:40
Definition: vertex.h:26
Vertex top_right
Definition: spritebatch.h:42
Glyph with information of the texture.
Definition: spritebatch.h:38
GLsizei num_vertices
Definition: spritebatch.h:27
~SpriteBatch()
Definition: spritebatch.cpp:53