aboutsummaryrefslogtreecommitdiffstats
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
parent34908f108ad7c2ee6cff96491a0bc40381477424 (diff)
downloadYAGE-9038ee45f93b1ae07a28a516781676ebe3a67536.tar.gz
YAGE-9038ee45f93b1ae07a28a516781676ebe3a67536.zip
[Bug] Fixed null pointers in spritebatch.
-rw-r--r--examples/simplegame/main.cpp7
-rw-r--r--yage/core/window.cpp2
-rw-r--r--yage/render/spritebatch.cpp27
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 <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 {