Set and get user data
This commit is contained in:
parent
05047c49d8
commit
f7754c81fb
@ -9,7 +9,18 @@ pub fn rgb(r: u8, g: u8, b: u8) u32 {
|
||||
return @intCast(u32, r) << 16 | @intCast(u32, g) << 8 | b;
|
||||
}
|
||||
|
||||
pub const Window = struct{
|
||||
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 fn Window(comptime TUserData: type) type {
|
||||
return struct {
|
||||
cwin: *minifb_c.mfb_window,
|
||||
|
||||
pub const UpdateError = error {
|
||||
@ -26,25 +37,26 @@ pub const Window = struct{
|
||||
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 {
|
||||
const intFlags: u32 = @bitCast(u5, flags);
|
||||
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 {.cwin=value};
|
||||
return Window(TUserData) {.cwin=value};
|
||||
} else {
|
||||
return error.ItBroke;
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
pub fn waitSync(self: Window) bool {
|
||||
pub fn waitSync(self: Window(TUserData)) bool {
|
||||
return minifb_c.mfb_wait_sync(self.cwin);
|
||||
}
|
||||
|
||||
pub fn update(self:Window, buffer: []u32) UpdateError!State {
|
||||
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,
|
||||
@ -54,7 +66,29 @@ pub const Window = struct{
|
||||
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);
|
||||
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();
|
||||
|
13
src/main.zig
13
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);
|
||||
|
Loading…
Reference in New Issue
Block a user