Set and get user data

This commit is contained in:
Brandon Dyck 2021-11-25 18:42:28 -07:00
parent 05047c49d8
commit f7754c81fb
2 changed files with 81 additions and 44 deletions

View File

@ -9,52 +9,86 @@ pub fn rgb(r: u8, g: u8, b: u8) u32 {
return @intCast(u32, r) << 16 | @intCast(u32, g) << 8 | b; return @intCast(u32, r) << 16 | @intCast(u32, g) << 8 | b;
} }
pub const Window = struct{ pub const KeyMod = packed struct {
cwin: *minifb_c.mfb_window, 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 { pub fn Window(comptime TUserData: type) type {
InvalidWindow, return struct {
InvalidBuffer, cwin: *minifb_c.mfb_window,
InternalError,
};
pub const State = enum {ok, exit}; pub const UpdateError = error {
InvalidWindow,
InvalidBuffer,
InternalError,
};
pub const OpenFlags = packed struct { pub const State = enum {ok, exit};
resizable: bool = false,
fullscreen: bool = false,
fullscreenDesktop: bool = false,
borderless: bool = false,
alwaysOnTop: bool = false,
};
pub fn open(title: [*:0]const u8, width: u32, height: u32, flags: OpenFlags) !Window { pub const OpenFlags = packed struct {
const intFlags: u32 = @bitCast(u5, flags); resizable: bool = false,
const cTitle = @as([*c]const u8, title); fullscreen: bool = false,
const cwin: ?*minifb_c.mfb_window = minifb_c.mfb_open_ex(cTitle, width, height, intFlags); fullscreenDesktop: bool = false,
if (cwin) |value| { borderless: bool = false,
return Window {.cwin=value}; alwaysOnTop: bool = false,
} else { reserved: u27 = 0,
return error.ItBroke; };
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)));
} }
} test "user data is null if never set" {
const win = try Window(u8).open("", 100, 100);
pub fn waitSync(self: Window) bool { const data = win.getUserData();
return minifb_c.mfb_wait_sync(self.cwin); testing.expectEqual(null, data);
}
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 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 { pub fn getTargetFPS() u32 {
return minifb_c.mfb_get_target_fps(); return minifb_c.mfb_get_target_fps();

View File

@ -17,8 +17,6 @@ const Coords = struct {
const State = struct { const State = struct {
period: f64, period: f64,
bgValue: u8 = 0,
increasing: bool = true,
timer: mfb.Timer, timer: mfb.Timer,
pub fn init(period: f64) !State { pub fn init(period: f64) !State {
@ -97,15 +95,20 @@ pub fn main() !void {
var gp_allocator = std.heap.GeneralPurposeAllocator(.{}){}; var gp_allocator = std.heap.GeneralPurposeAllocator(.{}){};
const alloc = &gp_allocator.allocator; 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); var state = try State.init(3);
defer state.deinit(); 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); var buf = try Buffer.init(alloc, Width, Height);
defer buf.deinit(); defer buf.deinit();
var aliasedState = win.getUserData();
if (aliasedState) |value| {
std.log.info("Period: {d}", .{value.*.period});
}
state.render(buf); state.render(buf);
while (win.waitSync()) { while (win.waitSync()) {
state.render(buf); state.render(buf);