unofficial mirror of guile-user@gnu.org 
 help / color / mirror / Atom feed
* Custom foreign types
@ 2024-06-17  2:13 Ryan Raymond
  2024-06-19 22:16 ` Maxime Devos via General Guile related discussions
  2024-06-20  0:11 ` Matt Wette
  0 siblings, 2 replies; 4+ messages in thread
From: Ryan Raymond @ 2024-06-17  2:13 UTC (permalink / raw)
  To: Guile User

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2024-06-20 13:17 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-06-17  2:13 Custom foreign types Ryan Raymond
2024-06-19 22:16 ` Maxime Devos via General Guile related discussions
2024-06-20  0:11 ` Matt Wette
2024-06-20 13:17   ` Matt Wette

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).