libmach: fix small issues and naming conventions

This commit is contained in:
Zachary Huang 2022-07-20 14:44:31 -04:00 committed by Stephen Gutekanst
parent 5d86314fbb
commit 8f6273c0d0
4 changed files with 30 additions and 25 deletions

View file

@ -4,17 +4,18 @@
typedef void resize_callback(void*, uint32_t, uint32_t); typedef void resize_callback(void*, uint32_t, uint32_t);
typedef enum { typedef enum MachStatus {
MachFailure, MachStatus_Success = 0x00000000,
MachSuccess MachStatus_Error = 0x00000001,
} MachReturn; MachStatus_Force32 = 0x7FFFFFFF
} MachStatus;
// `libmach` exported API bindings // `libmach` exported API bindings
void* mach_core_init(void); void* mach_core_init(void);
void mach_core_deinit(void*); void mach_core_deinit(void*);
void mach_core_set_should_close(void*); void mach_core_set_should_close(void*);
bool mach_core_window_should_close(void*); bool mach_core_window_should_close(void*);
MachReturn mach_core_update(void*, resize_callback); MachStatus mach_core_update(void*, resize_callback);
float mach_core_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) {
@ -32,7 +33,7 @@ int main() {
} }
while (!mach_core_window_should_close(core)) { while (!mach_core_window_should_close(core)) {
if (mach_core_update(core, resize_fn) == MachFailure) { if (mach_core_update(core, resize_fn) == MachStatus_Error) {
printf("Error updating Mach\n"); printf("Error updating Mach\n");
break; break;
}; };

View file

@ -15,6 +15,10 @@
;; Note: CFFI automatically translates C_style names into lispier kebab-case ones ;; Note: CFFI automatically translates C_style names into lispier kebab-case ones
(defcenum mach-status
(:success 0)
(:error 1))
(defcfun "mach_core_init" :pointer) (defcfun "mach_core_init" :pointer)
(defcfun "mach_core_update" :int (defcfun "mach_core_update" :int
@ -49,7 +53,8 @@
(loop while (not (mach-core-window-should-close core)) (loop while (not (mach-core-window-should-close core))
do (progn do (progn
(when (= 0 (mach-core-update core (callback resize-fn))) (when (= (foreign-enum-value 'mach-status :error)
(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-core-delta-time core)) 5.0) (when (> (incf *elapsed* (mach-core-delta-time core)) 5.0)

View file

@ -38,24 +38,24 @@ const allocator = gpa.allocator();
// Returns a pointer to a newly allocated Core // Returns a pointer to a newly allocated Core
// Will return a null pointer if an error occurred while initializing Core // Will return a null pointer if an error occurred while initializing Core
pub export fn mach_core_init() ?*Core { pub export fn mach_core_init() ?*Core {
const core = native.core_init(allocator) catch { const core = native.coreInit(allocator) catch {
return @intToPtr(?*Core, 0); return @intToPtr(?*Core, 0);
}; };
return core; return core;
} }
pub export fn mach_core_deinit(core: *Core) void { pub export fn mach_core_deinit(core: *Core) void {
native.core_deinit(core); native.coreDeinit(core, allocator);
} }
pub export fn mach_core_update(core: *Core, resize: ?native.CoreResizeCallback) MachReturn { pub export fn mach_core_update(core: *Core, resize: ?native.CoreResizeCallback) MachStatus {
native.core_update(core, resize) catch { native.coreUpdate(core, resize) catch {
return MachReturn.Failure; return MachStatus.Error;
}; };
return MachReturn.Success; return MachStatus.Success;
} }
const MachReturn = enum(c_int) { const MachStatus = enum(c_int) {
Failure, Success = 0x00000000,
Success, Error = 0x00000001,
}; };

View file

@ -594,38 +594,37 @@ pub fn main() !void {
defer _ = gpa.deinit(); defer _ = gpa.deinit();
const allocator = gpa.allocator(); const allocator = gpa.allocator();
var core = try core_init(allocator); var core = try coreInit(allocator);
defer core_deinit(core); defer coreDeinit(core, allocator);
try app.init(core); try app.init(core);
defer app.deinit(core); defer app.deinit(core);
while (!core.internal.window.shouldClose()) { while (!core.internal.window.shouldClose()) {
try core_update(core, null); try coreUpdate(core, null);
try app.update(core); try app.update(core);
} }
} }
pub fn core_init(allocator: std.mem.Allocator) !*Core { pub fn coreInit(allocator: std.mem.Allocator) !*Core {
const core: *Core = try allocator.create(Core); const core: *Core = try allocator.create(Core);
core.* = try Core.init(allocator); core.* = try Core.init(allocator);
// // Glfw specific: initialize the user pointer used in callbacks // Glfw specific: initialize the user pointer used in callbacks
core.*.internal.initCallback(); core.*.internal.initCallback();
return core; return core;
} }
pub export fn core_deinit(core: *Core) void { pub fn coreDeinit(core: *Core, allocator: std.mem.Allocator) void {
core.internal.deinit(); core.internal.deinit();
// assumes that core.allocator is the same allocator used to allocate core allocator.destroy(core);
core.allocator.destroy(core); // "I used the core to destroy the core"
} }
pub const CoreResizeCallback = fn (*Core, u32, u32) callconv(.C) void; pub const CoreResizeCallback = fn (*Core, u32, u32) callconv(.C) void;
pub fn core_update(core: *Core, resize: ?CoreResizeCallback) !void { pub fn coreUpdate(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