From 4ea0e7933e57c251dc2474947fa9a948cf19a868 Mon Sep 17 00:00:00 2001 From: Dane Johnson Date: Tue, 31 Mar 2026 19:52:07 -0500 Subject: [PATCH] Guile agar --- agar/.dir-locals.el | 4 ++++ agar/config.scm | 2 ++ agar/core.scm | 29 +++++++++++++++++++++++++++++ agar/gui.scm | 32 ++++++++++++++++++++++++++++++++ agar/util.scm | 16 ++++++++++++++++ examples/boids.scm | 10 ++++++++++ 6 files changed, 93 insertions(+) create mode 100644 agar/.dir-locals.el create mode 100644 agar/config.scm create mode 100644 agar/core.scm create mode 100644 agar/gui.scm create mode 100644 agar/util.scm create mode 100644 examples/boids.scm diff --git a/agar/.dir-locals.el b/agar/.dir-locals.el new file mode 100644 index 0000000..c182efa --- /dev/null +++ b/agar/.dir-locals.el @@ -0,0 +1,4 @@ +;;; Directory Local Variables -*- no-byte-compile: t -*- +;;; For more information see (info "(emacs) Directory Variables") + +((scheme-mode . ((eval . (put 'with-string-inputs 'scheme-indent-function 1))))) diff --git a/agar/config.scm b/agar/config.scm new file mode 100644 index 0000000..4a714e9 --- /dev/null +++ b/agar/config.scm @@ -0,0 +1,2 @@ +(define-module (agar config) + #:use-module (system foreign)) diff --git a/agar/core.scm b/agar/core.scm new file mode 100644 index 0000000..f90926a --- /dev/null +++ b/agar/core.scm @@ -0,0 +1,29 @@ +(define-module (agar core) + #:use-module (agar util) + #:use-module (system foreign) + #:use-module (system foreign-library) + #:export (init-core get-error destroy event-loop) + #:replace (quit)) + +(define init-core + (with-string-inputs '(0) + (foreign-library-function "libag_core" "AG_InitCore" + #:return-type int + #:arg-types (list '* unsigned-int)))) + +(define get-error + (foreign-library-function "libag_core" "AG_GetError" + #:return-type int + #:arg-types (list '* unsigned-int))) + +(define quit + (foreign-library-function "libag_core" "AG_Quit" + #:return-type void)) +(define destroy + (foreign-library-function "libag_core" "AG_Destroy" + #:return-type void)) + +(define event-loop + (foreign-library-function "libag_core" "AG_EventLoop" + #:return-type void + #:arg-types '())) diff --git a/agar/gui.scm b/agar/gui.scm new file mode 100644 index 0000000..f934bbd --- /dev/null +++ b/agar/gui.scm @@ -0,0 +1,32 @@ +(define-module (agar gui) + #:use-module (agar util) + #:use-module (system foreign) + #:use-module (system foreign-library) + #:export (init-graphics + window-new &window-main window-show + label-new)) + +(define* (init-graphics #:optional (drivers #f)) + ((with-string-inputs '(0) + (foreign-library-function "libag_gui" "AG_InitGraphics" + #:return-type int + #:arg-types (list '*))) + drivers)) + +(define window-new + (foreign-library-function "libag_gui" "AG_WindowNew" + #:return-type '* + #:arg-types (list unsigned-int))) + +(define &window-main #x10000) + +(define window-show + (foreign-library-function "libag_gui" "AG_WindowShow" + #:return-type void + #:arg-types (list '*))) + +(define label-new + (with-string-inputs '(2) + (foreign-library-function "libag_gui" "AG_LabelNew" + #:return-type '* + #:arg-types (list '* unsigned-int '*)))) diff --git a/agar/util.scm b/agar/util.scm new file mode 100644 index 0000000..85884f3 --- /dev/null +++ b/agar/util.scm @@ -0,0 +1,16 @@ +(define-module (agar util) + #:use-module (system foreign) + #:export (with-string-inputs)) + +(define (with-string-inputs ptr-nums fn) + (define (ptrfy-inputs arglist count) + (cond + [(null? arglist) '()] + [(memq count ptr-nums) + (cons (if (not (car arglist)) %null-pointer (string->pointer (car arglist))) + (ptrfy-inputs (cdr arglist) (1+ count)))] + [else + (cons (car arglist) + (ptrfy-inputs (cdr arglist) (1+ count)))])) + (lambda args + (apply fn (ptrfy-inputs args 0)))) diff --git a/examples/boids.scm b/examples/boids.scm new file mode 100644 index 0000000..7b06179 --- /dev/null +++ b/examples/boids.scm @@ -0,0 +1,10 @@ +(use-modules (agar core) + (agar gui)) + +(define* (main #:rest args) + (init-core "Boids Example" 0) + (init-graphics) + (let* ([window (window-new &window-main)] + [label (label-new window 0 "Hello!")]) + (window-show window) + (event-loop)))