From 7aa6c6a70fba7d0cfc09f45f40f7e479d7dfd506 Mon Sep 17 00:00:00 2001 From: Daniel Gibson Date: Sun, 2 Jun 2024 16:50:28 +0200 Subject: [PATCH] "Properly" scale ImGui font by reloading font with scaled size --- neo/sys/sys_imgui.cpp | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/neo/sys/sys_imgui.cpp b/neo/sys/sys_imgui.cpp index b5957f82..665f0307 100644 --- a/neo/sys/sys_imgui.cpp +++ b/neo/sys/sys_imgui.cpp @@ -203,11 +203,6 @@ float GetScale() void SetScale( float scale ) { - ImGuiIO& io = ImGui::GetIO(); - float realScale = (scale < 0.0f) ? GetDefaultScale() : scale; - io.FontGlobalScale = realScale; - // TODO: could instead set fontsize to 18.0f * scale - // (io.Fonts->ClearFonts() and then add again with new size - but must be done before NewFrame() / after EndFrame()) imgui_scale.SetFloat( scale ); } @@ -240,11 +235,8 @@ bool Init(void* _sdlWindow, void* sdlGlContext) ImGui::StyleColorsDark(); } - ImFontConfig fontCfg; - strcpy(fontCfg.Name, "ProggyVector"); - ImFont* font = io.Fonts->AddFontFromMemoryCompressedTTF(ProggyVector_compressed_data, ProggyVector_compressed_size, 18.0f, nullptr); - SetScale( GetScale() ); + imgui_scale.SetModified(); // so NewFrame() will load the scaled font // Setup Platform/Renderer backends if ( ! ImGui_ImplSDL2_InitForOpenGL( sdlWindow, sdlGlContext ) ) { @@ -322,6 +314,18 @@ void NewFrame() framesAfterAllWindowsClosed = 0; } + if( imgui_scale.IsModified() ) { + imgui_scale.ClearModified(); + ImGuiIO& io = ImGui::GetIO(); + io.Fonts->Clear(); + ImGui_ImplOpenGL2_DestroyFontsTexture(); + ImFontConfig fontCfg; + strcpy( fontCfg.Name, "ProggyVector" ); + float fontSize = 18.0f * GetScale(); + float fontSizeInt = roundf( fontSize ); // font sizes are supposed to be rounded to integers + ImFont* font = io.Fonts->AddFontFromMemoryCompressedTTF(ProggyVector_compressed_data, ProggyVector_compressed_size, fontSizeInt, nullptr); + } + // Start the Dear ImGui frame ImGui_ImplOpenGL2_NewFrame();