aboutsummaryrefslogtreecommitdiffstats
path: root/yage/render/spritebatch.cpp
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2017-12-31 18:17:04 +0000
committerYann Herklotz <ymherklotz@gmail.com>2017-12-31 18:17:04 +0000
commit9038ee45f93b1ae07a28a516781676ebe3a67536 (patch)
treedecf78fe537ff4860b598abc139816a8443aa96a /yage/render/spritebatch.cpp
parent34908f108ad7c2ee6cff96491a0bc40381477424 (diff)
downloadYAGE-9038ee45f93b1ae07a28a516781676ebe3a67536.tar.gz
YAGE-9038ee45f93b1ae07a28a516781676ebe3a67536.zip
[Bug] Fixed null pointers in spritebatch.
Diffstat (limited to 'yage/render/spritebatch.cpp')
-rw-r--r--yage/render/spritebatch.cpp27
1 files changed, 17 insertions, 10 deletions
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 <algorithm>
#include <stdexcept>
#include "../core/logger.h"
+#include <iostream>
+
+using std::cout;
#include <GLFW/glfw3.h>
@@ -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<Vertex> vertices(glyph_ptrs_.size() * NUM_VERTICES);
+ std::vector<Vertex> 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 {