From ed6ab7c92a81dd04ec550f97ae3a6914062e7447 Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Mon, 10 Jan 2022 15:38:19 -0700 Subject: [PATCH] Added setKeyboardCallback and update variants --- lib/minifb/src/minifb.zig | 166 +++++++++++++++++++++++++++++++++++--- src/main.zig | 17 +++- 2 files changed, 172 insertions(+), 11 deletions(-) diff --git a/lib/minifb/src/minifb.zig b/lib/minifb/src/minifb.zig index 7ad04b1..dc920ff 100644 --- a/lib/minifb/src/minifb.zig +++ b/lib/minifb/src/minifb.zig @@ -13,6 +13,133 @@ pub const Rgb = packed struct { _reserved: u8 = 0, }; +pub const Key = enum(c_int) { + unknown = c.KB_KEY_UNKNOWN, + + space = c.KB_KEY_SPACE, + apostrophe = c.KB_KEY_APOSTROPHE, + comma = c.KB_KEY_COMMA, + minus = c.KB_KEY_MINUS, + period = c.KB_KEY_PERIOD, + slash = c.KB_KEY_SLASH, + @"0" = c.KB_KEY_0, + @"1" = c.KB_KEY_1, + @"2" = c.KB_KEY_2, + @"3" = c.KB_KEY_3, + @"4" = c.KB_KEY_4, + @"5" = c.KB_KEY_5, + @"6" = c.KB_KEY_6, + @"7" = c.KB_KEY_7, + @"8" = c.KB_KEY_8, + @"9" = c.KB_KEY_9, + semicolon = c.KB_KEY_SEMICOLON, + equal = c.KB_KEY_EQUAL, + a = c.KB_KEY_A, + b = c.KB_KEY_B, + c = c.KB_KEY_C, + d = c.KB_KEY_D, + e = c.KB_KEY_E, + f = c.KB_KEY_F, + g = c.KB_KEY_G, + h = c.KB_KEY_H, + i = c.KB_KEY_I, + j = c.KB_KEY_J, + k = c.KB_KEY_K, + l = c.KB_KEY_L, + m = c.KB_KEY_M, + n = c.KB_KEY_N, + o = c.KB_KEY_O, + p = c.KB_KEY_P, + q = c.KB_KEY_Q, + r = c.KB_KEY_R, + s = c.KB_KEY_S, + t = c.KB_KEY_T, + u = c.KB_KEY_U, + v = c.KB_KEY_V, + w = c.KB_KEY_W, + x = c.KB_KEY_X, + y = c.KB_KEY_Y, + z = c.KB_KEY_Z, + left_bracket = c.KB_KEY_LEFT_BRACKET, + backslash = c.KB_KEY_BACKSLASH, + right_bracket = c.KB_KEY_RIGHT_BRACKET, + grave_accent = c.KB_KEY_GRAVE_ACCENT, + world_1 = c.KB_KEY_WORLD_1, + world_2 = c.KB_KEY_WORLD_2, + + escape = c.KB_KEY_ESCAPE, + enter = c.KB_KEY_ENTER, + tab = c.KB_KEY_TAB, + backspace = c.KB_KEY_BACKSPACE, + insert = c.KB_KEY_INSERT, + delete = c.KB_KEY_DELETE, + right = c.KB_KEY_RIGHT, + left = c.KB_KEY_LEFT, + down = c.KB_KEY_DOWN, + up = c.KB_KEY_UP, + page_up = c.KB_KEY_PAGE_UP, + page_down = c.KB_KEY_PAGE_DOWN, + home = c.KB_KEY_HOME, + end = c.KB_KEY_END, + caps_lock = c.KB_KEY_CAPS_LOCK, + scroll_lock = c.KB_KEY_SCROLL_LOCK, + num_lock = c.KB_KEY_NUM_LOCK, + print_screen = c.KB_KEY_PRINT_SCREEN, + pause = c.KB_KEY_PAUSE, + f1 = c.KB_KEY_F1, + f2 = c.KB_KEY_F2, + f3 = c.KB_KEY_F3, + f4 = c.KB_KEY_F4, + f5 = c.KB_KEY_F5, + f6 = c.KB_KEY_F6, + f7 = c.KB_KEY_F7, + f8 = c.KB_KEY_F8, + f9 = c.KB_KEY_F9, + f10 = c.KB_KEY_F10, + f11 = c.KB_KEY_F11, + f12 = c.KB_KEY_F12, + f13 = c.KB_KEY_F13, + f14 = c.KB_KEY_F14, + f15 = c.KB_KEY_F15, + f16 = c.KB_KEY_F16, + f17 = c.KB_KEY_F17, + f18 = c.KB_KEY_F18, + f19 = c.KB_KEY_F19, + f20 = c.KB_KEY_F20, + f21 = c.KB_KEY_F21, + f22 = c.KB_KEY_F22, + f23 = c.KB_KEY_F23, + f24 = c.KB_KEY_F24, + f25 = c.KB_KEY_F25, + kp_0 = c.KB_KEY_KP_0, + kp_1 = c.KB_KEY_KP_1, + kp_2 = c.KB_KEY_KP_2, + kp_3 = c.KB_KEY_KP_3, + kp_4 = c.KB_KEY_KP_4, + kp_5 = c.KB_KEY_KP_5, + kp_6 = c.KB_KEY_KP_6, + kp_7 = c.KB_KEY_KP_7, + kp_8 = c.KB_KEY_KP_8, + kp_9 = c.KB_KEY_KP_9, + kp_decimal = c.KB_KEY_KP_DECIMAL, + kp_divide = c.KB_KEY_KP_DIVIDE, + kp_multiply = c.KB_KEY_KP_MULTIPLY, + kp_subtract = c.KB_KEY_KP_SUBTRACT, + kp_add = c.KB_KEY_KP_ADD, + kp_enter = c.KB_KEY_KP_ENTER, + kp_equal = c.KB_KEY_KP_EQUAL, + left_shift = c.KB_KEY_LEFT_SHIFT, + left_control = c.KB_KEY_LEFT_CONTROL, + left_alt = c.KB_KEY_LEFT_ALT, + left_super = c.KB_KEY_LEFT_SUPER, + right_shift = c.KB_KEY_RIGHT_SHIFT, + right_control = c.KB_KEY_RIGHT_CONTROL, + right_alt = c.KB_KEY_RIGHT_ALT, + right_super = c.KB_KEY_RIGHT_SUPER, + menu = c.KB_KEY_MENU, + _, +}; + pub const KeyMod = packed struct { shift: bool = false, control: bool = false, @@ -76,7 +203,20 @@ pub fn Window(comptime TUserData: type) type { InternalError, }; - pub const State = enum { ok, exit }; + pub const UpdateState = enum { + ok, + exit, + + pub fn from(state: c.mfb_update_state) UpdateError!UpdateState { + return switch (state) { + c.STATE_OK => UpdateState.ok, + c.STATE_EXIT => UpdateState.exit, + c.STATE_INVALID_WINDOW => UpdateError.InvalidWindow, + c.STATE_INVALID_BUFFER => UpdateError.InvalidBuffer, + else => UpdateError.InternalError, + }; + } + }; pub const OpenFlags = packed struct { resizable: bool = false, @@ -108,15 +248,16 @@ pub fn Window(comptime TUserData: type) type { return c.mfb_wait_sync(self.cwin); } - pub fn update(self: Window(TUserData), buffer: []Rgb) UpdateError!State { - const rawState = c.mfb_update(self.cwin, @ptrCast(*anyopaque, buffer.ptr)); - switch (rawState) { - c.STATE_OK => return State.ok, - c.STATE_EXIT => return State.exit, - c.STATE_INVALID_WINDOW => return UpdateError.InvalidWindow, - c.STATE_INVALID_BUFFER => return UpdateError.InvalidBuffer, - else => return UpdateError.InternalError, - } + pub fn update(self: Window(TUserData), buffer: []Rgb) UpdateError!UpdateState { + return UpdateState.from(c.mfb_update(self.cwin, @ptrCast(*anyopaque, buffer.ptr))); + } + + pub fn updateEvents(self: Window(TUserData)) UpdateError!UpdateState { + return UpdateState.from(c.mfb_update_events(self.cwin)); + } + + pub fn updateEx(self: Window(TUserData), buffer: []Rgb, width: u32, height: u32) UpdateError!UpdateState { + return UpdateState.from(c.mfb_update_ex(self.cwin), @ptrCast(*anyopaque, buffer), width, height); } pub fn setUserData(self: Window(TUserData), data: *TUserData) void { @@ -153,6 +294,11 @@ pub fn Window(comptime TUserData: type) type { c.mfb_set_mouse_scroll_callback(self.cwin, @ptrCast(c.mfb_mouse_scroll_func, callback)); } + pub const KeyboardFunc = fn (win: Window(TUserData), key: Key, key_mod: KeyMod, is_pressed: bool) callconv(.C) void; + pub fn setKeyboardCallback(self: Window(TUserData), callback: KeyboardFunc) void { + c.mfb_set_keyboard_callback(self.cwin, @ptrCast(c.mfb_keyboard_func, callback)); + } + pub fn setViewport(self: Window(TUserData), offset_x: u32, offset_y: u32, width: u32, height: u32) bool { return c.mfb_set_viewport(self.cwin, offset_x, offset_y, width, height); } diff --git a/src/main.zig b/src/main.zig index 75d8bf7..ceef634 100644 --- a/src/main.zig +++ b/src/main.zig @@ -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; + } } }