diff options
author | Yann Herklotz <ymherklotz@gmail.com> | 2018-01-03 13:26:37 +0000 |
---|---|---|
committer | Yann Herklotz <ymherklotz@gmail.com> | 2018-01-03 13:27:23 +0000 |
commit | 86e4aa6265ade205aba94494a7a31a83b5686387 (patch) | |
tree | d7f9998b55d3fcc2b083242086afd05c1d436b6e /yage/render/spritebatch.cpp | |
parent | 9038ee45f93b1ae07a28a516781676ebe3a67536 (diff) | |
download | YAGE-86e4aa6265ade205aba94494a7a31a83b5686387.tar.gz YAGE-86e4aa6265ade205aba94494a7a31a83b5686387.zip |
[Engine] [Example] Reenabled vsync and working on example.
Diffstat (limited to 'yage/render/spritebatch.cpp')
-rw-r--r-- | yage/render/spritebatch.cpp | 48 |
1 files changed, 19 insertions, 29 deletions
diff --git a/yage/render/spritebatch.cpp b/yage/render/spritebatch.cpp index fb8eb605..14e30ccd 100644 --- a/yage/render/spritebatch.cpp +++ b/yage/render/spritebatch.cpp @@ -8,10 +8,10 @@ #include "spritebatch.h" -#include <algorithm> -#include <stdexcept> #include "../core/logger.h" +#include <algorithm> #include <iostream> +#include <stdexcept> using std::cout; @@ -22,15 +22,6 @@ namespace yage const int SpriteBatch::NUM_VERTICES; -Glyph::Glyph(GLuint texture, float depth, const Vertex &top_left, - const Vertex &top_right, const Vertex &bottom_right, - const Vertex &bottom_left) - : texture_(texture), depth_(depth), top_left_(top_left), - top_right_(top_right), bottom_right_(bottom_right), - bottom_left_(bottom_left) -{ -} - SpriteBatch::SpriteBatch() : vao_(0), vbo_(0) { glGenVertexArrays(1, &vao_); @@ -59,9 +50,6 @@ SpriteBatch::SpriteBatch() : vao_(0), vbo_(0) glEnableVertexAttribArray(0); glEnableVertexAttribArray(1); glEnableVertexAttribArray(2); - - // unbind vertex array object - glBindVertexArray(0); } SpriteBatch::~SpriteBatch() @@ -121,11 +109,12 @@ void SpriteBatch::render() { glBindVertexArray(vao_); // sort and create render batches - sortGlyphs(); createRenderBatches(); glActiveTexture(GL_TEXTURE0); for (auto &&batch : render_batches_) { + yLog << "Drawing " << batch.num_vertices + << " vertices bound to texture: " << batch.texture; glBindTexture(GL_TEXTURE_2D, batch.texture); glDrawArrays(GL_TRIANGLES, batch.offset, batch.num_vertices); } @@ -146,22 +135,22 @@ void SpriteBatch::createRenderBatches() vertices.reserve(glyph_ptrs_.size() * NUM_VERTICES); for (int i = 0; i < (int)glyph_ptrs_.size(); ++i) { - if (i == 0 || (i > 0 && (glyph_ptrs_[i]->texture() != - glyph_ptrs_[i - 1]->texture()))) { + if (i == 0 || (i > 0 && (glyph_ptrs_[i]->texture != + glyph_ptrs_[i - 1]->texture))) { render_batches_.emplace_back(i * NUM_VERTICES, NUM_VERTICES, - glyph_ptrs_[i]->texture()); + glyph_ptrs_[i]->texture); } else { render_batches_.back().num_vertices += NUM_VERTICES; } - vertices.push_back(glyph_ptrs_[i]->bottom_left()); - vertices.push_back(glyph_ptrs_[i]->top_left()); - vertices.push_back(glyph_ptrs_[i]->top_right()); + vertices.push_back(glyph_ptrs_[i]->bottom_left); + vertices.push_back(glyph_ptrs_[i]->top_left); + vertices.push_back(glyph_ptrs_[i]->top_right); - vertices.push_back(glyph_ptrs_[i]->top_right()); - vertices.push_back(glyph_ptrs_[i]->bottom_right()); - vertices.push_back(glyph_ptrs_[i]->bottom_left()); + vertices.push_back(glyph_ptrs_[i]->top_right); + vertices.push_back(glyph_ptrs_[i]->bottom_right); + vertices.push_back(glyph_ptrs_[i]->bottom_left); } // orphan the buffer @@ -174,16 +163,17 @@ void SpriteBatch::createRenderBatches() void SpriteBatch::sortGlyphs() { glyph_ptrs_.reserve(glyphs_.size()); - for(auto &glyph : glyphs_) { + for (auto &glyph : glyphs_) { glyph_ptrs_.push_back(&glyph); } + // sort using introsort or quicksort std::sort(glyph_ptrs_.begin(), glyph_ptrs_.end(), - [](Glyph *a, Glyph *b) -> bool { - if (a->depth() == b->depth()) { - return a->texture() < b->texture(); + [](details::Glyph *a, details::Glyph *b) -> bool { + if (a->depth == b->depth) { + return a->texture < b->texture; } - return a->depth() < b->depth(); + return a->depth < b->depth; }); } |