Fix ActiveFunc calling convention

This commit is contained in:
Brandon Dyck 2022-01-09 11:51:45 -07:00
parent 293b8729c0
commit 452be0a0b8
2 changed files with 13 additions and 12 deletions

View File

@ -1,4 +1,5 @@
const std = @import("std"); const std = @import("std");
const assert = std.debug.assert;
const c = @cImport({ const c = @cImport({
@cInclude("MiniFB.h"); @cInclude("MiniFB.h");
@cInclude("MiniFB_enums.h"); @cInclude("MiniFB_enums.h");
@ -48,7 +49,9 @@ pub fn Window(comptime TUserData: type) type {
const cTitle = @as([*c]const u8, title); const cTitle = @as([*c]const u8, title);
const cwin: ?*c.mfb_window = c.mfb_open_ex(cTitle, width, height, intFlags); const cwin: ?*c.mfb_window = c.mfb_open_ex(cTitle, width, height, intFlags);
if (cwin) |value| { if (cwin) |value| {
return Window(TUserData){ .cwin = value }; const win = Window(TUserData){ .cwin = value };
assert(@bitCast(usize, win) == @ptrToInt(win.cwin));
return win;
} else { } else {
return error.ItBroke; return error.ItBroke;
} }
@ -75,12 +78,10 @@ pub fn Window(comptime TUserData: type) type {
pub fn getUserData(self: Window(TUserData)) ?*TUserData { pub fn getUserData(self: Window(TUserData)) ?*TUserData {
var cData = c.mfb_get_user_data(self.cwin); var cData = c.mfb_get_user_data(self.cwin);
std.log.info("@alignOf cwin={d}, addr={x}", .{ @alignOf(@TypeOf(self.cwin)), @ptrToInt(self.cwin) });
std.log.info("@alignOf cdata={d}, addr={x}", .{ @alignOf(@TypeOf(cData)), @ptrToInt(cData) });
return @ptrCast(?*TUserData, @alignCast(@alignOf(?*TUserData), cData)); return @ptrCast(?*TUserData, @alignCast(@alignOf(?*TUserData), cData));
} }
pub const ActiveFunc = fn (win: Window(TUserData), isActive: bool) void; pub const ActiveFunc = fn (win: Window(TUserData), isActive: bool) callconv(.C) void;
pub fn setActiveCallback(self: Window(TUserData), callback: ActiveFunc) void { pub fn setActiveCallback(self: Window(TUserData), callback: ActiveFunc) void {
c.mfb_set_active_callback(self.cwin, @ptrCast(c.mfb_active_func, callback)); c.mfb_set_active_callback(self.cwin, @ptrCast(c.mfb_active_func, callback));
} }
@ -93,13 +94,13 @@ test "user data is null if never set" {
try testing.expectEqual(@as(?*u64, null), data); try testing.expectEqual(@as(?*u64, null), data);
} }
// test "user data is not null if previously set" { test "user data is not null if previously set" {
// const win = try Window(u64).open("abc", 100, 100, .{}); const win = try Window(u64).open("abc", 100, 100, .{});
// var data: u64 = 42; var data: u64 = 42;
// win.setUserData(&data); win.setUserData(&data);
// const expected: u64 = 42; const expected: u64 = 42;
// try testing.expectEqual(expected, win.getUserData().?.*); try testing.expectEqual(expected, win.getUserData().?.*);
// } }
pub fn getTargetFPS() u32 { pub fn getTargetFPS() u32 {
return c.mfb_get_target_fps(); return c.mfb_get_target_fps();

View File

@ -98,7 +98,7 @@ const Buffer = struct {
} }
}; };
fn handleActive(win: mfb.Window(State), isActive: bool) void { fn handleActive(win: mfb.Window(State), isActive: bool) callconv(.C) void {
// win.getUserData().?.*.active = isActive; // win.getUserData().?.*.active = isActive;
var data = win.getUserData(); var data = win.getUserData();
std.log.info("got data", .{}); std.log.info("got data", .{});