From 9038ee45f93b1ae07a28a516781676ebe3a67536 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Sun, 31 Dec 2017 18:17:04 +0000 Subject: [Bug] Fixed null pointers in spritebatch. --- yage/render/spritebatch.cpp | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) (limited to 'yage/render') diff --git a/yage/render/spritebatch.cpp b/yage/render/spritebatch.cpp index 09df5b9f..fb8eb605 100644 --- a/yage/render/spritebatch.cpp +++ b/yage/render/spritebatch.cpp @@ -11,6 +11,9 @@ #include #include #include "../core/logger.h" +#include + +using std::cout; #include @@ -112,13 +115,13 @@ void SpriteBatch::draw(const glm::vec4 &destination_rect, // deal with fragmenting by creating vector of pointers glyphs_.emplace_back(texture, depth, top_left, top_right, bottom_right, bottom_left); - glyph_ptrs_.push_back(&glyphs_.back()); } void SpriteBatch::render() { glBindVertexArray(vao_); // sort and create render batches + sortGlyphs(); createRenderBatches(); glActiveTexture(GL_TEXTURE0); @@ -135,13 +138,13 @@ void SpriteBatch::render() void SpriteBatch::createRenderBatches() { - std::vector vertices(glyph_ptrs_.size() * NUM_VERTICES); + std::vector vertices; if (glyph_ptrs_.empty()) { return; } - int cv = 0; - + 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()))) { @@ -152,13 +155,13 @@ void SpriteBatch::createRenderBatches() render_batches_.back().num_vertices += NUM_VERTICES; } - vertices[cv++] = glyph_ptrs_[i]->bottom_left(); - vertices[cv++] = glyph_ptrs_[i]->top_left(); - vertices[cv++] = 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[cv++] = glyph_ptrs_[i]->top_right(); - vertices[cv++] = glyph_ptrs_[i]->bottom_right(); - vertices[cv++] = 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 @@ -170,6 +173,10 @@ void SpriteBatch::createRenderBatches() void SpriteBatch::sortGlyphs() { + glyph_ptrs_.reserve(glyphs_.size()); + 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 { -- cgit