aboutsummaryrefslogtreecommitdiffstats
path: root/yage/render/spritebatch.cpp
diff options
context:
space:
mode:
authorYann Herklotz <ymherklotz@gmail.com>2017-12-31 18:00:01 +0000
committerYann Herklotz <ymherklotz@gmail.com>2017-12-31 18:00:01 +0000
commit34908f108ad7c2ee6cff96491a0bc40381477424 (patch)
treef58c6bdde485d07a7136f78055240bab923bd4a6 /yage/render/spritebatch.cpp
parent943e3a5bc98ebcc2aa1b1d576700f7c4010c143c (diff)
downloadYAGE-34908f108ad7c2ee6cff96491a0bc40381477424.tar.gz
YAGE-34908f108ad7c2ee6cff96491a0bc40381477424.zip
[Test] Benchmarking the engine and trying to optimize.
Diffstat (limited to 'yage/render/spritebatch.cpp')
-rw-r--r--yage/render/spritebatch.cpp49
1 files changed, 24 insertions, 25 deletions
diff --git a/yage/render/spritebatch.cpp b/yage/render/spritebatch.cpp
index bce5986f..09df5b9f 100644
--- a/yage/render/spritebatch.cpp
+++ b/yage/render/spritebatch.cpp
@@ -9,8 +9,10 @@
#include "spritebatch.h"
#include <algorithm>
-#include <iostream>
#include <stdexcept>
+#include "../core/logger.h"
+
+#include <GLFW/glfw3.h>
namespace yage
{
@@ -61,13 +63,12 @@ SpriteBatch::SpriteBatch() : vao_(0), vbo_(0)
SpriteBatch::~SpriteBatch()
{
- if (vao_ != 0) {
- glDeleteVertexArrays(1, &vao_);
- }
-
if (vbo_ != 0) {
glDeleteBuffers(1, &vbo_);
}
+ if (vao_ != 0) {
+ glDeleteVertexArrays(1, &vao_);
+ }
}
void SpriteBatch::begin()
@@ -118,10 +119,10 @@ void SpriteBatch::render()
{
glBindVertexArray(vao_);
// sort and create render batches
- createRenderBatches();
sortGlyphs();
+ createRenderBatches();
+ glActiveTexture(GL_TEXTURE0);
for (auto &&batch : render_batches_) {
- glActiveTexture(GL_TEXTURE0);
glBindTexture(GL_TEXTURE_2D, batch.texture);
glDrawArrays(GL_TRIANGLES, batch.offset, batch.num_vertices);
}
@@ -134,13 +135,13 @@ void SpriteBatch::render()
void SpriteBatch::createRenderBatches()
{
- std::vector<Vertex> vertices;
+ std::vector<Vertex> vertices(glyph_ptrs_.size() * NUM_VERTICES);
if (glyph_ptrs_.empty()) {
return;
}
- render_batches_.reserve(glyph_ptrs_.size() * NUM_VERTICES);
-
+ int cv = 0;
+
for (int i = 0; i < (int)glyph_ptrs_.size(); ++i) {
if (i == 0 || (i > 0 && (glyph_ptrs_[i]->texture() !=
glyph_ptrs_[i - 1]->texture()))) {
@@ -151,22 +152,20 @@ void SpriteBatch::createRenderBatches()
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]->top_right());
- vertices.push_back(glyph_ptrs_[i]->bottom_right());
- vertices.push_back(glyph_ptrs_[i]->bottom_left());
-
- // orphan the buffer
- glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), nullptr,
- GL_DYNAMIC_DRAW);
- // upload the data
- glBufferSubData(GL_ARRAY_BUFFER, 0, vertices.size() * sizeof(Vertex),
- vertices.data());
- // unbind buffer
+ vertices[cv++] = glyph_ptrs_[i]->bottom_left();
+ vertices[cv++] = glyph_ptrs_[i]->top_left();
+ vertices[cv++] = 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();
}
+
+ // orphan the buffer
+ glBufferData(GL_ARRAY_BUFFER, vertices.size() * sizeof(Vertex), nullptr,
+ GL_DYNAMIC_DRAW);
+ glBufferSubData(GL_ARRAY_BUFFER, 0, vertices.size() * sizeof(Vertex),
+ vertices.data());
}
void SpriteBatch::sortGlyphs()