From 34908f108ad7c2ee6cff96491a0bc40381477424 Mon Sep 17 00:00:00 2001 From: Yann Herklotz Date: Sun, 31 Dec 2017 18:00:01 +0000 Subject: [Test] Benchmarking the engine and trying to optimize. --- yage/core/imageloader.cpp | 2 +- yage/core/logger.h | 8 +++---- yage/core/resourcemanager.cpp | 4 ++-- yage/core/resourcemanager.h | 5 +++-- yage/core/texturecache.cpp | 6 ++++-- yage/core/texturecache.h | 2 +- yage/render/spritebatch.cpp | 49 +++++++++++++++++++++---------------------- 7 files changed, 39 insertions(+), 37 deletions(-) (limited to 'yage') diff --git a/yage/core/imageloader.cpp b/yage/core/imageloader.cpp index 60290058..7b34312d 100644 --- a/yage/core/imageloader.cpp +++ b/yage/core/imageloader.cpp @@ -48,7 +48,7 @@ Texture ImageLoader::loadPng(const std::string &file_path) glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); diff --git a/yage/core/logger.h b/yage/core/logger.h index 98c64b88..d1f75aec 100644 --- a/yage/core/logger.h +++ b/yage/core/logger.h @@ -13,13 +13,13 @@ #include #include +#include "../util/active.h" +#include "logmessage.h" +#include "logsink.h" + namespace yage { -class Active; -class LogMessage; -class LogSink; - class Logger { public: diff --git a/yage/core/resourcemanager.cpp b/yage/core/resourcemanager.cpp index eac09305..ec3761b8 100644 --- a/yage/core/resourcemanager.cpp +++ b/yage/core/resourcemanager.cpp @@ -14,9 +14,9 @@ namespace yage TextureCache ResourceManager::texture_cache_; -Texture ResourceManager::getTexture(const std::string &texture_path) +Texture ResourceManager::getTexture(const std::string &texture_path, int x, int y) { - return texture_cache_.getTexture(texture_path); + return texture_cache_.getTexture(texture_path, x, y); } } // namespace yage diff --git a/yage/core/resourcemanager.h b/yage/core/resourcemanager.h index a0249436..11a16f63 100644 --- a/yage/core/resourcemanager.h +++ b/yage/core/resourcemanager.h @@ -24,7 +24,7 @@ namespace yage { -class Texture; +struct Texture; class ResourceManager { @@ -32,7 +32,8 @@ private: static TextureCache texture_cache_; public: - static Texture getTexture(const std::string &texture_path); + static Texture getTexture(const std::string &texture_path, int x = 1, + int y = 1); }; } // namespace yage diff --git a/yage/core/texturecache.cpp b/yage/core/texturecache.cpp index 447051b7..66251de5 100644 --- a/yage/core/texturecache.cpp +++ b/yage/core/texturecache.cpp @@ -7,19 +7,21 @@ */ #include "texturecache.h" -#include "imageloader.h" #include "../data/texture.h" +#include "imageloader.h" namespace yage { -Texture TextureCache::getTexture(const std::string &texture_path) +Texture TextureCache::getTexture(const std::string &texture_path, int x, int y) { auto itr = texture_map_.find(texture_path); if (itr == texture_map_.end()) { Texture new_texture = ImageLoader::loadPng(texture_path); texture_map_.insert(make_pair(texture_path, new_texture)); + new_texture.x = x; + new_texture.y = y; return new_texture; } diff --git a/yage/core/texturecache.h b/yage/core/texturecache.h index ac57aaba..fbedab9d 100644 --- a/yage/core/texturecache.h +++ b/yage/core/texturecache.h @@ -24,7 +24,7 @@ private: public: TextureCache() = default; - Texture getTexture(const std::string &texture_path); + Texture getTexture(const std::string &texture_path, int x = 1, int y = 1); Texture getTextureFromSpriteSheet(); }; 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 -#include #include +#include "../core/logger.h" + +#include 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 vertices; + std::vector 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() -- cgit