YAGE
Yet Another Game Engine
spritebatch.hpp
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 SPRITE_BATCH_HPP
10 #define 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 
24 class Glyph
25 {
26  // member variables
27 private:
28  GLuint texture_;
29  float depth_;
30  Vertex top_left_;
31  Vertex top_right_;
32  Vertex bottom_right_;
33  Vertex bottom_left_;
34 
35  // member functions
36 public:
37  Glyph(GLuint texture, float depth, const Vertex &top_left, const Vertex &top_right, const Vertex &bottom_right, const Vertex &bottom_left);
38 
39  inline GLuint texture() const { return texture_; }
40  inline float depth() const { return depth_; }
41  inline Vertex top_left() const { return top_left_; }
42  inline Vertex top_right() const { return top_right_; }
43  inline Vertex bottom_right() const { return bottom_right_; }
44  inline Vertex bottom_left() const { return bottom_left_; }
45 };
46 
48 {
49  friend SpriteBatch;
50  // member variables
51 private:
52  GLsizei num_vertices_;
53  GLint offset_;
54  GLuint texture_;
55 
56  // member functions
57 public:
58  RenderBatch(GLint offset, GLsizei num_vertices, GLuint texture);
59 
60  // getters
61  inline GLint offset() const { return offset_; }
62  inline GLsizei num_vertices() const { return num_vertices_; }
63  inline GLuint texture() const { return texture_; }
64 };
65 
67 {
68  // member variables
69 public:
70  static const int NUM_VERTICES=6;
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  // member functions
79 public:
80  SpriteBatch();
81  ~SpriteBatch();
82 
83  // initialize vaos and vbos
84  void init();
85  void begin();
86  void end();
87  // adds a sprite to the sprite batch to be rendered later
88  void draw(const glm::vec4 &destination_rect, const glm::vec4 &uv_rect, GLuint texture, const Color &color, float depth);
89  // render the batch
90  void render();
91 private:
92  void createVertexArray();
93  void createRenderBatches();
94  void sortGlyphs();
95 };
96 
97 } // yage
98 
99 #endif
Definition: spritebatch.hpp:47
Definition: vertex.hpp:58
Definition: spritebatch.hpp:66
Definition: spritebatch.hpp:24
Definition: camera2d.hpp:17
Definition: vertex.hpp:30