libmach: update API, exposes init, update, and deinit functions

This commit is contained in:
Zachary Huang 2022-07-19 00:31:29 -04:00 committed by Stephen Gutekanst
parent d194dafb79
commit 77aecbe806
6 changed files with 166 additions and 138 deletions

View file

@ -4,8 +4,9 @@ Build the `libmach` dynamic library by running `make` (or running `zig build` in
The resulting binary should be located in `libmach/build/`.
Test the functionality of `libmach` using `make test_c` and `make test_lisp`.
These commands use C and Lisp to call into `libmach`, and both should show a blank window for exactly 1 second.
These commands use C and Lisp to call into `libmach`, and both should show a blank window for 5 seconds.
If you resize the window, it should print the new dimensions to the standard output.
Note: `make test_lisp` requires a relatively recent version of Steel Bank Common Lisp (`sbcl`) to be installed.
Note: `make test_lisp` requires a relatively recent version of Steel Bank Common Lisp (`sbcl`) to be installed, plus Quicklisp.
You can find the Zig source code for `libmach` in `src/bindings.zig`.
You can find the Zig source code for `libmach` in `src/platform/libmach.zig`.

View file

@ -1,40 +1,46 @@
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
typedef void mach_core_callback(void*);
typedef void resize_callback(void*, uint32_t, uint32_t);
// `libmach` exported API bindings
void mach_core_set_init(mach_core_callback);
void mach_core_set_update(mach_core_callback);
void mach_core_set_deinit(mach_core_callback);
void mach_run(void);
void core_set_should_close(void*);
float core_delta_time(void*);
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 resize_fn(void* core, uint32_t width, uint32_t height) {
printf("Resize callback: %u %u\n", width, height);
}
static float elapsed = 0;
void my_init(void* core) {
printf("My init!\n");
}
void my_update(void* core) {
float dt = core_delta_time(core);
if (elapsed < 1.0) {
elapsed += dt;
} else {
core_set_should_close(core);
}
printf("My update! total time = %f\n", elapsed);
}
void my_deinit(void* core) {
printf("My deinit!\n");
}
int main() {
mach_core_set_init(my_init);
mach_core_set_update(my_update);
mach_core_set_deinit(my_deinit);
mach_run();
void* core = mach_init_core();
if (core == 0) {
printf("Error instantiating mach core\n");
return 0;
}
while (!mach_window_should_close(core)) {
if (mach_update(core, resize_fn) == 0) {
printf("Error updating Mach\n");
break;
};
elapsed += mach_delta_time(core);
if (elapsed > 5.0) {
mach_set_should_close(core);
}
// printf("Elapsed: %f\n", elapsed);
}
mach_deinit(core);
return 0;
}

View file

@ -15,50 +15,51 @@
;; Note: CFFI automatically translates C_style names into lispier kebab-case ones
;; typedef void mach_core_callback(void*);
(defctype mach-core-callback :pointer)
;; 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
;; void mach_core_set_init(mach_core_callback);
(defcfun "mach_core_set_init" :void
(callback mach-core-callback))
;; int mach_update(void*, resize_callback);
(defcfun "mach_update" :int
(core :pointer) (resize-fn :pointer))
;; void mach_core_set_update(mach_core_callback);
(defcfun "mach_core_set_update" :void
(callback mach-core-callback))
;; void mach_deinit(void*);
(defcfun "mach_deinit" :void
(core :pointer))
;; void mach_core_set_deinit(mach_core_callback);
(defcfun "mach_core_set_deinit" :void
(callback mach-core-callback))
;; void mach_set_should_close(void*);
(defcfun "mach_set_should_close" :void
(core :pointer))
;; void mach_run(void);
(defcfun "mach_run" :void)
;; float mach_delta_time(void*);
(defcfun "mach_delta_time" :float
(core :pointer))
;; void core_set_should_close(void*);
(defcfun "core_set_should_close" :void (core :pointer))
;; float core_delta_time(void*);
(defcfun "core_delta_time" :float (core :pointer))
(defcallback my-init :void ((core :pointer))
(format t "Hello from my-init!~%"))
;; bool mach_window_should_close(void*);
(defcfun "mach_window_should_close" :bool
(core :pointer))
;; main
(defvar *elapsed* 0.0)
(defcallback my-update :void ((core :pointer))
(format t "Hello from my-update ~a~%" *elapsed*)
(if (< *elapsed* 1.0)
(incf *elapsed* (core-delta-time core))
(core-set-should-close core)))
(defcallback resize-fn :void ((core :pointer) (width :unsigned-int) (height :unsigned-int))
(format t "Resize Callback: ~S ~S~%" width height))
(defcallback my-deinit :void ((core :pointer))
(format t "Hello from my-deinit!~%"))
(setf core (mach-init-core))
(mach-core-set-init (callback my-init))
(format t "Core: ~S~%" core)
(mach-core-set-update (callback my-update))
(when (pointer-eq core (null-pointer))
(format t "Failed to initialize mach core~%")
(sb-ext:exit))
(mach-core-set-deinit (callback my-deinit))
(mach-run)
(loop while (not (mach-window-should-close core))
do (progn
(when (= 0 (mach-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))))
(sb-ext:exit)