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