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.
This commit is contained in:
2026-05-14 17:01:15 -07:00
parent 4331d010e5
commit e42554e6fd
+15 -8
View File
@@ -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();