Re: Custom foreign types
Matt Wette <[email protected]>
| Newsgroups | gmane.lisp.guile.user |
|---|---|
| Message-ID | <[email protected]> |
On 6/16/24 7:13 PM, Ryan Raymond wrote: > 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 Since Color is a struct of four chars you need to declare ClearBackground as (pp void (df "ClearBackground" rl) (list (list char char char char))) Matt