aboutsummaryrefslogtreecommitdiffstats
path: root/include/YAGE/spritebatch.hpp
blob: 61efd0aa83f804b2ebf8cf1251d9a208fa906fe1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/* ----------------------------------------------------------------------------
 * spritebatch.hpp
 *
 * Copyright (c) 2017 Yann Herklotz Grave <ymherklotz@gmail.com> -- MIT License
 * See file LICENSE for more details
 * ----------------------------------------------------------------------------
 */

#ifndef SPRITE_BATCH_HPP
#define SPRITE_BATCH_HPP

#include "vertex.hpp"

#include <GL/glew.h>
#include <glm/glm.hpp>

#include <vector>

namespace yage
{

class SpriteBatch;

class Glyph
{
    // member variables
private:
    GLuint texture_;
    float depth_;
    Vertex top_left_;
    Vertex top_right_;
    Vertex bottom_right_;
    Vertex bottom_left_;

    // member functions
public:
    Glyph(GLuint texture, float depth, const Vertex &top_left, const Vertex &top_right, const Vertex &bottom_right, const Vertex &bottom_left);

    inline GLuint texture() const { return texture_; }
    inline float depth() const { return depth_; }
    inline Vertex top_left() const { return top_left_; }
    inline Vertex top_right() const { return top_right_; }
    inline Vertex bottom_right() const { return bottom_right_; }
    inline Vertex bottom_left() const { return bottom_left_; }
};

class RenderBatch
{
    friend SpriteBatch;
    // member variables
private:
    GLsizei num_vertices_;    
    GLint offset_;
    GLuint texture_;

    // member functions
public:
    RenderBatch(GLint offset, GLsizei num_vertices, GLuint texture);

    // getters
    inline GLint offset() const { return offset_; }
    inline GLsizei num_vertices() const { return num_vertices_; }
    inline GLuint texture() const { return texture_; }
};

class SpriteBatch
{
    // member variables
public:
    static const int NUM_VERTICES=6;
private:
    GLuint vbo_=0;
    GLuint vao_=0;
    std::vector<Glyph> glyphs_;
    std::vector<Glyph *> glyph_ptrs_;
    std::vector<RenderBatch> render_batches_;

    // member functions
public:
    SpriteBatch();
    ~SpriteBatch();

    // initialize vaos and vbos
    void init();
    void begin();
    void end();
    // adds a sprite to the sprite batch to be rendered later
    void draw(const glm::vec4 &destination_rect, const glm::vec4 &uv_rect, GLuint texture, const Color &color, float depth);
    // render the batch
    void render();
private:
    void createVertexArray();
    void createRenderBatches();
    void sortGlyphs();
};
    
} // yage

#endif