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. --- examples/resources/fighter_fd.png | Bin 0 -> 6467 bytes examples/resources/fighter_fl.png | Bin 0 -> 2916 bytes examples/resources/fighter_md.png | Bin 0 -> 6464 bytes examples/resources/fighter_ml.png | Bin 0 -> 2843 bytes examples/resources/healer_fd.png | Bin 0 -> 7862 bytes examples/resources/healer_fl.png | Bin 0 -> 3256 bytes examples/resources/healer_md.png | Bin 0 -> 6756 bytes examples/resources/healer_ml.png | Bin 0 -> 2940 bytes examples/resources/mage_fd.png | Bin 0 -> 7026 bytes examples/resources/mage_fl.png | Bin 0 -> 3283 bytes examples/resources/mage_md.png | Bin 0 -> 6794 bytes examples/resources/mage_ml.png | Bin 0 -> 3139 bytes examples/resources/ranger_fd.png | Bin 0 -> 6798 bytes examples/resources/ranger_fl.png | Bin 0 -> 2884 bytes examples/resources/ranger_md.png | Bin 0 -> 6182 bytes examples/resources/ranger_ml.png | Bin 0 -> 2803 bytes examples/resources/textureshader.frag | 12 ++-- examples/resources/textureshader.vert | 8 +-- examples/shooter/main.cpp | 108 +++++++++++++++++++++++++++++++++- examples/shooter/player.cpp | 62 +++++++++++++++++++ examples/shooter/player.h | 37 ++++++++++++ examples/simplegame/main.cpp | 41 ++++++++++--- 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 ++++++++------- 29 files changed, 288 insertions(+), 56 deletions(-) create mode 100644 examples/resources/fighter_fd.png create mode 100644 examples/resources/fighter_fl.png create mode 100644 examples/resources/fighter_md.png create mode 100644 examples/resources/fighter_ml.png create mode 100644 examples/resources/healer_fd.png create mode 100644 examples/resources/healer_fl.png create mode 100644 examples/resources/healer_md.png create mode 100644 examples/resources/healer_ml.png create mode 100644 examples/resources/mage_fd.png create mode 100644 examples/resources/mage_fl.png create mode 100644 examples/resources/mage_md.png create mode 100644 examples/resources/mage_ml.png create mode 100644 examples/resources/ranger_fd.png create mode 100644 examples/resources/ranger_fl.png create mode 100644 examples/resources/ranger_md.png create mode 100644 examples/resources/ranger_ml.png create mode 100644 examples/shooter/player.cpp create mode 100644 examples/shooter/player.h diff --git a/examples/resources/fighter_fd.png b/examples/resources/fighter_fd.png new file mode 100644 index 00000000..ea576b4d Binary files /dev/null and b/examples/resources/fighter_fd.png differ diff --git a/examples/resources/fighter_fl.png b/examples/resources/fighter_fl.png new file mode 100644 index 00000000..69b677ca Binary files /dev/null and b/examples/resources/fighter_fl.png differ diff --git a/examples/resources/fighter_md.png b/examples/resources/fighter_md.png new file mode 100644 index 00000000..1b333abb Binary files /dev/null and b/examples/resources/fighter_md.png differ diff --git a/examples/resources/fighter_ml.png b/examples/resources/fighter_ml.png new file mode 100644 index 00000000..74dca840 Binary files /dev/null and b/examples/resources/fighter_ml.png differ diff --git a/examples/resources/healer_fd.png b/examples/resources/healer_fd.png new file mode 100644 index 00000000..138e7c48 Binary files /dev/null and b/examples/resources/healer_fd.png differ diff --git a/examples/resources/healer_fl.png b/examples/resources/healer_fl.png new file mode 100644 index 00000000..8360cfb9 Binary files /dev/null and b/examples/resources/healer_fl.png differ diff --git a/examples/resources/healer_md.png b/examples/resources/healer_md.png new file mode 100644 index 00000000..20ce4d2f Binary files /dev/null and b/examples/resources/healer_md.png differ diff --git a/examples/resources/healer_ml.png b/examples/resources/healer_ml.png new file mode 100644 index 00000000..28377d6b Binary files /dev/null and b/examples/resources/healer_ml.png differ diff --git a/examples/resources/mage_fd.png b/examples/resources/mage_fd.png new file mode 100644 index 00000000..b6a692f7 Binary files /dev/null and b/examples/resources/mage_fd.png differ diff --git a/examples/resources/mage_fl.png b/examples/resources/mage_fl.png new file mode 100644 index 00000000..66f5d2e5 Binary files /dev/null and b/examples/resources/mage_fl.png differ diff --git a/examples/resources/mage_md.png b/examples/resources/mage_md.png new file mode 100644 index 00000000..40838087 Binary files /dev/null and b/examples/resources/mage_md.png differ diff --git a/examples/resources/mage_ml.png b/examples/resources/mage_ml.png new file mode 100644 index 00000000..d559fb56 Binary files /dev/null and b/examples/resources/mage_ml.png differ diff --git a/examples/resources/ranger_fd.png b/examples/resources/ranger_fd.png new file mode 100644 index 00000000..e574cb1f Binary files /dev/null and b/examples/resources/ranger_fd.png differ diff --git a/examples/resources/ranger_fl.png b/examples/resources/ranger_fl.png new file mode 100644 index 00000000..9bc31719 Binary files /dev/null and b/examples/resources/ranger_fl.png differ diff --git a/examples/resources/ranger_md.png b/examples/resources/ranger_md.png new file mode 100644 index 00000000..5803cbe6 Binary files /dev/null and b/examples/resources/ranger_md.png differ diff --git a/examples/resources/ranger_ml.png b/examples/resources/ranger_ml.png new file mode 100644 index 00000000..e2e528b4 Binary files /dev/null and b/examples/resources/ranger_ml.png differ diff --git a/examples/resources/textureshader.frag b/examples/resources/textureshader.frag index 8d916be6..1292de96 100644 --- a/examples/resources/textureshader.frag +++ b/examples/resources/textureshader.frag @@ -1,11 +1,10 @@ -#version 450 +#version 330 -layout(location = 0) in vec2 fragment_position; -layout(location = 1) in vec4 fragment_colour; -layout(location = 2) in vec2 fragment_uv; +in vec2 fragment_position; +in vec4 fragment_colour; +in vec2 fragment_uv; -layout(location = 0) out vec4 colour; -layout(location = 1) out vec4 colour2; +out vec4 colour; uniform sampler2D texture_sampler; @@ -13,6 +12,5 @@ void main() { vec4 texture_colour = texture(texture_sampler, fragment_uv); - colour2 = vec4(1.f, 0.f, 0.f, 1.f); colour = texture_colour; } diff --git a/examples/resources/textureshader.vert b/examples/resources/textureshader.vert index b0c11a6c..aac246c9 100644 --- a/examples/resources/textureshader.vert +++ b/examples/resources/textureshader.vert @@ -1,12 +1,12 @@ -#version 450 +#version 330 layout(location = 0) in vec2 vertex_position; layout(location = 1) in vec4 vertex_colour; layout(location = 2) in vec2 vertex_uv; -layout(location = 0) out vec2 fragment_position; -layout(location = 1) out vec4 fragment_colour; -layout(location = 2) out vec2 fragment_uv; +out vec2 fragment_position; +out vec4 fragment_colour; +out vec2 fragment_uv; uniform mat4 P; diff --git a/examples/shooter/main.cpp b/examples/shooter/main.cpp index 12cf218d..d2727877 100644 --- a/examples/shooter/main.cpp +++ b/examples/shooter/main.cpp @@ -1,6 +1,6 @@ #include -#include "glad/glad.h" +#include "player.h" using std::cout; @@ -12,9 +12,115 @@ int main(int argc, char **argv) yage::Shader shader("examples/resources/textureshader.vert", "examples/resources/textureshader.frag"); + std::vector male_l = { + yage::ResourceManager::getTexture("examples/resources/fighter_ml.png", + 3, 4), + yage::ResourceManager::getTexture("examples/resources/ranger_ml.png", 3, + 4), + yage::ResourceManager::getTexture("examples/resources/mage_ml.png", 3, + 4), + yage::ResourceManager::getTexture("examples/resources/healer_ml.png", 3, + 4)}; + std::vector female_l = { + yage::ResourceManager::getTexture("examples/resources/fighter_fl.png", + 3, 4), + yage::ResourceManager::getTexture("examples/resources/ranger_fl.png", 3, + 4), + yage::ResourceManager::getTexture("examples/resources/mage_fl.png", 3, + 4), + yage::ResourceManager::getTexture("examples/resources/healer_fl.png", 3, + 4)}; + std::vector male_d = { + yage::ResourceManager::getTexture("examples/resources/fighter_md.png", + 3, 4), + yage::ResourceManager::getTexture("examples/resources/ranger_md.png", 3, + 4), + yage::ResourceManager::getTexture("examples/resources/mage_md.png", 3, + 4), + yage::ResourceManager::getTexture("examples/resources/healer_md.png", 3, + 4)}; + std::vector female_d = { + yage::ResourceManager::getTexture("examples/resources/fighter_fd.png", + 3, 4), + yage::ResourceManager::getTexture("examples/resources/ranger_fd.png", 3, + 4), + yage::ResourceManager::getTexture("examples/resources/mage_fd.png", 3, + 4), + yage::ResourceManager::getTexture("examples/resources/healer_fd.png", 3, + 4)}; + + yage::SpriteBatch sp; + yage::Camera camera(800, 600); + + int i = 0; + int j = 0; + bool space_pressed = false; + bool c_pressed = false; + + shader.use(); + shader.setUniform("texture_sampler", 0); + + auto textures = male_l; + + Player player({400, 300, 48 * 2, 64 * 2}, textures.front()); + while (!window.shouldClose()) { window.pollEvents(); + + if (window.keyPressed(yage::key::D)) { + player.move(Direction::RIGHT); + } + if (window.keyPressed(yage::key::S)) { + player.move(Direction::DOWN); + } + if (window.keyPressed(yage::key::A)) { + player.move(Direction::LEFT); + } + if (window.keyPressed(yage::key::W)) { + player.move(Direction::UP); + } + if (!window.keyPressed(yage::key::D) && + !window.keyPressed(yage::key::S) && + !window.keyPressed(yage::key::A) && + !window.keyPressed(yage::key::W)) { + player.idle(); + } + if (window.keyPressed(yage::key::SPACE) && !space_pressed) { + space_pressed = true; + i = (i + 1) % textures.size(); + } + if (!window.keyPressed(yage::key::SPACE)) { + space_pressed = false; + } + if (window.keyPressed(yage::key::C) && !c_pressed) { + c_pressed = true; + j = (j + 1) % 4; + switch (j) { + case 0: + textures = male_l; + break; + case 1: + textures = male_d; + break; + case 2: + textures = female_l; + break; + case 3: + textures = female_d; + break; + } + } + if (!window.keyPressed(yage::key::C)) { + c_pressed = false; + } + + player.setTexture(textures[i]); + + camera.update(shader); + window.clearBuffer(); + player.draw(sp); + sp.render(); window.swapBuffer(); } diff --git a/examples/shooter/player.cpp b/examples/shooter/player.cpp new file mode 100644 index 00000000..2d0148e6 --- /dev/null +++ b/examples/shooter/player.cpp @@ -0,0 +1,62 @@ +#include "player.h" + +Player::Player(const glm::vec4 &bound, const yage::Texture &texture) + : bound_(bound), texture_(texture), direction_(Direction::DOWN), + action_(Action::IDLE), speed_(4) +{ +} + +void Player::setTexture(const yage::Texture &texture) { + texture_ = texture; +} + +void Player::draw(yage::SpriteBatch &sp) const +{ + static int time = 0; + static int iteration = 0; + float width = 1.f / static_cast(texture_.x); + float height = 1.f / static_cast(texture_.y); + + switch (action_) { + case Action::IDLE: + sp.draw(bound_, + {width, static_cast(direction_) * height, width, height}, + texture_.id, yage::Colour(255, 255, 255, 255), 0); + break; + case Action::MOVING: + if(time % 15 == 0) { + iteration = (iteration + 1) % 2; + } + sp.draw(bound_, + {iteration * 2 * width, static_cast(direction_) * height, width, height}, + texture_.id, yage::Colour(255, 255, 255, 255), 0); + time = (time + 1) % 59; + break; + } +} + +void Player::move(Direction direction) +{ + direction_ = direction; + action_ = Action::MOVING; + + switch (direction_) { + case Direction::LEFT: + bound_.x -= speed_; + break; + case Direction::DOWN: + bound_.y -= speed_; + break; + case Direction::RIGHT: + bound_.x += speed_; + break; + case Direction::UP: + bound_.y += speed_; + break; + } +} + +void Player::idle() +{ + action_ = Action::IDLE; +} diff --git a/examples/shooter/player.h b/examples/shooter/player.h new file mode 100644 index 00000000..72af7c34 --- /dev/null +++ b/examples/shooter/player.h @@ -0,0 +1,37 @@ +#ifndef EXAMPLE_SHOOTER_PLAYER_H +#define EXAMPLE_SHOOTER_PLAYER_H + +#include "yage/yage.h" + +enum class Direction { + LEFT, + DOWN, + RIGHT, + UP, +}; + +enum class Action { + IDLE, + MOVING, +}; + +class Player +{ +public: + Player(const glm::vec4 &bound, const yage::Texture &texture); + + void setTexture(const yage::Texture &texture); + + void draw(yage::SpriteBatch &sp) const; + + void move(Direction direction); + void idle(); +private: + glm::vec4 bound_; + yage::Texture texture_; + Direction direction_; + Action action_; + int speed_; +}; + +#endif diff --git a/examples/simplegame/main.cpp b/examples/simplegame/main.cpp index 895fce51..03c85123 100644 --- a/examples/simplegame/main.cpp +++ b/examples/simplegame/main.cpp @@ -17,10 +17,10 @@ using namespace yage; int main() { Window window; - window.create("Simple Game", 800, 640); + window.create("Simple Game", 1920, 1080); - Shader textureProgram("examples/resources/learnopenglshader.vert", - "examples/resources/learnopenglshader.frag"); + Shader textureProgram("examples/resources/textureshader.vert", + "examples/resources/textureshader.frag"); SpriteBatch sp; Texture fountain = ResourceManager::getTexture( @@ -32,9 +32,16 @@ int main() cout << "texture: " << brick.width << ", " << brick.height << '\n'; - Camera camera(800, 640); + Camera camera(1920, 1080); textureProgram.use(); - textureProgram.setUniform("ourTexture", 0); + textureProgram.setUniform("texture_sampler", 0); + + double prev_time = glfwGetTime(); + double final_time = 0; + double diff = 0; + double fps = 0; + int i = 0; + double time; while (!window.shouldClose()) { window.clearBuffer(); @@ -48,10 +55,30 @@ int main() texture = breast_plate; } - sp.draw({-0.5, -0.5, 1, 1}, {0, 0, 1, 1}, brick.id, Colour(255, 255, 255, 255), 0); + camera.update(textureProgram); - sp.render(); + 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, + Colour(255, 255, 255, 255), 0); + } + + sp.draw({50, 50, 100, 100}, {0, 0, 1, 1}, brick.id, Colour(255, 255, 255, 255), 1); + yLog << "draw: " << glfwGetTime() - time; + time = glfwGetTime(); + sp.render(); + yLog << "render: " << glfwGetTime() - time; window.swapBuffer(); + + if (i == 0) { + final_time = glfwGetTime(); + diff = final_time - prev_time; + prev_time = final_time; + fps = 1 / diff * 30; + yLog << "fps: " << fps; + } + i = (i + 1) % 30; } } 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