From e42554e6fd72dc361a5ed3ebd655e1dfb5d81ef0 Mon Sep 17 00:00:00 2001 From: Tyler Date: Thu, 14 May 2026 17:01:15 -0700 Subject: [PATCH] Enable raylib 6 HiDPI/HighDPI support for proper font and UI scaling Add FLAG_WINDOW_HIGHDPI to enable raylib's automatic framebuffer scaling on HiDPI displays. Layout coordinates use logical screen size only, so UI stays proportional to window size while text and graphics render at full framebuffer resolution for crisp output on any monitor. Also fix hardcoded pixel offsets in selection bounds rectangle to use selScale consistently. --- src/spectrogram.c | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/spectrogram.c b/src/spectrogram.c index ac217d9..c0026f5 100644 --- a/src/spectrogram.c +++ b/src/spectrogram.c @@ -38,7 +38,12 @@ #define BASE_WIDTH 1280 #define BASE_HEIGHT 800 -// Get uniform scale factor based on current window size +// Base resolution for proportional UI scaling. +// GetUIScale() uses logical screen (not framebuffer) dimensions so that +// layout stays based on window size alone. FLAG_WINDOW_HIGHDPI makes +// BeginDrawing() render to the framebuffer at the correct resolution, so +// every 1px drawn in layout coordinates automatically maps to the right +// physical size on any monitor. static float GetUIScale(void) { float scaleX = (float)GetScreenWidth() / BASE_WIDTH; @@ -1269,7 +1274,7 @@ static bool UpdateSlider(Rectangle bounds, float* value) int main(int argc, char* argv[]) { - SetConfigFlags(FLAG_VSYNC_HINT | FLAG_WINDOW_RESIZABLE); + SetConfigFlags(FLAG_VSYNC_HINT | FLAG_WINDOW_RESIZABLE | FLAG_WINDOW_HIGHDPI); InitWindow(1280, 800, "Spectrogram Viewer"); SetTargetFPS(60); SetTraceLogLevel(LOG_WARNING); // Suppress INFO texture logs @@ -1284,7 +1289,9 @@ int main(int argc, char* argv[]) SearchAndSetResourceDir("resources"); - // Load TTF font for crisp text at any scale + // Load TTF font at a fixed base size. Scaling is handled uniformly by + // GetUIScale() for both layout and DrawTextScaled(), so the font scales + // naturally with the window size on any DPI monitor. mainFont = LoadFontEx("fonts/DejaVuSansMono.ttf", 16, 0, 0); if (mainFont.texture.id == 0) { TraceLog(LOG_WARNING, "Failed to load TTF font, using default bitmap font"); @@ -1533,11 +1540,11 @@ int main(int argc, char* argv[]) float selTopMargin = 50 * selScale; float selBottomMargin = 10 * selScale; - Rectangle selBounds = { - selSidebarWidth + selFreqLabelWidth, - selTopMargin, - GetScreenWidth() - selSidebarWidth - selFreqLabelWidth - selVScrollbarWidth - 20, - GetScreenHeight() - selTopMargin - selBottomMargin - selLabelHeight - selScrollbarHeight - 10 + Rectangle selBounds = { + selSidebarWidth + selFreqLabelWidth, + selTopMargin, + GetScreenWidth() - selSidebarWidth - selFreqLabelWidth - selVScrollbarWidth - 20.0f * selScale, + GetScreenHeight() - selTopMargin - selBottomMargin - selLabelHeight - selScrollbarHeight - 10.0f * selScale }; Vector2 mousePos = GetMousePosition();