diff --git a/examples/advanced-gen-texture-light/main.zig b/examples/advanced-gen-texture-light/main.zig index b0e8ed51..a181fde7 100755 --- a/examples/advanced-gen-texture-light/main.zig +++ b/examples/advanced-gen-texture-light/main.zig @@ -33,7 +33,6 @@ const Dir = struct { }; pub fn init(app: *App, engine: *mach.Engine) !void { - engine.core.setKeyCallback(keyCallback); // todo // engine.core.internal.window.setKeyCallback(struct { // fn callback(window: glfw.Window, key: glfw.Key, scancode: i32, action: glfw.Action, mods: glfw.Mods) void { @@ -67,6 +66,42 @@ pub fn deinit(app: *App, _: *mach.Engine) void { } pub fn update(app: *App, engine: *mach.Engine) !bool { + while (engine.core.pollEvent()) |event| { + switch (event) { + .key_press => |ev| switch (ev.key) { + .q, .escape, .space => engine.core.setShouldClose(true), + .w, .up => { + app.keys |= Dir.up; + }, + .s, .down => { + app.keys |= Dir.down; + }, + .a, .left => { + app.keys |= Dir.left; + }, + .d, .right => { + app.keys |= Dir.right; + }, + else => {}, + }, + .key_release => |ev| switch (ev.key) { + .w, .up => { + app.keys &= ~Dir.up; + }, + .s, .down => { + app.keys &= ~Dir.down; + }, + .a, .left => { + app.keys &= ~Dir.left; + }, + .d, .right => { + app.keys &= ~Dir.right; + }, + else => {}, + }, + } + } + // move camera const speed = zm.f32x4s(@floatCast(f32, engine.delta_time * 5)); const fwd = zm.normalize3(app.camera.target - app.camera.eye); @@ -872,40 +907,3 @@ const Instance = struct { return zm.mul(zm.quatToMat(self.rotation), zm.translationV(self.position)); } }; - -fn keyCallback(app: *App, engine: *mach.Engine, key: mach.Key, action: mach.Action) void { - if (action == .press) { - switch (key) { - .q, .escape, .space => engine.core.setShouldClose(true), - .w, .up => { - app.keys |= Dir.up; - }, - .s, .down => { - app.keys |= Dir.down; - }, - .a, .left => { - app.keys |= Dir.left; - }, - .d, .right => { - app.keys |= Dir.right; - }, - else => {}, - } - } else if (action == .release) { - switch (key) { - .w, .up => { - app.keys &= ~Dir.up; - }, - .s, .down => { - app.keys &= ~Dir.down; - }, - .a, .left => { - app.keys &= ~Dir.left; - }, - .d, .right => { - app.keys &= ~Dir.right; - }, - else => {}, - } - } -} diff --git a/examples/fractal-cube/main.zig b/examples/fractal-cube/main.zig index f122dcf2..053d2802 100755 --- a/examples/fractal-cube/main.zig +++ b/examples/fractal-cube/main.zig @@ -41,16 +41,6 @@ bgl: gpu.BindGroupLayout, pub fn init(app: *App, engine: *mach.Engine) !void { timer = try mach.Timer.start(); - engine.core.setKeyCallback(struct { - fn callback(_: *App, eng: *mach.Engine, key: mach.Key, action: mach.Action) void { - if (action == .press) { - switch (key) { - .space => eng.core.setShouldClose(true), - else => {}, - } - } - } - }.callback); try engine.core.setSizeLimits(.{ .width = 20, .height = 20 }, .{ .width = null, .height = null }); const vs_module = engine.gpu_driver.device.createShaderModule(&.{ @@ -229,6 +219,16 @@ pub fn deinit(app: *App, _: *mach.Engine) void { } pub fn update(app: *App, engine: *mach.Engine) !bool { + while (engine.core.pollEvent()) |event| { + switch (event) { + .key_press => |ev| { + if (ev.key == .space) + engine.core.setShouldClose(true); + }, + else => {}, + } + } + const cube_view = app.cube_texture_view_render; const back_buffer_view = engine.gpu_driver.swap_chain.?.getCurrentTextureView(); diff --git a/examples/gkurve/main.zig b/examples/gkurve/main.zig index 60ef547e..2065087c 100644 --- a/examples/gkurve/main.zig +++ b/examples/gkurve/main.zig @@ -41,16 +41,6 @@ bind_group: gpu.BindGroup, vertices_len: u32, pub fn init(app: *App, engine: *mach.Engine) !void { - engine.core.setKeyCallback(struct { - fn callback(_: *App, eng: *mach.Engine, key: mach.Key, action: mach.Action) void { - if (action == .press) { - switch (key) { - .space => eng.core.setShouldClose(true), - else => {}, - } - } - } - }.callback); try engine.core.setSizeLimits(.{ .width = 20, .height = 20 }, .{ .width = null, .height = null }); const vs_module = engine.gpu_driver.device.createShaderModule(&.{ @@ -207,6 +197,16 @@ pub fn deinit(app: *App, _: *mach.Engine) void { } pub fn update(app: *App, engine: *mach.Engine) !bool { + while (engine.core.pollEvent()) |event| { + switch (event) { + .key_press => |ev| { + if (ev.key == .space) + engine.core.setShouldClose(true); + }, + else => {}, + } + } + const back_buffer_view = engine.gpu_driver.swap_chain.?.getCurrentTextureView(); const color_attachment = gpu.RenderPassColorAttachment{ .view = back_buffer_view, diff --git a/examples/instanced-cube/main.zig b/examples/instanced-cube/main.zig index b562a18d..0f87115f 100755 --- a/examples/instanced-cube/main.zig +++ b/examples/instanced-cube/main.zig @@ -23,16 +23,6 @@ const App = @This(); pub fn init(app: *App, engine: *mach.Engine) !void { timer = try mach.Timer.start(); - engine.core.setKeyCallback(struct { - fn callback(_: *App, eng: *mach.Engine, key: mach.Key, action: mach.Action) void { - if (action == .press) { - switch (key) { - .space => eng.core.setShouldClose(true), - else => {}, - } - } - } - }.callback); try engine.core.setSizeLimits(.{ .width = 20, .height = 20 }, .{ .width = null, .height = null }); const vs_module = engine.gpu_driver.device.createShaderModule(&.{ @@ -148,6 +138,16 @@ pub fn deinit(app: *App, _: *mach.Engine) void { } pub fn update(app: *App, engine: *mach.Engine) !bool { + while (engine.core.pollEvent()) |event| { + switch (event) { + .key_press => |ev| { + if (ev.key == .space) + engine.core.setShouldClose(true); + }, + else => {}, + } + } + const back_buffer_view = engine.gpu_driver.swap_chain.?.getCurrentTextureView(); const color_attachment = gpu.RenderPassColorAttachment{ .view = back_buffer_view, diff --git a/examples/rotating-cube/main.zig b/examples/rotating-cube/main.zig index 520edf1c..1a4cfc87 100755 --- a/examples/rotating-cube/main.zig +++ b/examples/rotating-cube/main.zig @@ -23,17 +23,6 @@ bind_group: gpu.BindGroup, pub fn init(app: *App, engine: *mach.Engine) !void { timer = try mach.Timer.start(); - // TODO: higher level input handlers - engine.core.setKeyCallback(struct { - fn callback(_: *App, eng: *mach.Engine, key: mach.Key, action: mach.Action) void { - if (action == .press) { - switch (key) { - .space => eng.core.setShouldClose(true), - else => {}, - } - } - } - }.callback); try engine.core.setSizeLimits(.{ .width = 20, .height = 20 }, .{ .width = null, .height = null }); const vs_module = engine.gpu_driver.device.createShaderModule(&.{ @@ -157,6 +146,16 @@ pub fn deinit(app: *App, _: *mach.Engine) void { } pub fn update(app: *App, engine: *mach.Engine) !bool { + while (engine.core.pollEvent()) |event| { + switch (event) { + .key_press => |ev| { + if (ev.key == .space) + engine.core.setShouldClose(true); + }, + else => {}, + } + } + const back_buffer_view = engine.gpu_driver.swap_chain.?.getCurrentTextureView(); const color_attachment = gpu.RenderPassColorAttachment{ .view = back_buffer_view, diff --git a/examples/textured-cube/main.zig b/examples/textured-cube/main.zig index 6b88556e..4f4ccf66 100644 --- a/examples/textured-cube/main.zig +++ b/examples/textured-cube/main.zig @@ -26,16 +26,6 @@ const App = @This(); pub fn init(app: *App, engine: *mach.Engine) !void { timer = try mach.Timer.start(); - engine.core.setKeyCallback(struct { - fn callback(_: *App, eng: *mach.Engine, key: mach.Key, action: mach.Action) void { - if (action == .press) { - switch (key) { - .space => eng.core.setShouldClose(true), - else => {}, - } - } - } - }.callback); try engine.core.setSizeLimits(.{ .width = 20, .height = 20 }, .{ .width = null, .height = null }); const vs_module = engine.gpu_driver.device.createShaderModule(&.{ @@ -186,6 +176,16 @@ pub fn deinit(app: *App, _: *mach.Engine) void { } pub fn update(app: *App, engine: *mach.Engine) !bool { + while (engine.core.pollEvent()) |event| { + switch (event) { + .key_press => |ev| { + if (ev.key == .space) + engine.core.setShouldClose(true); + }, + else => {}, + } + } + const back_buffer_view = engine.gpu_driver.swap_chain.?.getCurrentTextureView(); const color_attachment = gpu.RenderPassColorAttachment{ .view = back_buffer_view, diff --git a/examples/two-cubes/main.zig b/examples/two-cubes/main.zig index c4d82ac2..6d535a96 100755 --- a/examples/two-cubes/main.zig +++ b/examples/two-cubes/main.zig @@ -24,16 +24,6 @@ const App = @This(); pub fn init(app: *App, engine: *mach.Engine) !void { timer = try mach.Timer.start(); - engine.core.setKeyCallback(struct { - fn callback(_: *App, eng: *mach.Engine, key: mach.Key, action: mach.Action) void { - if (action == .press) { - switch (key) { - .space => eng.core.setShouldClose(true), - else => {}, - } - } - } - }.callback); try engine.core.setSizeLimits(.{ .width = 20, .height = 20 }, .{ .width = null, .height = null }); const vs_module = engine.gpu_driver.device.createShaderModule(&.{ @@ -173,6 +163,16 @@ pub fn deinit(app: *App, _: *mach.Engine) void { } pub fn update(app: *App, engine: *mach.Engine) !bool { + while (engine.core.pollEvent()) |event| { + switch (event) { + .key_press => |ev| { + if (ev.key == .space) + engine.core.setShouldClose(true); + }, + else => {}, + } + } + const back_buffer_view = engine.gpu_driver.swap_chain.?.getCurrentTextureView(); const color_attachment = gpu.RenderPassColorAttachment{ .view = back_buffer_view,