Set and get user data
This commit is contained in:
parent
05047c49d8
commit
f7754c81fb
@ -9,53 +9,87 @@ 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,
|
||||||
pub const UpdateError = error {
|
alt: bool = false,
|
||||||
InvalidWindow,
|
super: bool = false,
|
||||||
InvalidBuffer,
|
capsLock: bool = false,
|
||||||
InternalError,
|
numLock: bool = false,
|
||||||
};
|
reserved: u26 = 0,
|
||||||
|
|
||||||
pub const State = enum {ok, exit};
|
|
||||||
|
|
||||||
pub const OpenFlags = packed struct {
|
|
||||||
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 {
|
|
||||||
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 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,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
|
|
||||||
|
pub fn Window(comptime TUserData: type) type {
|
||||||
|
return struct {
|
||||||
|
cwin: *minifb_c.mfb_window,
|
||||||
|
|
||||||
|
pub const UpdateError = error {
|
||||||
|
InvalidWindow,
|
||||||
|
InvalidBuffer,
|
||||||
|
InternalError,
|
||||||
|
};
|
||||||
|
|
||||||
|
pub const State = enum {ok, exit};
|
||||||
|
|
||||||
|
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)));
|
||||||
|
}
|
||||||
|
|
||||||
|
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 {
|
pub fn getTargetFPS() u32 {
|
||||||
return minifb_c.mfb_get_target_fps();
|
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 {
|
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);
|
||||||
|
Loading…
Reference in New Issue
Block a user