Added setKeyboardCallback and update variants

This commit is contained in:
2022-01-10 15:38:19 -07:00
parent a816791914
commit ed6ab7c92a
2 changed files with 172 additions and 11 deletions

View File

@ -41,6 +41,7 @@ const State = struct {
},
rectangle_width: f32 = @intToFloat(f32, Width) / 3.0,
rectangle_height: f32 = @intToFloat(f32, Height) / 3.0,
paused: bool = false,
pub fn init(alloc: std.mem.Allocator, period: f64) !State {
var timer = try alloc.create(mfb.Timer);
@ -195,6 +196,15 @@ fn handleMouseScroll(win: mfb.Window(State), key_mod: mfb.KeyMod, delta_x: f32,
}
}
fn handleKeyboard(win: mfb.Window(State), key: mfb.Key, _: mfb.KeyMod, is_pressed: bool) callconv(.C) void {
if (key == mfb.Key.escape and is_pressed) {
win.close();
} else if (key == mfb.Key.space and is_pressed) {
var state = win.getUserData().?;
state.*.paused = !state.*.paused;
}
}
pub fn main() !void {
var gp_allocator = std.heap.GeneralPurposeAllocator(.{}){};
const alloc = gp_allocator.allocator();
@ -213,6 +223,7 @@ pub fn main() !void {
win.setMouseButtonCallback(handleMouseButton);
win.setMouseMoveCallback(handleMouseMove);
win.setMouseScrollCallback(handleMouseScroll);
win.setKeyboardCallback(handleKeyboard);
var buf = try Buffer.init(alloc, Width, Height);
defer buf.deinit();
@ -221,6 +232,10 @@ pub fn main() !void {
while (win.waitSync()) {
state.render(buf);
// TODO Handle mfb state
_ = win.update(buf.slice) catch unreachable;
if (state.paused) {
_ = win.updateEvents() catch unreachable;
} else {
_ = win.update(buf.slice) catch unreachable;
}
}
}