From ac2f09d9cf0c54fdea442f7611b67e3304b12dbf Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Sun, 9 Jan 2022 15:31:52 -0700 Subject: [PATCH] Implemented setResizeCallback and setViewport --- lib/minifb/src/minifb.zig | 9 +++++++++ src/main.zig | 13 +++++++++++++ 2 files changed, 22 insertions(+) diff --git a/lib/minifb/src/minifb.zig b/lib/minifb/src/minifb.zig index 75fb0cc..61187fc 100644 --- a/lib/minifb/src/minifb.zig +++ b/lib/minifb/src/minifb.zig @@ -85,6 +85,15 @@ pub fn Window(comptime TUserData: type) type { pub fn setActiveCallback(self: Window(TUserData), callback: ActiveFunc) void { c.mfb_set_active_callback(self.cwin, @ptrCast(c.mfb_active_func, callback)); } + + pub const ResizeFunc = fn (win: Window(TUserData), width: i32, height: i32) callconv(.C) void; + pub fn setResizeCallback(self: Window(TUserData), callback: ResizeFunc) void { + c.mfb_set_resize_callback(self.cwin, @ptrCast(c.mfb_resize_func, callback)); + } + + pub fn setViewport(self: Window(TUserData), offset_x: u32, offset_y: u32, width: u32, height: u32) bool { + return c.mfb_set_viewport(self.cwin, offset_x, offset_y, width, height); + } }; } diff --git a/src/main.zig b/src/main.zig index c798de7..e82f824 100644 --- a/src/main.zig +++ b/src/main.zig @@ -106,6 +106,18 @@ fn handleActive(win: mfb.Window(State), isActive: bool) callconv(.C) void { } } +fn handleResize(win: mfb.Window(State), width: i32, height: i32) callconv(.C) void { + const did_set = win.setViewport( + @intCast(u32, width) / 4, + @intCast(u32, height) / 4, + @intCast(u32, width) / 2, + @intCast(u32, height) / 2, + ); + if (!did_set) { + std.log.warn("did not set viewport!", .{}); + } +} + pub fn main() !void { var gp_allocator = std.heap.GeneralPurposeAllocator(.{}){}; const alloc = gp_allocator.allocator(); @@ -118,6 +130,7 @@ pub fn main() !void { win.setUserData(&state); win.setActiveCallback(handleActive); + win.setResizeCallback(handleResize); var buf = try Buffer.init(alloc, Width, Height); defer buf.deinit();