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