[gambit-list] Structs in FFI

David St-Hilaire sthilaid at iro.umontreal.ca
Fri Jul 25 08:54:37 EDT 2008


Will Donnelly wrote:
> Recently (this morning) I began using Gambit Scheme, and so far it has
> been a wonderful language to work with.  I like doing graphical stuff,
> so I'm trying to use Gambit's FFI to interface with the SDL libraries.
>  I've gotten everything to work so far, compiling and linking with
> libSDL from within gsc properly, but now I have two questions.
> 
> One, is there any way to specify, in comments or some such, what
> compiler and linker flags a file should have in a call to
> compile-file?  I'm using emacs as an editor, and the C-c C-k command
> is useless, since the file won't get linked to libSDL like it needs
> to. 

Er, not too sure about that one...

> Two, am I missing something when it comes to interfacing with C
> structs?  The only references that I've found
> (http://dynamo.iro.umontreal.ca/~gambit/wiki/index.php/Using_Gambit_with_External_Libraries)
> and (http://www.iro.umontreal.ca/~gambit/doc/gambit-c.html#SEC135)
> seem to contain horrendously complex code for something as simple as
> accessing a struct like
> 
> typedef struct SDL_Rect {
> 	Sint16 x, y;
> 	Uint16 w, h;
> } SDL_Rect;
> 
> from within Scheme.  Does anyone with more experience than I have some
> pointers on how to do this?

I'd first like to point out to you that a really nice but incomplete binding to 
SDL has already been written by Ken Dickey and can be obtained on the gambit 
code dumping ground 
(http://dynamo.iro.umontreal.ca/~gambit/wiki/index.php/Dumping_Grounds).

I don't know what is best to do when you need to deal with structs, as it is no 
very friendly to do so in gambit's ffi. What i usually to would be to have some 
c-lambda's that will get what I need to be done. Ex:

(define SDL_Rect:x (c-lambda ((pointer "SDL_Rect")) int16 "___result = ___arg1.x;"))

(define SDL_Rect:x-set! (c-lambda ((pointer "SDL_Rect") int16) void "___arg1.x = 
___arg2;"))

etc...

This sure is a bit of a pain, and I know there are better ways to do this, but 
they are too much unfriendly for me to use them.

Hope it helped!

David




More information about the Gambit-list mailing list