From: Ryan Raymond <rjraymond@oakland.edu>
To: Guile User <guile-user@gnu.org>
Subject: Custom foreign types
Date: Sun, 16 Jun 2024 22:13:24 -0400 [thread overview]
Message-ID: <CAGvJ-HSJmXMXd=02cpao-YqhOSder3NEtwqdiQ_7OLr2WrxO5Q@mail.gmail.com> (raw)
Hello, all.
I know we can create pointers to structs with make-c-struct, but I would
like to pass structs directly as arguments to foreign functions. I can do
that by using bytevector-uint-ref, and setting the size to that of the
struct, but when it is time to specify the type in pointer->procedure, I am
out of luck if the sizes don't match.
Here is how I am binding Raylib using FFI currently.
(use-modules
(rnrs bytevectors)
(system foreign))
(define rl (dynamic-link "libraylib"))
(define pp pointer->procedure)
(define df dynamic-func)
(define BeginDrawing (pp void (df "BeginDrawing" rl) '()))
(define ClearBackground_c (pp void (df "ClearBackground" rl) (list uint32)))
(define (ClearBackground color)
(ClearBackground_c (make-c-struct (list uint8 uint8 uint8 uint8) color)))
(define CloseWindow (pp void (df "CloseWindow" rl) '()))
(define DrawGrid (pp void (df "DrawPlane" rl) (list int float)))
(define EndDrawing (pp void (df "EndDrawing" rl) '()))
(define GetFrameTime (pp float (df "GetFrameTime" rl) '()))
(define InitWindow_c (pp void (df "InitWindow" rl) (list int int '*)))
(define (InitWindow width height title) (InitWindow_c width height
(string->pointer title)))
(define SetTargetFPS (pp void (df "SetTargetFPS" rl) (list int)))
(define WindowShouldClose_c (pp int8 (df "WindowShouldClose" rl) '()))
(define (WindowShouldClose) (= 1 (WindowShouldClose_c)))
(define width 600)
(define height 400)
(define title "This is a Guile-bound thinga")
(define (make-Color r g b a)
(bytevector-uint-ref
(u8-list->bytevector (list r g b a)) 0 (native-endianness) 4))
(define (make-Vector3 x y z)
(bytevector-uint-ref
(f32vector x y z) 0 (native-endianness) 4))
Is there a cleaner way to do this?
Ryan
next reply other threads:[~2024-06-17 2:13 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-06-17 2:13 Ryan Raymond [this message]
2024-06-19 22:16 ` Custom foreign types Maxime Devos via General Guile related discussions
2024-06-20 0:11 ` Matt Wette
2024-06-20 13:17 ` Matt Wette
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
List information: https://www.gnu.org/software/guile/
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to='CAGvJ-HSJmXMXd=02cpao-YqhOSder3NEtwqdiQ_7OLr2WrxO5Q@mail.gmail.com' \
--to=rjraymond@oakland.edu \
--cc=guile-user@gnu.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).