From f1b1da60eaa0a9accf33ba0919511a1295b8bbab Mon Sep 17 00:00:00 2001 From: Quantum Date: Thu, 29 Jul 2021 21:46:50 -0400 Subject: [PATCH] [client] imgui: improve method for high DPI We now give ImGui the true logical size of the window and tell it to scale the framebuffer. To fix the blurry fonts, we continue to load fonts at the scale necessary for the DPI and use FontGlobalScale to shrink the fonts back to the logical size. The font rectangle is then expanded by the framebuffer scaling, resulting in good text rendering. This method has the advantage of not messing up the sizes of resizable overlays when moving across monitors. --- client/src/app.c | 10 +++++++++- client/src/main.c | 12 +++++++----- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/client/src/app.c b/client/src/app.c index a3cae879..c6be9845 100644 --- a/client/src/app.c +++ b/client/src/app.c @@ -70,7 +70,7 @@ void app_updateCursorPos(double x, double y) g_cursor.valid = true; if (g_state.overlayInput) - g_state.io->MousePos = (ImVec2) { x * g_state.windowScale, y * g_state.windowScale }; + g_state.io->MousePos = (ImVec2) { x, y }; } void app_handleFocusEvent(bool focused) @@ -723,6 +723,14 @@ int app_renderOverlay(struct Rect * rects, int maxRects) const int written = overlay->ops->render(overlay->udata, false, buffer, MAX_OVERLAY_RECTS); + for (int i = 0; i < written; ++i) + { + buffer[i].x *= g_state.windowScale; + buffer[i].y *= g_state.windowScale; + buffer[i].w *= g_state.windowScale; + buffer[i].h *= g_state.windowScale; + } + // It is an error to run out of rectangles, because we will not be able to // correctly calculate the damage of the next frame. assert(written >= 0); diff --git a/client/src/main.c b/client/src/main.c index c30c7abe..f39623e4 100644 --- a/client/src/main.c +++ b/client/src/main.c @@ -181,12 +181,14 @@ static int renderThread(void * unused) if (resize) { g_state.io->DisplaySize = (ImVec2) { - .x = g_state.windowW * g_state.windowScale, - .y = g_state.windowH * g_state.windowScale + .x = g_state.windowW, + .y = g_state.windowH, }; - - imGuiResetStyle(); - ImGuiStyle_ScaleAllSizes(g_state.style, g_state.windowScale); + g_state.io->DisplayFramebufferScale = (ImVec2) { + .x = g_state.windowScale, + .y = g_state.windowScale, + }; + g_state.io->FontGlobalScale = 1.0f / g_state.windowScale; ImFontAtlas_Clear(g_state.io->Fonts); ImFontAtlas_AddFontFromFileTTF(g_state.io->Fonts, g_state.fontName,