examples: update to new event-based input method

This commit is contained in:
iddev5 2022-05-20 18:34:30 +05:30 committed by Stephen Gutekanst
parent f1e7c10fbb
commit 02b9048734
7 changed files with 96 additions and 99 deletions

View file

@ -33,7 +33,6 @@ const Dir = struct {
}; };
pub fn init(app: *App, engine: *mach.Engine) !void { pub fn init(app: *App, engine: *mach.Engine) !void {
engine.core.setKeyCallback(keyCallback);
// todo // todo
// engine.core.internal.window.setKeyCallback(struct { // engine.core.internal.window.setKeyCallback(struct {
// fn callback(window: glfw.Window, key: glfw.Key, scancode: i32, action: glfw.Action, mods: glfw.Mods) void { // 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 { 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 // move camera
const speed = zm.f32x4s(@floatCast(f32, engine.delta_time * 5)); const speed = zm.f32x4s(@floatCast(f32, engine.delta_time * 5));
const fwd = zm.normalize3(app.camera.target - app.camera.eye); 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)); 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 => {},
}
}
}

View file

@ -41,16 +41,6 @@ bgl: gpu.BindGroupLayout,
pub fn init(app: *App, engine: *mach.Engine) !void { pub fn init(app: *App, engine: *mach.Engine) !void {
timer = try mach.Timer.start(); 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 }); try engine.core.setSizeLimits(.{ .width = 20, .height = 20 }, .{ .width = null, .height = null });
const vs_module = engine.gpu_driver.device.createShaderModule(&.{ 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 { 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 cube_view = app.cube_texture_view_render;
const back_buffer_view = engine.gpu_driver.swap_chain.?.getCurrentTextureView(); const back_buffer_view = engine.gpu_driver.swap_chain.?.getCurrentTextureView();

View file

@ -41,16 +41,6 @@ bind_group: gpu.BindGroup,
vertices_len: u32, vertices_len: u32,
pub fn init(app: *App, engine: *mach.Engine) !void { 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 }); try engine.core.setSizeLimits(.{ .width = 20, .height = 20 }, .{ .width = null, .height = null });
const vs_module = engine.gpu_driver.device.createShaderModule(&.{ 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 { 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 back_buffer_view = engine.gpu_driver.swap_chain.?.getCurrentTextureView();
const color_attachment = gpu.RenderPassColorAttachment{ const color_attachment = gpu.RenderPassColorAttachment{
.view = back_buffer_view, .view = back_buffer_view,

View file

@ -23,16 +23,6 @@ const App = @This();
pub fn init(app: *App, engine: *mach.Engine) !void { pub fn init(app: *App, engine: *mach.Engine) !void {
timer = try mach.Timer.start(); 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 }); try engine.core.setSizeLimits(.{ .width = 20, .height = 20 }, .{ .width = null, .height = null });
const vs_module = engine.gpu_driver.device.createShaderModule(&.{ 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 { 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 back_buffer_view = engine.gpu_driver.swap_chain.?.getCurrentTextureView();
const color_attachment = gpu.RenderPassColorAttachment{ const color_attachment = gpu.RenderPassColorAttachment{
.view = back_buffer_view, .view = back_buffer_view,

View file

@ -23,17 +23,6 @@ bind_group: gpu.BindGroup,
pub fn init(app: *App, engine: *mach.Engine) !void { pub fn init(app: *App, engine: *mach.Engine) !void {
timer = try mach.Timer.start(); 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 }); try engine.core.setSizeLimits(.{ .width = 20, .height = 20 }, .{ .width = null, .height = null });
const vs_module = engine.gpu_driver.device.createShaderModule(&.{ 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 { 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 back_buffer_view = engine.gpu_driver.swap_chain.?.getCurrentTextureView();
const color_attachment = gpu.RenderPassColorAttachment{ const color_attachment = gpu.RenderPassColorAttachment{
.view = back_buffer_view, .view = back_buffer_view,

View file

@ -26,16 +26,6 @@ const App = @This();
pub fn init(app: *App, engine: *mach.Engine) !void { pub fn init(app: *App, engine: *mach.Engine) !void {
timer = try mach.Timer.start(); 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 }); try engine.core.setSizeLimits(.{ .width = 20, .height = 20 }, .{ .width = null, .height = null });
const vs_module = engine.gpu_driver.device.createShaderModule(&.{ 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 { 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 back_buffer_view = engine.gpu_driver.swap_chain.?.getCurrentTextureView();
const color_attachment = gpu.RenderPassColorAttachment{ const color_attachment = gpu.RenderPassColorAttachment{
.view = back_buffer_view, .view = back_buffer_view,

View file

@ -24,16 +24,6 @@ const App = @This();
pub fn init(app: *App, engine: *mach.Engine) !void { pub fn init(app: *App, engine: *mach.Engine) !void {
timer = try mach.Timer.start(); 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 }); try engine.core.setSizeLimits(.{ .width = 20, .height = 20 }, .{ .width = null, .height = null });
const vs_module = engine.gpu_driver.device.createShaderModule(&.{ 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 { 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 back_buffer_view = engine.gpu_driver.swap_chain.?.getCurrentTextureView();
const color_attachment = gpu.RenderPassColorAttachment{ const color_attachment = gpu.RenderPassColorAttachment{
.view = back_buffer_view, .view = back_buffer_view,