examples: texture-light: don't crash on window resize (recreate depth buffer)

Signed-off-by: Stephen Gutekanst <stephen@hexops.com>
This commit is contained in:
Stephen Gutekanst 2022-04-21 04:05:53 -07:00 committed by Stephen Gutekanst
parent 7daff572f3
commit e7609c76a2

View file

@ -44,9 +44,6 @@ pub fn main() !void {
// }.callback); // }.callback);
try app.window.setSizeLimits(.{ .width = 20, .height = 20 }, .{ .width = null, .height = null }); try app.window.setSizeLimits(.{ .width = 20, .height = 20 }, .{ .width = null, .height = null });
const queue = app.device.getQueue();
const device = app.device;
const eye = vec3(5.0, 7.0, 5.0); const eye = vec3(5.0, 7.0, 5.0);
const target = vec3(0.0, 0.0, 0.0); const target = vec3(0.0, 0.0, 0.0);
@ -54,11 +51,12 @@ pub fn main() !void {
const aspect_ratio = @intToFloat(f32, size.width) / @intToFloat(f32, size.height); const aspect_ratio = @intToFloat(f32, size.width) / @intToFloat(f32, size.height);
ctx.* = FrameParams{ ctx.* = FrameParams{
.queue = queue, .queue = app.device.getQueue(),
.cube = Cube.init(app), .cube = Cube.init(app),
.light = Light.init(app), .light = Light.init(app),
.depth = Texture.depth(device, size.width, size.height), .depth = Texture.depth(app.device, size.width, size.height),
.camera = Camera.init(device, eye, target, vec3(0.0, 1.0, 0.0), aspect_ratio, 45.0, 0.1, 100.0), .depth_size = size,
.camera = Camera.init(app.device, eye, target, vec3(0.0, 1.0, 0.0), aspect_ratio, 45.0, 0.1, 100.0),
}; };
try app.run(.{ .frame = frame }); try app.run(.{ .frame = frame });
@ -70,6 +68,7 @@ const FrameParams = struct {
camera: Camera, camera: Camera,
light: Light, light: Light,
depth: Texture, depth: Texture,
depth_size: glfw.Window.Size,
keys: u8 = 0, keys: u8 = 0,
const up: u8 = 0b0001; const up: u8 = 0b0001;
@ -79,6 +78,12 @@ const FrameParams = struct {
}; };
fn frame(app: *App, params: *FrameParams) !void { fn frame(app: *App, params: *FrameParams) !void {
// If window is resized, recreate depth buffer otherwise we cannot use it.
const size = app.window.getFramebufferSize() catch unreachable; // TODO: return type inference can't handle this
if (size.width != params.depth_size.width or size.height != params.depth_size.height) {
params.depth = Texture.depth(app.device, size.width, size.height);
params.depth_size = size;
}
// move camera // move camera
const speed = zm.f32x4s(0.2); const speed = zm.f32x4s(0.2);