From ec63d69cdbeb0098b6c726c4391696a712d383ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Yaroslav=20de=20la=20Pe=C3=B1a=20Smirnov?= Date: Sat, 21 Sep 2024 20:58:44 +0300 Subject: gl: make viewport resize with window Keep it square, but make it so the sides of the viewport are equal to shorter side of the window. --- src/main.zig | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) (limited to 'src') diff --git a/src/main.zig b/src/main.zig index 564fe37..7543090 100644 --- a/src/main.zig +++ b/src/main.zig @@ -247,11 +247,28 @@ pub const Window = struct { /// Resize this window fn resize(win: *Window, width: i32, height: i32) !void { - win.height = if (height != 0) height else win.height; - win.width = if (width != 0) width else win.width; - if (width == 0 and height == 0) return; + const new_height = if (height != 0) height else win.height; + const new_width = if (width != 0) width else win.width; + if (win.width == new_width and win.height == new_height) return; + + win.width = new_width; + win.height = new_height; win.egl_win.resize(win.width, win.height, 0, 0); - c.glViewport(@divTrunc(win.width, 2) - 160, @divTrunc(win.height, 2) - 160, 320, 320); + if (win.width < win.height) { + c.glViewport( + 0, + @divTrunc(win.height, 2) - @divTrunc(win.width, 2), + win.width, + win.width, + ); + } else { + c.glViewport( + @divTrunc(win.width, 2) - @divTrunc(win.height, 2), + 0, + win.height, + win.height, + ); + } } /// XDG Surface callbacks -- cgit v1.2.3