Set and get user data
This commit is contained in:
@ -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();
|
||||
|
Reference in New Issue
Block a user