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