From adf5332969356edcf0ddfde1eb72579c7a56a248 Mon Sep 17 00:00:00 2001 From: Silver Date: Thu, 7 Apr 2022 02:33:56 +0100 Subject: [PATCH] app: add resize callback function --- examples/main.zig | 2 +- src/main.zig | 17 ++++++++++++++--- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/examples/main.zig b/examples/main.zig index 37c09fb6..0bdb3c7c 100644 --- a/examples/main.zig +++ b/examples/main.zig @@ -75,7 +75,7 @@ pub fn main() !void { vs_module.release(); fs_module.release(); - try app.run(frame); + try app.run(.{ .frame = frame }); } const FrameParams = struct { diff --git a/src/main.zig b/src/main.zig index 283086d0..34da037f 100644 --- a/src/main.zig +++ b/src/main.zig @@ -197,9 +197,17 @@ pub fn App(comptime Context: type, comptime config: AppConfig) type { }; } - const FrameFunc = fn (app: *Self, ctx: Context) error{OutOfMemory}!void; + const Funcs = struct { + // Run once per frame + frame: fn (app: *Self, ctx: Context) error{OutOfMemory}!void, + // Run once at the start, and whenever the swapchain is recreated + resize: ?fn (app: *Self, ctx: Context, width: u32, height: u32) error{OutOfMemory}!void = null, + }; - pub fn run(app: *Self, frame: FrameFunc) !void { + pub fn run(app: *Self, funcs: Funcs) !void { + if (app.swap_chain != null and funcs.resize != null) { + try funcs.resize.?(app, app.context, app.current_desc.width, app.current_desc.height); + } while (!app.window.shouldClose()) { try glfw.pollEvents(); @@ -217,10 +225,13 @@ pub fn App(comptime Context: type, comptime config: AppConfig) type { app.target_desc.width, app.target_desc.height, ); + if (funcs.resize) |f| { + try f(app, app.context, app.target_desc.width, app.target_desc.height); + } app.current_desc = app.target_desc; } - try frame(app, app.context); + try funcs.frame(app, app.context); std.time.sleep(16 * std.time.ns_per_ms); // TODO: this is very naive } }