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. --- examples/simplegame/main.cpp | 7 ++++--- yage/core/window.cpp | 2 +- yage/render/spritebatch.cpp | 27 +++++++++++++++++---------- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/examples/simplegame/main.cpp b/examples/simplegame/main.cpp index 03c85123..bd5a05b9 100644 --- a/examples/simplegame/main.cpp +++ b/examples/simplegame/main.cpp @@ -57,10 +57,11 @@ int main() camera.update(textureProgram); + int amount = 5; time = glfwGetTime(); - for (int i = 0; i < 1920/10; i++) { - for(int j = 0; j < 1080/10; j++) - sp.draw({(float)(10*i), (float)(10*j), 10.f, 10.f}, {0.f, 0.f, 1.f, 1.f}, fountain.id, + for (int i = 0; i < 1920/amount; i++) { + for(int j = 0; j < 1080/amount; j++) + sp.draw({(float)(amount*i), (float)(amount*j), (float)amount, (float)amount}, {0.f, 0.f, 1.f, 1.f}, fountain.id, Colour(255, 255, 255, 255), 0); } diff --git a/yage/core/window.cpp b/yage/core/window.cpp index 5db75074..d8f3a7e8 100644 --- a/yage/core/window.cpp +++ b/yage/core/window.cpp @@ -58,7 +58,7 @@ void Window::create(std::string window_name, int width, int height) glfwSetKeyCallback(window_, key_callback); // set vsync on - glfwSwapInterval(1); + glfwSwapInterval(0); // set the clear colour to black glClearColor(0.18f, 0.18f, 0.18f, 1.f); 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