YAGE  v0.1.1
Yet Another Game Engine
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
spritebatch.h
Go to the documentation of this file.
1 /* ----------------------------------------------------------------------------
2  * spritebatch.h
3  *
4  * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com> -- MIT License
5  * See file LICENSE for more details
6  * ----------------------------------------------------------------------------
7  */
8 
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 
20 #include <vector>
21 
22 namespace yage
23 {
24 
25 class SpriteBatch;
26 
29 class Glyph
30 {
31 private:
32  GLuint texture_;
33  float depth_;
38 
39 public:
40  Glyph(GLuint texture, float depth, const Vertex &top_left,
41  const Vertex &top_right, const Vertex &bottom_right,
42  const Vertex &bottom_left);
43 
44  GLuint texture() const { return texture_; }
45  float depth() const { return depth_; }
46  Vertex top_left() const { return top_left_; }
47  Vertex top_right() const { return top_right_; }
48  Vertex bottom_right() const { return bottom_right_; }
49  Vertex bottom_left() const { return bottom_left_; }
50 };
51 
53 {
54  friend SpriteBatch;
55 
56 private:
57  GLsizei num_vertices_;
58  GLint offset_;
59  GLuint texture_;
60 
61 public:
62  RenderBatch(GLint offset, GLsizei num_vertices, GLuint texture);
63 
64  GLint offset() const { return offset_; }
65  GLsizei num_vertices() const { return num_vertices_; }
66  GLuint texture() const { return texture_; }
67 };
68 
70 {
71 public:
72  static const int NUM_VERTICES = 6;
73 
74 private:
75  GLuint vbo_ = 0;
76  GLuint vao_ = 0;
77  std::vector<Glyph> glyphs_;
78  std::vector<Glyph *> glyph_ptrs_;
79  std::vector<RenderBatch> render_batches_;
80 
81 public:
82  SpriteBatch();
83  SpriteBatch(const SpriteBatch &) = delete;
84  SpriteBatch(SpriteBatch &&) = delete;
85  ~SpriteBatch();
86 
87  SpriteBatch &operator=(const SpriteBatch &) = delete;
88  SpriteBatch &operator=(SpriteBatch &&) = delete;
89 
90  // initialize vaos and vbos
91  void init();
92  void begin();
93  void end();
94  // adds a sprite to the sprite batch to be rendered later
95  void draw(const glm::vec4 &destination_rect, const glm::vec4 &uv_rect,
96  GLuint texture, const Color &color, float depth);
97  // render the batch
98  void render();
99 
100 private:
101  void createVertexArray();
102  void createRenderBatches();
103  void sortGlyphs();
104 };
105 
106 } // namespace yage
107 
108 #endif
Definition: spritebatch.h:52
std::vector< Glyph * > glyph_ptrs_
Definition: spritebatch.h:78
GLuint texture_
Definition: spritebatch.h:59
Vertex bottom_left_
Definition: spritebatch.h:37
void end()
Definition: spritebatch.cpp:58
Definition: vertex.h:49
Definition: spritebatch.h:69
Vertex bottom_right() const
Definition: spritebatch.h:48
void init()
Definition: spritebatch.cpp:46
void createVertexArray()
Definition: spritebatch.cpp:105
void begin()
Definition: spritebatch.cpp:51
Vertex top_left_
Definition: spritebatch.h:34
Vertex bottom_left() const
Definition: spritebatch.h:49
void render()
Definition: spritebatch.cpp:95
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:75
float depth_
Definition: spritebatch.h:33
GLint offset() const
Definition: spritebatch.h:64
friend SpriteBatch
Definition: spritebatch.h:54
void createRenderBatches()
Definition: spritebatch.cpp:143
GLuint texture_
Definition: spritebatch.h:32
float depth() const
Definition: spritebatch.h:45
Vertex bottom_right_
Definition: spritebatch.h:36
static const int NUM_VERTICES
Definition: spritebatch.h:72
GLuint vao_
Definition: spritebatch.h:76
GLuint texture() const
Definition: spritebatch.h:44
std::vector< RenderBatch > render_batches_
Definition: spritebatch.h:79
std::vector< Glyph > glyphs_
Definition: spritebatch.h:77
GLint offset_
Definition: spritebatch.h:58
void draw(const glm::vec4 &destination_rect, const glm::vec4 &uv_rect, GLuint texture, const Color &color, float depth)
Definition: spritebatch.cpp:64
GLsizei num_vertices() const
Definition: spritebatch.h:65
Vertex top_right_
Definition: spritebatch.h:35
GLuint texture() const
Definition: spritebatch.h:66
void sortGlyphs()
Definition: spritebatch.cpp:181
Glyph with information of the texture.
Definition: spritebatch.h:29
GLsizei num_vertices_
Definition: spritebatch.h:57
RenderBatch(GLint offset, GLsizei num_vertices, GLuint texture)
Definition: spritebatch.cpp:28
~SpriteBatch()
Definition: spritebatch.cpp:35
Vertex top_left() const
Definition: spritebatch.h:46
Vertex top_right() const
Definition: spritebatch.h:47
Definition: vertex.h:26