libmach: update API again, factors out init/update/deinit from native.zig main function

This commit is contained in:
Zachary Huang 2022-07-19 23:42:34 -04:00 committed by Stephen Gutekanst
parent ce21694d75
commit 5d86314fbb
5 changed files with 121 additions and 147 deletions

View file

@ -4,13 +4,18 @@
typedef void resize_callback(void*, uint32_t, uint32_t);
typedef enum {
MachFailure,
MachSuccess
} MachReturn;
// `libmach` exported API bindings
void* mach_init_core(void);
void mach_deinit(void*);
void mach_set_should_close(void*);
bool mach_window_should_close(void*);
int mach_update(void*, resize_callback);
float mach_delta_time(void*);
void* mach_core_init(void);
void mach_core_deinit(void*);
void mach_core_set_should_close(void*);
bool mach_core_window_should_close(void*);
MachReturn mach_core_update(void*, resize_callback);
float mach_core_delta_time(void*);
void resize_fn(void* core, uint32_t width, uint32_t 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;
int main() {
void* core = mach_init_core();
void* core = mach_core_init();
if (core == 0) {
printf("Error instantiating mach core\n");
return 0;
}
while (!mach_window_should_close(core)) {
if (mach_update(core, resize_fn) == 0) {
while (!mach_core_window_should_close(core)) {
if (mach_core_update(core, resize_fn) == MachFailure) {
printf("Error updating Mach\n");
break;
};
elapsed += mach_delta_time(core);
elapsed += mach_core_delta_time(core);
if (elapsed > 5.0) {
mach_set_should_close(core);
mach_core_set_should_close(core);
}
// printf("Elapsed: %f\n", elapsed);
}
mach_deinit(core);
mach_core_deinit(core);
return 0;
}

View file

@ -15,29 +15,24 @@
;; Note: CFFI automatically translates C_style names into lispier kebab-case ones
;; void* mach_init(void);
(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
(defcfun "mach_core_init" :pointer)
;; int mach_update(void*, resize_callback);
(defcfun "mach_update" :int
(defcfun "mach_core_update" :int
(core :pointer) (resize-fn :pointer))
;; void mach_deinit(void*);
(defcfun "mach_deinit" :void
(defcfun "mach_core_deinit" :void
(core :pointer))
;; void mach_set_should_close(void*);
(defcfun "mach_set_should_close" :void
(defcfun "mach_core_set_should_close" :void
(core :pointer))
;; float mach_delta_time(void*);
(defcfun "mach_delta_time" :float
(defcfun "mach_core_delta_time" :float
(core :pointer))
;; bool mach_window_should_close(void*);
(defcfun "mach_window_should_close" :bool
(defcfun "mach_core_window_should_close" :bool
(core :pointer))
;; main
@ -46,20 +41,18 @@
(defcallback resize-fn :void ((core :pointer) (width :unsigned-int) (height :unsigned-int))
(format t "Resize Callback: ~S ~S~%" width height))
(setf core (mach-init-core))
(format t "Core: ~S~%" core)
(setf core (mach-core-init))
(when (pointer-eq core (null-pointer))
(format t "Failed to initialize mach core~%")
(sb-ext:exit))
(loop while (not (mach-window-should-close core))
(loop while (not (mach-core-window-should-close core))
do (progn
(when (= 0 (mach-update core (callback resize-fn)))
(when (= 0 (mach-core-update core (callback resize-fn)))
(format t "Error updating mach~%")
(sb-ext:exit))
(when (> (incf *elapsed* (mach-delta-time core)) 5.0)
(mach-set-should-close core))))
(when (> (incf *elapsed* (mach-core-delta-time core)) 5.0)
(mach-core-set-should-close core))))
(sb-ext:exit)