From 30ae57d0baf3f49e9fb12f66778aa6dda62717e5 Mon Sep 17 00:00:00 2001 From: Ritchie Cunningham Date: Thu, 2 Oct 2025 23:45:56 +0100 Subject: [PATCH] [Fix] Created get_text_width for menu button centering. --- client/src/gfx/txt_renderer.cpp | 8 ++++++++ client/src/gfx/txt_renderer.h | 2 ++ client/src/ui/main_menu.cpp | 9 ++++----- common/src/machine.h | 1 - 4 files changed, 14 insertions(+), 6 deletions(-) diff --git a/client/src/gfx/txt_renderer.cpp b/client/src/gfx/txt_renderer.cpp index f0012d8..7f52680 100644 --- a/client/src/gfx/txt_renderer.cpp +++ b/client/src/gfx/txt_renderer.cpp @@ -116,3 +116,11 @@ void TextRenderer::render_text(const char* text, float x, float y, float scale, glBindVertexArray(0); glBindTexture(GL_TEXTURE_2D, 0); } + +float TextRenderer::get_text_width(const char* text, float scale) { + float width = 0.0f; + for(const char* p = text; *p; p++) { + width += (_chars[*p].advance >> 6) * scale; + } + return width; +} diff --git a/client/src/gfx/txt_renderer.h b/client/src/gfx/txt_renderer.h index c731e1c..3fd2260 100644 --- a/client/src/gfx/txt_renderer.h +++ b/client/src/gfx/txt_renderer.h @@ -22,6 +22,8 @@ public: void load_font(const char* font_path, unsigned int font_size); void render_text(const char* text, float x, float y, float scale, const Color& color); + float get_text_width(const char* text, float scale); + private: Shader* _txt_shader; diff --git a/client/src/ui/main_menu.cpp b/client/src/ui/main_menu.cpp index 9d98f5e..a02cae3 100644 --- a/client/src/ui/main_menu.cpp +++ b/client/src/ui/main_menu.cpp @@ -113,11 +113,10 @@ void MainMenu::render(ShapeRenderer* shape_renderer, TextRenderer* txt_renderer) button.rect.h, button_color); } - /* - * Draw button text centered. - * TODO: Calculate text width for perfect centering. - */ - txt_renderer->render_text(button.label.c_str(), button.rect.x + 50, + /* Draw button text centered. */ + float text_width = txt_renderer->get_text_width(button.label.c_str(), 1.0f); + float text_x = button.rect.x + (button.rect.w - text_width) / 2.0f; + txt_renderer->render_text(button.label.c_str(), text_x, button.rect.y + 18, 1.0f, text_color); } } diff --git a/common/src/machine.h b/common/src/machine.h index 2ce282e..ebad545 100644 --- a/common/src/machine.h +++ b/common/src/machine.h @@ -15,7 +15,6 @@ public: vfs_root(nullptr), is_vfs_a_copy(false) {} - /* TODO: Implement recursive deletion of vfs_root. */ ~Machine(void); /* Clean up VFS tree. */ uint32_t id;