From f7754c81fbf14545625f702b07d54d7bbb3dff03 Mon Sep 17 00:00:00 2001 From: Brandon Dyck Date: Thu, 25 Nov 2021 18:42:28 -0700 Subject: [PATCH] Set and get user data --- lib/minifb/src/minifb.zig | 112 +++++++++++++++++++++++++------------- src/main.zig | 13 +++-- 2 files changed, 81 insertions(+), 44 deletions(-) diff --git a/lib/minifb/src/minifb.zig b/lib/minifb/src/minifb.zig index ef7ea3e..e4a069d 100644 --- a/lib/minifb/src/minifb.zig +++ b/lib/minifb/src/minifb.zig @@ -9,52 +9,86 @@ pub fn rgb(r: u8, g: u8, b: u8) u32 { return @intCast(u32, r) << 16 | @intCast(u32, g) << 8 | b; } -pub const Window = struct{ - cwin: *minifb_c.mfb_window, +pub const KeyMod = packed struct { + shift: bool = false, + control: bool = false, + alt: bool = false, + super: bool = false, + capsLock: bool = false, + numLock: bool = false, + reserved: u26 = 0, +}; - pub const UpdateError = error { - InvalidWindow, - InvalidBuffer, - InternalError, - }; +pub fn Window(comptime TUserData: type) type { + return struct { + cwin: *minifb_c.mfb_window, - pub const State = enum {ok, exit}; + pub const UpdateError = error { + InvalidWindow, + InvalidBuffer, + InternalError, + }; - pub const OpenFlags = packed struct { - resizable: bool = false, - fullscreen: bool = false, - fullscreenDesktop: bool = false, - borderless: bool = false, - alwaysOnTop: bool = false, - }; + pub const State = enum {ok, exit}; - pub fn open(title: [*:0]const u8, width: u32, height: u32, flags: OpenFlags) !Window { - const intFlags: u32 = @bitCast(u5, flags); - const cTitle = @as([*c]const u8, title); - const cwin: ?*minifb_c.mfb_window = minifb_c.mfb_open_ex(cTitle, width, height, intFlags); - if (cwin) |value| { - return Window {.cwin=value}; - } else { - return error.ItBroke; + pub const OpenFlags = packed struct { + resizable: bool = false, + fullscreen: bool = false, + fullscreenDesktop: bool = false, + borderless: bool = false, + alwaysOnTop: bool = false, + reserved: u27 = 0, + }; + + pub fn open(title: [*:0]const u8, width: u32, height: u32, flags: OpenFlags) !Window(TUserData) { + const intFlags = @bitCast(u32, flags); + const cTitle = @as([*c]const u8, title); + const cwin: ?*minifb_c.mfb_window = minifb_c.mfb_open_ex(cTitle, width, height, intFlags); + if (cwin) |value| { + return Window(TUserData) {.cwin=value}; + } else { + return error.ItBroke; + } + + } + + pub fn waitSync(self: Window(TUserData)) bool { + return minifb_c.mfb_wait_sync(self.cwin); + } + + pub fn update(self: Window(TUserData), buffer: []u32) UpdateError!State { + const rawState = minifb_c.mfb_update(self.cwin, buffer.ptr); + switch (rawState) { + .STATE_OK => return State.ok, + .STATE_EXIT => return State.exit, + .STATE_INVALID_WINDOW => return UpdateError.InvalidWindow, + .STATE_INVALID_BUFFER => return UpdateError.InvalidBuffer, + else => return UpdateError.InternalError, + } + } + + pub fn setUserData(self: Window(TUserData), data: *TUserData) void { + minifb_c.mfb_set_user_data(self.cwin, data); + } + + pub fn getUserData(self: Window(TUserData)) ?*TUserData { + return @ptrCast(?*TUserData, @alignCast(@alignOf(?*TUserData), minifb_c.mfb_get_user_data(self.cwin))); } - } - - pub fn waitSync(self: Window) bool { - return minifb_c.mfb_wait_sync(self.cwin); - } - - pub fn update(self:Window, buffer: []u32) UpdateError!State { - const rawState = minifb_c.mfb_update(self.cwin, buffer.ptr); - switch (rawState) { - .STATE_OK => return State.ok, - .STATE_EXIT => return State.exit, - .STATE_INVALID_WINDOW => return UpdateError.InvalidWindow, - .STATE_INVALID_BUFFER => return UpdateError.InvalidBuffer, - else => return UpdateError.InternalError, + test "user data is null if never set" { + const win = try Window(u8).open("", 100, 100); + const data = win.getUserData(); + testing.expectEqual(null, data); } - } -}; + + test "user data is not null if previously set" { + const win = try Window(u8).open("", 100, 100); + var data: u8 = 42; + win.setUserData(&data); + testing.expectEqual(42, win.getUserData().?.*); + } + }; +} pub fn getTargetFPS() u32 { return minifb_c.mfb_get_target_fps(); diff --git a/src/main.zig b/src/main.zig index 0d1a42a..9fd7469 100644 --- a/src/main.zig +++ b/src/main.zig @@ -17,8 +17,6 @@ const Coords = struct { const State = struct { period: f64, - bgValue: u8 = 0, - increasing: bool = true, timer: mfb.Timer, pub fn init(period: f64) !State { @@ -97,15 +95,20 @@ pub fn main() !void { var gp_allocator = std.heap.GeneralPurposeAllocator(.{}){}; const alloc = &gp_allocator.allocator; - var win = mfb.Window.open("Hello minifb-zig", Width, Height, .{.resizable=true}) catch unreachable; - mfb.setTargetFPS(7); - var state = try State.init(3); defer state.deinit(); + var win = mfb.Window(State).open("Hello minifb-zig", Width, Height, .{.resizable=true}) catch unreachable; + mfb.setTargetFPS(7); + win.setUserData(&state); + var buf = try Buffer.init(alloc, Width, Height); defer buf.deinit(); + var aliasedState = win.getUserData(); + if (aliasedState) |value| { + std.log.info("Period: {d}", .{value.*.period}); + } state.render(buf); while (win.waitSync()) { state.render(buf);