From 99635ec12eae5e698b975fa10b5eed7b1a48c8ed Mon Sep 17 00:00:00 2001 From: David Vanderson Date: Thu, 18 Dec 2025 13:04:19 -0500 Subject: [PATCH] raylib: hidpi fixes Updates our raylib dependency, a lot of hidpi bugs got fixed in raylib. We did have to bring back converting between dvui.Rect and raylib Rects. --- build.zig.zon | 4 ++-- examples/raylib-ontop.zig | 13 ++++------- examples/raylib-standalone.zig | 4 +++- src/backends/raylib-c.zig | 40 ++++++++++++++++++++++++++++++---- 4 files changed, 45 insertions(+), 16 deletions(-) diff --git a/build.zig.zon b/build.zig.zon index 9b29699a..60553784 100644 --- a/build.zig.zon +++ b/build.zig.zon @@ -21,8 +21,8 @@ .lazy = true, }, .raylib = .{ - .url = "git+https://github.com/raysan5/raylib?ref=master#12ce106661bc9c8f74ea3f3069291e5fc4e7d50a", - .hash = "raylib-5.6.0-dev-whq8uECwAAUfjtGTyP_uENUua4I_wf8r_-k3p0Wq3Bah", + .url = "git+https://github.com/raysan5/raylib?ref=master#720dd22491cdc4a29ec0ea87b572d9380872198c", + .hash = "raylib-5.6.0-dev-whq8uMVQCgVTgRxG3REWgBBSi2BR-nf4bq6eqANxImt7", .lazy = true, }, //used in the raylib-ontop example diff --git a/examples/raylib-ontop.zig b/examples/raylib-ontop.zig index b65644c1..efc3c74c 100644 --- a/examples/raylib-ontop.zig +++ b/examples/raylib-ontop.zig @@ -24,6 +24,7 @@ pub fn main() !void { // create OS window directly with raylib ray.SetConfigFlags(ray.FLAG_WINDOW_RESIZABLE); + ray.SetConfigFlags(ray.FLAG_WINDOW_HIGHDPI); ray.SetConfigFlags(ray.FLAG_VSYNC_HINT); ray.InitWindow(800, 600, "DVUI Raylib Ontop Example"); defer ray.CloseWindow(); @@ -59,7 +60,7 @@ pub fn main() !void { } // if dvui widgets might not cover the whole window, then need to clear // the previous frame's render - ray.ClearBackground(RaylibBackend.dvuiColorToRaylib(dvui.Color.black)); + ray.ClearBackground(RaylibBackend.dvuiColorToRaylib(dvui.Color.gray)); { var b = dvui.box(@src(), .{}, .{ .expand = .horizontal, .margin = .{ .x = 10 } }); @@ -103,15 +104,9 @@ fn colorPicker(result: *dvui.Color) void { var overlay = dvui.overlay(@src(), .{ .min_size_content = .{ .w = 100, .h = 100 } }); defer overlay.deinit(); - const bounds = overlay.data().contentRectScale().r; - const ray_bounds: ray.Rectangle = .{ - .x = bounds.x, - .y = bounds.y, - .width = bounds.w, - .height = bounds.h, - }; + const bounds = RaylibBackend.dvuiRectToRaylib(overlay.data().contentRectScale().r); var c_color: ray.Color = RaylibBackend.dvuiColorToRaylib(result.*); - _ = ray.GuiColorPicker(ray_bounds, "Pick Color", &c_color); + _ = ray.GuiColorPicker(bounds, "Pick Color", &c_color); result.* = RaylibBackend.raylibColorToDvui(c_color); } diff --git a/examples/raylib-standalone.zig b/examples/raylib-standalone.zig index 22ba71ba..05de0239 100644 --- a/examples/raylib-standalone.zig +++ b/examples/raylib-standalone.zig @@ -199,7 +199,9 @@ fn dvui_frame() bool { // rs.r is the pixel rectangle, rs.s is the scale factor (like for // hidpi screens or display scaling) - const r = rs.r; + // raylib multiplies everything internally by the monitor scale, so we + // have to divide by that + const r = RaylibBackend.dvuiRectToRaylib(rs.r); const s = rs.s / dvui.windowNaturalScale(); c.DrawText("Congrats! You created your first window!", @intFromFloat(r.x + 10 * s), @intFromFloat(r.y + 10 * s), @intFromFloat(20 * s), c.LIGHTGRAY); } diff --git a/src/backends/raylib-c.zig b/src/backends/raylib-c.zig index 786e552a..9334701b 100644 --- a/src/backends/raylib-c.zig +++ b/src/backends/raylib-c.zig @@ -217,13 +217,17 @@ pub fn drawClippedTriangles(self: *RaylibBackend, texture: ?dvui.Texture, vtx: [ if (clipr_in) |clip_rect| { if (self.fb_width == null) { + // clip_rect is in pixels, but raylib multiplies by GetWindowScaleDPI(), so we + // have to divide by that here + const clipr = dvuiRectToRaylib(clip_rect); c.BeginScissorMode( - @intFromFloat(clip_rect.x), - @intFromFloat(clip_rect.y), - @intFromFloat(clip_rect.w), - @intFromFloat(clip_rect.h), + @intFromFloat(clipr.x), + @intFromFloat(clipr.y), + @intFromFloat(clipr.width), + @intFromFloat(clipr.height), ); } else { + // raylib does NOT multiply by the window scale when targeting a texture // need to swap y c.BeginScissorMode( @intFromFloat(clip_rect.x), @@ -861,6 +865,34 @@ pub fn dvuiColorToRaylib(color: dvui.Color) c.Color { return c.Color{ .r = @intCast(color.r), .b = @intCast(color.b), .g = @intCast(color.g), .a = @intCast(color.a) }; } +/// Divides by the scaling of the monitor, only needed when rendering to +/// the main render target. No conversion is needed when rendering to +/// textures. +pub fn dvuiRectToRaylib(rect: dvui.Rect.Physical) c.Rectangle { + // We have to check this flag, because GetWindowScaleDPI() will report 1.25 + // (if the display scale is 125%), but the flag determines whether raylib + // multiplies by it internally. + if (c.IsWindowState(c.FLAG_WINDOW_HIGHDPI)) { + // raylib multiplies everything internally by the monitor scale, so we + // have to divide by that + const s = c.GetWindowScaleDPI(); + return c.Rectangle{ + .x = rect.x / s.x, + .y = rect.y / s.y, + .width = rect.w / s.x, + .height = rect.h / s.y, + }; + } else { + // In this case raylib does no changes. + return c.Rectangle{ + .x = rect.x, + .y = rect.y, + .width = rect.w, + .height = rect.h, + }; + } +} + pub fn EndDrawingWaitEventTimeout(_: *RaylibBackend, timeout_micros: u32) void { if (timeout_micros == std.math.maxInt(u32)) { // wait no timeout -- knotmirror 0.0.1