app: add resize callback function

This commit is contained in:
Silver 2022-04-07 02:33:56 +01:00 committed by Stephen Gutekanst
parent c62b5ba52f
commit adf5332969
2 changed files with 15 additions and 4 deletions

View file

@ -75,7 +75,7 @@ pub fn main() !void {
vs_module.release(); vs_module.release();
fs_module.release(); fs_module.release();
try app.run(frame); try app.run(.{ .frame = frame });
} }
const FrameParams = struct { const FrameParams = struct {

View file

@ -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()) { while (!app.window.shouldClose()) {
try glfw.pollEvents(); try glfw.pollEvents();
@ -217,10 +225,13 @@ pub fn App(comptime Context: type, comptime config: AppConfig) type {
app.target_desc.width, app.target_desc.width,
app.target_desc.height, 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; 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 std.time.sleep(16 * std.time.ns_per_ms); // TODO: this is very naive
} }
} }