Skip to content

Commit

Permalink
raylib: fix clip when targetting texture
Browse files Browse the repository at this point in the history
When targetting a texture, raylib does not multiply the clipping rect by the
window scale.
  • Loading branch information
david-vanderson committed Dec 5, 2024
1 parent 5abff13 commit 88fa1e1
Showing 1 changed file with 12 additions and 9 deletions.
21 changes: 12 additions & 9 deletions src/backends/raylib_backend.zig
Original file line number Diff line number Diff line change
Expand Up @@ -195,23 +195,26 @@ pub fn drawClippedTriangles(self: *RaylibBackend, texture: ?*anyopaque, vtx: []c
c.rlDrawRenderBatchActive();

if (clipr_in) |clip_rect| {
// clipr is in pixels, but raylib multiplies by GetWindowScaleDPI(), so we
// have to divide by that here
const clipr = dvuiRectToRaylib(clip_rect);
if (self.fb_width == null) {
// clipr is in pixels, but raylib multiplies by GetWindowScaleDPI(), so we
// have to divide by that here
const clipr = dvuiRectToRaylib(clip_rect);

// figure out how much we are losing by truncating x and y, need to add that back to w and h
const clipx: c_int = @intFromFloat(clipr.x);
const clipw: c_int = @intFromFloat(@ceil(clipr.width + clipr.x - @floor(clipr.x)));
// figure out how much we are losing by truncating x and y, need to add that back to w and h
const clipx: c_int = @intFromFloat(clipr.x);
const clipw: c_int = @intFromFloat(@ceil(clipr.width + clipr.x - @floor(clipr.x)));

if (self.fb_width == null) {
const clipy: c_int = @intFromFloat(clipr.y);
const cliph: c_int = @max(0, @as(c_int, @intFromFloat(@ceil(clipr.height + clipr.y - @floor(clipr.y)))));
c.BeginScissorMode(clipx, clipy, clipw, cliph);
} else {
// raylib does NOT multiply by the window scale when targeting a texture
const clipx: c_int = @intFromFloat(clip_rect.x);
const clipw: c_int = @intFromFloat(@ceil(clip_rect.w + clip_rect.x - @floor(clip_rect.x)));
// need to swap y
const ry: f32 = @as(f32, @floatFromInt(self.fb_height.?)) / dvui.windowNaturalScale() - clipr.y - clipr.height;
const ry: f32 = @as(f32, @floatFromInt(self.fb_height.?)) - clip_rect.y - clip_rect.h;
const y: c_int = @intFromFloat(ry);
const h: c_int = @intFromFloat(@ceil(clipr.height + ry - @floor(ry)));
const h: c_int = @intFromFloat(@ceil(clip_rect.h + ry - @floor(ry)));
c.BeginScissorMode(clipx, y, clipw, h);
}
}
Expand Down

0 comments on commit 88fa1e1

Please sign in to comment.