libmach: initial API bindings for mach core

This commit is contained in:
Zachary Huang 2022-07-13 00:55:36 -04:00 committed by Stephen Gutekanst
parent 02c7fe9a75
commit 9ece370059
8 changed files with 234 additions and 0 deletions

52
src/bindings.zig Normal file
View file

@ -0,0 +1,52 @@
const std = @import("std");
const gpu = @import("gpu");
const Core = @import("Core.zig");
const libmach = @import("platform/libmach.zig");
const native = @import("platform/native.zig");
// Current Limitations:
// 1. Currently, ecs seems to be using some weird compile-time type trickery, so I'm not exactly sure how
// `engine` should be integrated into the C API
// 2. Core might need to expose more state so more API functions can be exposed (for example, the WebGPU API)
// 3. Be very careful about arguments, types, memory, etc - any mismatch will result in undefined behavior
pub const App = libmach;
pub export fn mach_core_set_init(core_init: libmach.CoreCallback) void {
std.debug.print("mach core set init\n", .{});
libmach.core_callbacks.core_init = core_init;
}
pub export fn mach_core_set_deinit(core_deinit: libmach.CoreCallback) void {
std.debug.print("mach core set deinit\n", .{});
libmach.core_callbacks.core_deinit = core_deinit;
}
pub export fn mach_core_set_update(core_update: libmach.CoreCallback) void {
std.debug.print("mach core set update\n", .{});
libmach.core_callbacks.core_update = core_update;
}
pub export fn mach_run() void {
if (libmach.core_callbacks.core_init == null) {
std.debug.print("Did not provide a core_init callback\n", .{});
return;
}
if (libmach.core_callbacks.core_update == null) {
std.debug.print("Did not provide a core_update callback\n", .{});
return;
}
if (libmach.core_callbacks.core_deinit == null) {
std.debug.print("Did not provide a core_deinit callback\n", .{});
return;
}
native.main() catch unreachable;
}
pub export fn core_set_should_close(core: *Core) void {
core.*.setShouldClose(true);
}
pub export fn core_delta_time(core: *Core) f32 {
return core.*.delta_time;
}

35
src/platform/libmach.zig Normal file
View file

@ -0,0 +1,35 @@
const std = @import("std");
const Core = @import("../Core.zig");
const gpu = @import("gpu");
const ecs = @import("ecs");
pub const App = @This();
// Zig says that *App has a size of 0 bits, and it won't compile if
// pub const core_callback_t = fn (*App, *Core) callconv(.C) void;
// What is *App needed for anyway?
pub const CoreCallback = fn (*Core) callconv(.C) void;
pub const CoreCallbacks = struct {
core_init: ?CoreCallback,
core_update: ?CoreCallback,
core_deinit: ?CoreCallback,
};
pub var core_callbacks = CoreCallbacks {
.core_init = null,
.core_update = null,
.core_deinit = null,
};
pub fn init(_: *App, core: *Core) !void {
core_callbacks.core_init.?(core);
}
pub fn deinit(_: *App, core: *Core) void {
core_callbacks.core_deinit.?(core);
}
pub fn update(_: *App, core: *Core) !void {
core_callbacks.core_update.?(core);
}