Set and get user data

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

View File

@ -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);