YAGE  v0.1.3.0
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 
12 #ifndef YAGE_SPRITE_BATCH_H
13 #define YAGE_SPRITE_BATCH_H
14 
15 #include "vertex.h"
16 
17 #include <glad/glad.h>
18 #include <glm/glm.hpp>
19 #include <yage/math/matrix.h>
20 
21 #include <vector>
22 
23 namespace yage
24 {
25 
26 class SpriteBatch;
27 
30 class Glyph
31 {
32 private:
33  GLuint texture_;
34  float depth_;
39 
40 public:
41  Glyph(GLuint texture, float depth, const Vertex &top_left,
42  const Vertex &top_right, const Vertex &bottom_right,
43  const Vertex &bottom_left);
44 
45  GLuint texture() const { return texture_; }
46  float depth() const { return depth_; }
47  Vertex top_left() const { return top_left_; }
48  Vertex top_right() const { return top_right_; }
49  Vertex bottom_right() const { return bottom_right_; }
50  Vertex bottom_left() const { return bottom_left_; }
51 };
52 
54 {
55  friend SpriteBatch;
56 
57 private:
58  GLsizei num_vertices_;
59  GLint offset_;
60  GLuint texture_;
61 
62 public:
63  RenderBatch(GLint offset, GLsizei num_vertices, GLuint texture);
64 
65  GLint offset() const { return offset_; }
66  GLsizei num_vertices() const { return num_vertices_; }
67  GLuint texture() const { return texture_; }
68 };
69 
71 {
72 public:
73  static const int NUM_VERTICES = 6;
74 
75 private:
76  GLuint vbo_ = 0;
77  GLuint vao_ = 0;
78  std::vector<Glyph> glyphs_;
79  std::vector<Glyph *> glyph_ptrs_;
80  std::vector<RenderBatch> render_batches_;
81 
82 public:
83  SpriteBatch();
84  SpriteBatch(const SpriteBatch &) = delete;
85  SpriteBatch(SpriteBatch &&) = delete;
86  ~SpriteBatch();
87 
88  SpriteBatch &operator=(const SpriteBatch &) = delete;
89  SpriteBatch &operator=(SpriteBatch &&) = delete;
90 
91  // initialize vaos and vbos
92  void begin();
93  void end();
94 
95  // adds a sprite to the sprite batch to be rendered later
96  void draw(const yage::Vector4f &destination_rect,
97  const yage::Vector4f &uv_rect, GLuint texture,
98  const Colour &colour, float depth);
99  // render the batch
100  void render();
101 
102 private:
103  void createVertexArray();
104  void createRenderBatches();
105  void sortGlyphs();
106 };
107 
108 } // namespace yage
109 
110 #endif
Definition: spritebatch.h:53
std::vector< Glyph * > glyph_ptrs_
Definition: spritebatch.h:79
GLuint texture_
Definition: spritebatch.h:60
Vertex bottom_left_
Definition: spritebatch.h:38
void end()
Definition: spritebatch.cpp:56
Definition: vertex.h:49
Definition: spritebatch.h:70
Vertex bottom_right() const
Definition: spritebatch.h:49
void createVertexArray()
Definition: spritebatch.cpp:112
void begin()
Definition: spritebatch.cpp:49
Vertex top_left_
Definition: spritebatch.h:35
Vertex bottom_left() const
Definition: spritebatch.h:50
void render()
Definition: spritebatch.cpp:93
SpriteBatch & operator=(const SpriteBatch &)=delete
Glyph(GLuint texture, float depth, const Vertex &top_left, const Vertex &top_right, const Vertex &bottom_right, const Vertex &bottom_left)
Definition: spritebatch.cpp:19
GLuint vbo_
Definition: spritebatch.h:76
float depth_
Definition: spritebatch.h:34
GLint offset() const
Definition: spritebatch.h:65
friend SpriteBatch
Definition: spritebatch.h:55
void createRenderBatches()
Definition: spritebatch.cpp:150
GLuint texture_
Definition: spritebatch.h:33
float depth() const
Definition: spritebatch.h:46
Vertex bottom_right_
Definition: spritebatch.h:37
static const int NUM_VERTICES
Definition: spritebatch.h:73
void draw(const yage::Vector4f &destination_rect, const yage::Vector4f &uv_rect, GLuint texture, const Colour &colour, float depth)
Definition: spritebatch.cpp:62
GLuint vao_
Definition: spritebatch.h:77
GLuint texture() const
Definition: spritebatch.h:45
std::vector< RenderBatch > render_batches_
Definition: spritebatch.h:80
std::vector< Glyph > glyphs_
Definition: spritebatch.h:78
SpriteBatch()
Definition: spritebatch.cpp:33
GLint offset_
Definition: spritebatch.h:59
Definition: vertex.h:26
GLsizei num_vertices() const
Definition: spritebatch.h:66
Vertex top_right_
Definition: spritebatch.h:36
GLuint texture() const
Definition: spritebatch.h:67
void sortGlyphs()
Definition: spritebatch.cpp:188
4D Vector class
Definition: matrix.h:376
Glyph with information of the texture.
Definition: spritebatch.h:30
GLsizei num_vertices_
Definition: spritebatch.h:58
RenderBatch(GLint offset, GLsizei num_vertices, GLuint texture)
Definition: spritebatch.cpp:28
~SpriteBatch()
Definition: spritebatch.cpp:38
Vertex top_left() const
Definition: spritebatch.h:47
Vertex top_right() const
Definition: spritebatch.h:48