libmach: initial API bindings for mach core
This commit is contained in:
parent
02c7fe9a75
commit
9ece370059
8 changed files with 234 additions and 0 deletions
2
libmach/.gitignore
vendored
Normal file
2
libmach/.gitignore
vendored
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
build/
|
||||
test
|
||||
12
libmach/Makefile
Normal file
12
libmach/Makefile
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
build/libmach.dylib: ../build.zig ../src/*.zig ../src/**/*.zig
|
||||
cd ..; zig build
|
||||
|
||||
test: test.c build/libmach.dylib
|
||||
clang -L./build -lmach -o test test.c
|
||||
|
||||
test_c: test
|
||||
# my best attempt at cross-platform dynamic linking (for now)
|
||||
DYLD_LIBRARY_PATH=./build LD_LIBRARY_OATH=./build ./test
|
||||
|
||||
test_lisp: build/libmach.dylib
|
||||
sbcl --load test.lisp
|
||||
11
libmach/README.md
Normal file
11
libmach/README.md
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
# libmach
|
||||
|
||||
Build the `libmach` dynamic library by running `make` (or running `zig build` in the parent directory).
|
||||
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.
|
||||
|
||||
Note: `make test_lisp` requires a relatively recent version of Steel Bank Common Lisp (`sbcl`) to be installed.
|
||||
|
||||
You can find the Zig source code for `libmach` in `src/bindings.zig`.
|
||||
40
libmach/test.c
Normal file
40
libmach/test.c
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
#include <stdio.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
typedef void mach_core_callback(void*);
|
||||
|
||||
// `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*);
|
||||
|
||||
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();
|
||||
return 0;
|
||||
}
|
||||
64
libmach/test.lisp
Normal file
64
libmach/test.lisp
Normal file
|
|
@ -0,0 +1,64 @@
|
|||
;; Tests the behavior of `libmach` using Common Lisp's CFFI
|
||||
;; This Lisp script is basically a one-to-one translation of `test.c`
|
||||
|
||||
(ql:quickload :cffi)
|
||||
|
||||
(defpackage :cffi-user
|
||||
(:use :cl :cffi))
|
||||
|
||||
(in-package :cffi-user)
|
||||
|
||||
(define-foreign-library libmach
|
||||
(t (:default "./build/libmach")))
|
||||
|
||||
(use-foreign-library libmach)
|
||||
|
||||
;; 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_core_set_init(mach_core_callback);
|
||||
(defcfun "mach_core_set_init" :void
|
||||
(callback mach-core-callback))
|
||||
|
||||
;; void mach_core_set_update(mach_core_callback);
|
||||
(defcfun "mach_core_set_update" :void
|
||||
(callback mach-core-callback))
|
||||
|
||||
;; void mach_core_set_deinit(mach_core_callback);
|
||||
(defcfun "mach_core_set_deinit" :void
|
||||
(callback mach-core-callback))
|
||||
|
||||
;; void mach_run(void);
|
||||
(defcfun "mach_run" :void)
|
||||
|
||||
;; 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!~%"))
|
||||
|
||||
(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 my-deinit :void ((core :pointer))
|
||||
(format t "Hello from my-deinit!~%"))
|
||||
|
||||
(mach-core-set-init (callback my-init))
|
||||
|
||||
(mach-core-set-update (callback my-update))
|
||||
|
||||
(mach-core-set-deinit (callback my-deinit))
|
||||
|
||||
(mach-run)
|
||||
|
||||
(sb-ext:exit)
|
||||
Loading…
Add table
Add a link
Reference in a new issue