From 2aed98d68864aa018e8d5b943bed95693d1a33bf Mon Sep 17 00:00:00 2001 From: karl Date: Tue, 4 May 2021 12:22:23 +0200 Subject: [PATCH] Some refactoring and better usage of text --- cpp/TextLabel.cpp | 12 ++++++++---- include/Gedeng/TextLabel.h | 8 ++++++-- test/parallax-demo/main.cpp | 15 +++++++++++++-- 3 files changed, 27 insertions(+), 8 deletions(-) diff --git a/cpp/TextLabel.cpp b/cpp/TextLabel.cpp index e07a305..e81da58 100644 --- a/cpp/TextLabel.cpp +++ b/cpp/TextLabel.cpp @@ -11,7 +11,7 @@ namespace Gedeng { -TextLabel::TextLabel() : shader(Shader("Shader/text.vs", "Shader/text.fs")) { +TextLabel::TextLabel(const String &text) : text(text), shader(Shader("Shader/text.vs", "Shader/text.fs")) { FT_Library ft; if (FT_Init_FreeType(&ft)) { GG_CORE_ERROR("Couldn't create font library!"); @@ -25,7 +25,7 @@ TextLabel::TextLabel() : shader(Shader("Shader/text.vs", "Shader/text.fs")) { } // Set size - FT_Set_Pixel_Sizes(face, 0, 48); + FT_Set_Pixel_Sizes(face, 0, 12); // Load first 128 ASCII Characters glPixelStorei(GL_UNPACK_ALIGNMENT, 1); // disable byte-alignment restriction @@ -75,14 +75,18 @@ TextLabel::TextLabel() : shader(Shader("Shader/text.vs", "Shader/text.fs")) { valid = true; } -void TextLabel::render_text(String text, float x, float y, float scale, Vector3 color) { +void TextLabel::set_text(const String &text) { + this->text = text; +} + +void TextLabel::render_text(float x, float y, float scale, Vector3 color) { // activate corresponding render state shader.use(); glUniform3f(glGetUniformLocation(shader.ID, "textColor"), color.x, color.y, color.z); // FIXME: We need the screen size here... (or UI module size?) - glm::mat4 projection = glm::ortho(0.0f, static_cast(500), 0.0f, static_cast(500)); + glm::mat4 projection = glm::ortho(0.0f, static_cast(900), 0.0f, static_cast(600)); glUniformMatrix4fv(glGetUniformLocation(shader.ID, "projection"), 1, GL_FALSE, glm::value_ptr(projection)); glActiveTexture(GL_TEXTURE0); diff --git a/include/Gedeng/TextLabel.h b/include/Gedeng/TextLabel.h index 99cd347..f2ba6be 100644 --- a/include/Gedeng/TextLabel.h +++ b/include/Gedeng/TextLabel.h @@ -13,9 +13,11 @@ namespace Gedeng { class TextLabel { public: - TextLabel(); + TextLabel(const String &text = String("")); - void render_text(String text, float x, float y, float scale, Vector3 color); + void set_text(const String &text); + + void render_text(float x, float y, float scale, Vector3 color); bool is_valid(); @@ -29,6 +31,8 @@ class TextLabel { std::map characters; unsigned int vao, vbo; + + String text; bool valid; Shader shader; diff --git a/test/parallax-demo/main.cpp b/test/parallax-demo/main.cpp index 64a6cc4..05cf4e4 100644 --- a/test/parallax-demo/main.cpp +++ b/test/parallax-demo/main.cpp @@ -42,6 +42,18 @@ class ParallaxApp : public Gedeng::Application { if (Gedeng::Input::is_key_down(GLFW_KEY_X)) { bump_depth -= delta * 0.1; } + + Gedeng::String text; + text += std::to_string(number_of_steps).c_str(); + text += " steps, "; + + text += std::to_string(number_of_refinement_steps).c_str(); + text += " refinement steps, "; + + text += std::to_string(bump_depth).c_str(); + text += " bump depth"; + + debug_text.set_text(text); } void dynamic_update(double delta) override { @@ -71,8 +83,7 @@ class ParallaxApp : public Gedeng::Application { quad_mesh.rotate(delta * 25.0f, glm::normalize(glm::vec3(0.0, 0.0, 1.0))); quad_mesh.render(render_shader); - debug_text.render_text(Gedeng::String("This is sample text"), 25.0f, 25.0f, 1.0f, - Gedeng::Vector3(1.0, 1.0, 0.0)); + debug_text.render_text(25.0f, 25.0f, 1.0f, Gedeng::Vector3(1.0, 1.0, 0.0)); } private: