libmach: update API again, factors out init/update/deinit from native.zig main function
This commit is contained in:
parent
ce21694d75
commit
5d86314fbb
5 changed files with 121 additions and 147 deletions
|
|
@ -123,7 +123,8 @@ pub fn build(b: *std.build.Builder) void {
|
||||||
compile_all.dependOn(b.getInstallStep());
|
compile_all.dependOn(b.getInstallStep());
|
||||||
|
|
||||||
// compiles the `libmach` shared library
|
// compiles the `libmach` shared library
|
||||||
const lib = b.addSharedLibrary("mach", "src/bindings.zig", .unversioned);
|
const lib = b.addSharedLibrary("mach", "src/platform/libmach.zig", .unversioned);
|
||||||
|
lib.main_pkg_path = "src/";
|
||||||
const app_pkg = std.build.Pkg{
|
const app_pkg = std.build.Pkg{
|
||||||
.name = "app",
|
.name = "app",
|
||||||
.source = .{ .path = "src/platform/libmach.zig" },
|
.source = .{ .path = "src/platform/libmach.zig" },
|
||||||
|
|
|
||||||
|
|
@ -4,13 +4,18 @@
|
||||||
|
|
||||||
typedef void resize_callback(void*, uint32_t, uint32_t);
|
typedef void resize_callback(void*, uint32_t, uint32_t);
|
||||||
|
|
||||||
|
typedef enum {
|
||||||
|
MachFailure,
|
||||||
|
MachSuccess
|
||||||
|
} MachReturn;
|
||||||
|
|
||||||
// `libmach` exported API bindings
|
// `libmach` exported API bindings
|
||||||
void* mach_init_core(void);
|
void* mach_core_init(void);
|
||||||
void mach_deinit(void*);
|
void mach_core_deinit(void*);
|
||||||
void mach_set_should_close(void*);
|
void mach_core_set_should_close(void*);
|
||||||
bool mach_window_should_close(void*);
|
bool mach_core_window_should_close(void*);
|
||||||
int mach_update(void*, resize_callback);
|
MachReturn mach_core_update(void*, resize_callback);
|
||||||
float mach_delta_time(void*);
|
float mach_core_delta_time(void*);
|
||||||
|
|
||||||
void resize_fn(void* core, uint32_t width, uint32_t height) {
|
void resize_fn(void* core, uint32_t width, uint32_t height) {
|
||||||
printf("Resize callback: %u %u\n", width, height);
|
printf("Resize callback: %u %u\n", width, height);
|
||||||
|
|
@ -19,28 +24,28 @@ void resize_fn(void* core, uint32_t width, uint32_t height) {
|
||||||
static float elapsed = 0;
|
static float elapsed = 0;
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
void* core = mach_init_core();
|
void* core = mach_core_init();
|
||||||
|
|
||||||
if (core == 0) {
|
if (core == 0) {
|
||||||
printf("Error instantiating mach core\n");
|
printf("Error instantiating mach core\n");
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (!mach_window_should_close(core)) {
|
while (!mach_core_window_should_close(core)) {
|
||||||
if (mach_update(core, resize_fn) == 0) {
|
if (mach_core_update(core, resize_fn) == MachFailure) {
|
||||||
printf("Error updating Mach\n");
|
printf("Error updating Mach\n");
|
||||||
break;
|
break;
|
||||||
};
|
};
|
||||||
|
|
||||||
elapsed += mach_delta_time(core);
|
elapsed += mach_core_delta_time(core);
|
||||||
if (elapsed > 5.0) {
|
if (elapsed > 5.0) {
|
||||||
mach_set_should_close(core);
|
mach_core_set_should_close(core);
|
||||||
}
|
}
|
||||||
|
|
||||||
// printf("Elapsed: %f\n", elapsed);
|
// printf("Elapsed: %f\n", elapsed);
|
||||||
}
|
}
|
||||||
|
|
||||||
mach_deinit(core);
|
mach_core_deinit(core);
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -15,29 +15,24 @@
|
||||||
|
|
||||||
;; Note: CFFI automatically translates C_style names into lispier kebab-case ones
|
;; Note: CFFI automatically translates C_style names into lispier kebab-case ones
|
||||||
|
|
||||||
;; void* mach_init(void);
|
(defcfun "mach_core_init" :pointer)
|
||||||
(defcfun "mach_init_core" :pointer)
|
|
||||||
;; for some reason, calling "mach_init" always returns a null pointer, and I have no clue why...
|
|
||||||
;; So I renamed the API function name to "mach_init_core" instead
|
|
||||||
|
|
||||||
;; int mach_update(void*, resize_callback);
|
(defcfun "mach_core_update" :int
|
||||||
(defcfun "mach_update" :int
|
|
||||||
(core :pointer) (resize-fn :pointer))
|
(core :pointer) (resize-fn :pointer))
|
||||||
|
|
||||||
;; void mach_deinit(void*);
|
(defcfun "mach_core_deinit" :void
|
||||||
(defcfun "mach_deinit" :void
|
|
||||||
(core :pointer))
|
(core :pointer))
|
||||||
|
|
||||||
;; void mach_set_should_close(void*);
|
;; void mach_set_should_close(void*);
|
||||||
(defcfun "mach_set_should_close" :void
|
(defcfun "mach_core_set_should_close" :void
|
||||||
(core :pointer))
|
(core :pointer))
|
||||||
|
|
||||||
;; float mach_delta_time(void*);
|
;; float mach_delta_time(void*);
|
||||||
(defcfun "mach_delta_time" :float
|
(defcfun "mach_core_delta_time" :float
|
||||||
(core :pointer))
|
(core :pointer))
|
||||||
|
|
||||||
;; bool mach_window_should_close(void*);
|
;; bool mach_window_should_close(void*);
|
||||||
(defcfun "mach_window_should_close" :bool
|
(defcfun "mach_core_window_should_close" :bool
|
||||||
(core :pointer))
|
(core :pointer))
|
||||||
|
|
||||||
;; main
|
;; main
|
||||||
|
|
@ -46,20 +41,18 @@
|
||||||
(defcallback resize-fn :void ((core :pointer) (width :unsigned-int) (height :unsigned-int))
|
(defcallback resize-fn :void ((core :pointer) (width :unsigned-int) (height :unsigned-int))
|
||||||
(format t "Resize Callback: ~S ~S~%" width height))
|
(format t "Resize Callback: ~S ~S~%" width height))
|
||||||
|
|
||||||
(setf core (mach-init-core))
|
(setf core (mach-core-init))
|
||||||
|
|
||||||
(format t "Core: ~S~%" core)
|
|
||||||
|
|
||||||
(when (pointer-eq core (null-pointer))
|
(when (pointer-eq core (null-pointer))
|
||||||
(format t "Failed to initialize mach core~%")
|
(format t "Failed to initialize mach core~%")
|
||||||
(sb-ext:exit))
|
(sb-ext:exit))
|
||||||
|
|
||||||
(loop while (not (mach-window-should-close core))
|
(loop while (not (mach-core-window-should-close core))
|
||||||
do (progn
|
do (progn
|
||||||
(when (= 0 (mach-update core (callback resize-fn)))
|
(when (= 0 (mach-core-update core (callback resize-fn)))
|
||||||
(format t "Error updating mach~%")
|
(format t "Error updating mach~%")
|
||||||
(sb-ext:exit))
|
(sb-ext:exit))
|
||||||
(when (> (incf *elapsed* (mach-delta-time core)) 5.0)
|
(when (> (incf *elapsed* (mach-core-delta-time core)) 5.0)
|
||||||
(mach-set-should-close core))))
|
(mach-core-set-should-close core))))
|
||||||
|
|
||||||
(sb-ext:exit)
|
(sb-ext:exit)
|
||||||
|
|
|
||||||
|
|
@ -3,6 +3,7 @@ const Core = @import("../Core.zig");
|
||||||
const gpu = @import("gpu");
|
const gpu = @import("gpu");
|
||||||
const ecs = @import("ecs");
|
const ecs = @import("ecs");
|
||||||
const glfw = @import("glfw");
|
const glfw = @import("glfw");
|
||||||
|
const native = @import("native.zig");
|
||||||
|
|
||||||
pub const App = @This();
|
pub const App = @This();
|
||||||
|
|
||||||
|
|
@ -19,89 +20,42 @@ pub fn update(_: *App, _: *Core) !void { }
|
||||||
// 2. Core might need to expose more state so more API functions can be exposed (for example, the WebGPU 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
|
// 3. Be very careful about arguments, types, memory, etc - any mismatch will result in undefined behavior
|
||||||
|
|
||||||
pub export fn mach_set_should_close(core: *Core) void {
|
pub export fn mach_core_set_should_close(core: *Core) void {
|
||||||
core.setShouldClose(true);
|
core.setShouldClose(true);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub export fn mach_delta_time(core: *Core) f32 {
|
pub export fn mach_core_delta_time(core: *Core) f32 {
|
||||||
return core.delta_time;
|
return core.delta_time;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub export fn mach_core_window_should_close(core: *Core) bool {
|
||||||
|
return core.internal.window.shouldClose();
|
||||||
|
}
|
||||||
|
|
||||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
const allocator = gpa.allocator();
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
// Initializes and returns Mach's core structure
|
// Returns a pointer to a newly allocated Core
|
||||||
// Uses an optional type so it is valid to return 0 (on an error)
|
// Will return a null pointer if an error occurred while initializing Core
|
||||||
// TODO: come up with a better error reporting system
|
pub export fn mach_core_init() ?*Core {
|
||||||
pub export fn mach_init_core() ?*Core {
|
const core = native.core_init(allocator) catch {
|
||||||
const core: *Core = allocator.create(Core) catch {
|
return @intToPtr(?*Core, 0);
|
||||||
return @intToPtr(?*Core, 0); // on error, return null pointer
|
|
||||||
};
|
};
|
||||||
core.* = Core.init(allocator) catch {
|
|
||||||
return @intToPtr(?*Core, 0); // on error, return null pointer
|
|
||||||
};
|
|
||||||
|
|
||||||
// // Glfw specific: initialize the user pointer used in callbacks
|
|
||||||
core.*.internal.initCallback();
|
|
||||||
|
|
||||||
return core;
|
return core;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Deinitializes mach core structure
|
pub export fn mach_core_deinit(core: *Core) void {
|
||||||
pub export fn mach_deinit(core: *Core) void {
|
native.core_deinit(core);
|
||||||
core.internal.deinit();
|
|
||||||
allocator.destroy(core);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub export fn mach_window_should_close(core: *Core) bool {
|
pub export fn mach_core_update(core: *Core, resize: ?native.CoreResizeCallback) MachReturn {
|
||||||
return core.internal.window.shouldClose();
|
native.core_update(core, resize) catch {
|
||||||
}
|
return MachReturn.Failure;
|
||||||
|
|
||||||
pub const CoreCallback = fn (*Core, u32, u32) callconv(.C) void;
|
|
||||||
|
|
||||||
// Adapted from native.zig
|
|
||||||
pub export fn mach_update(core: *Core, resize_fn: ?CoreCallback) i32 {
|
|
||||||
if (core.internal.wait_event_timeout > 0.0) {
|
|
||||||
if (core.internal.wait_event_timeout == std.math.inf(f64)) {
|
|
||||||
// Wait for an event
|
|
||||||
glfw.waitEvents() catch {
|
|
||||||
return 0;
|
|
||||||
};
|
};
|
||||||
} else {
|
return MachReturn.Success;
|
||||||
// Wait for an event with a timeout
|
}
|
||||||
glfw.waitEventsTimeout(core.internal.wait_event_timeout) catch {
|
|
||||||
return 0;
|
const MachReturn = enum(c_int) {
|
||||||
|
Failure,
|
||||||
|
Success,
|
||||||
};
|
};
|
||||||
}
|
|
||||||
} else {
|
|
||||||
// Don't wait for events
|
|
||||||
glfw.pollEvents() catch {
|
|
||||||
return 0;
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
core.delta_time_ns = core.timer.lapPrecise();
|
|
||||||
core.delta_time = @intToFloat(f32, core.delta_time_ns) / @intToFloat(f32, std.time.ns_per_s);
|
|
||||||
|
|
||||||
var framebuffer_size = core.getFramebufferSize();
|
|
||||||
core.target_desc.width = framebuffer_size.width;
|
|
||||||
core.target_desc.height = framebuffer_size.height;
|
|
||||||
|
|
||||||
if (core.swap_chain == null or !core.current_desc.equal(&core.target_desc)) {
|
|
||||||
const use_legacy_api = core.surface == null;
|
|
||||||
if (!use_legacy_api) {
|
|
||||||
core.swap_chain = core.device.nativeCreateSwapChain(core.surface, &core.target_desc);
|
|
||||||
} else core.swap_chain.?.configure(
|
|
||||||
core.swap_chain_format,
|
|
||||||
.{ .render_attachment = true },
|
|
||||||
core.target_desc.width,
|
|
||||||
core.target_desc.height,
|
|
||||||
);
|
|
||||||
|
|
||||||
if (resize_fn != null) {
|
|
||||||
resize_fn.?(core, core.target_desc.width, core.target_desc.height);
|
|
||||||
}
|
|
||||||
core.current_desc = core.target_desc;
|
|
||||||
}
|
|
||||||
return 1;
|
|
||||||
}
|
|
||||||
|
|
|
||||||
|
|
@ -587,23 +587,45 @@ pub const Platform = struct {
|
||||||
|
|
||||||
pub const BackingTimer = std.time.Timer;
|
pub const BackingTimer = std.time.Timer;
|
||||||
|
|
||||||
|
var app: App = undefined;
|
||||||
|
|
||||||
pub fn main() !void {
|
pub fn main() !void {
|
||||||
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
|
||||||
defer _ = gpa.deinit();
|
defer _ = gpa.deinit();
|
||||||
const allocator = gpa.allocator();
|
const allocator = gpa.allocator();
|
||||||
|
|
||||||
var core = try Core.init(allocator);
|
var core = try core_init(allocator);
|
||||||
defer core.internal.deinit();
|
defer core_deinit(core);
|
||||||
var app: App = undefined;
|
|
||||||
|
|
||||||
try app.init(&core);
|
try app.init(core);
|
||||||
defer app.deinit(&core);
|
defer app.deinit(core);
|
||||||
|
|
||||||
// Glfw specific: initialize the user pointer used in callbacks
|
while (!core.internal.window.shouldClose()) {
|
||||||
core.internal.initCallback();
|
try core_update(core, null);
|
||||||
|
|
||||||
const window = core.internal.window;
|
try app.update(core);
|
||||||
while (!window.shouldClose()) {
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn core_init(allocator: std.mem.Allocator) !*Core {
|
||||||
|
const core: *Core = try allocator.create(Core);
|
||||||
|
core.* = try Core.init(allocator);
|
||||||
|
|
||||||
|
// // Glfw specific: initialize the user pointer used in callbacks
|
||||||
|
core.*.internal.initCallback();
|
||||||
|
|
||||||
|
return core;
|
||||||
|
}
|
||||||
|
|
||||||
|
pub export fn core_deinit(core: *Core) void {
|
||||||
|
core.internal.deinit();
|
||||||
|
// assumes that core.allocator is the same allocator used to allocate core
|
||||||
|
core.allocator.destroy(core); // "I used the core to destroy the core"
|
||||||
|
}
|
||||||
|
|
||||||
|
pub const CoreResizeCallback = fn (*Core, u32, u32) callconv(.C) void;
|
||||||
|
|
||||||
|
pub fn core_update(core: *Core, resize: ?CoreResizeCallback) !void {
|
||||||
if (core.internal.wait_event_timeout > 0.0) {
|
if (core.internal.wait_event_timeout > 0.0) {
|
||||||
if (core.internal.wait_event_timeout == std.math.inf(f64)) {
|
if (core.internal.wait_event_timeout == std.math.inf(f64)) {
|
||||||
// Wait for an event
|
// Wait for an event
|
||||||
|
|
@ -636,11 +658,10 @@ pub fn main() !void {
|
||||||
);
|
);
|
||||||
|
|
||||||
if (@hasDecl(App, "resize")) {
|
if (@hasDecl(App, "resize")) {
|
||||||
try app.resize(&core, core.target_desc.width, core.target_desc.height);
|
try app.resize(core, core.target_desc.width, core.target_desc.height);
|
||||||
|
} else if (resize != null) {
|
||||||
|
resize.?(core, core.target_desc.width, core.target_desc.height);
|
||||||
}
|
}
|
||||||
core.current_desc = core.target_desc;
|
core.current_desc = core.target_desc;
|
||||||
}
|
}
|
||||||
|
|
||||||
try app.update(&core);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue