[gambit-list] FFI and SCMOBJ to C representation
Christian Jaeger
christian at pflanze.mine.nu
Sat Dec 29 11:37:49 EST 2007
Adrien Pierard wrote:
> Hello,
>
> I'm currently writing an API for a simple 2D graphics library for
> Gambit, based on TinyPTC and SDL.
>
> However, it seems that I have some issues with conversion of Scheme data
> to C.
>
> Here is the API idea:
> (PTC-OPEN name width height) creates a window
> (PTC-CLOSE) closes the window
> (PTC-UPDATE buffer) blits the buffer to the window
>
> I chose to represent the buffer with a vector. I'd even prefer an
> U32VECTOR for colors are represented by an unsigned integer, however.
>
> The update function works like this:
>
> int scm_ptc_update (___SCMOBJ) {
> int idx;
> ___SCMOBJ ___temp; // for the type test
> if ( ___VECTORP(obj) ) {
> for(idx=0 ; idx < width * height ; idx++ ) {
> buffer[idx] = ___INT ( ___VECTORREF (obj, idx));
>
IIRC the ___VECTORREF etc. macros expect the index as scheme object,
thus: ___VECTORREF(obj,___FIX(idx)).
> }
> // Call the underlying update function
> return (( ptc_update (buffer)) == PTC_SUCCESS ? ___TRU : ___FAL );
> } else {
> return ( ___FAL );
> } };
>
>
> If the test program fills the vector with
>
> (vector-set! buffer (random-integer 255))
>
(not relevant, but (random-integer 256) should be the correct expression)
> I see large vertical and periodical stripes, and , roughly, grey noise,
> instead of blue noise. Printf-ing the values shows a lot of them greater
> than 255.
> Therefor, I reckon that my conversion is not correct.
>
> How should I then read a Scheme int32 value to store it in C ?
>
I think it's only the index problem that's wrong.
But you could make your life simpler like this:
use a u32vector as your buffer, then in the C code instead of copying
everything just call (or so, from memory, not tested as typed):
ptc_update (___CAST(unsigned int*,___BODY(vec)))
> Subsidiary question: how can I call pp or pretty-print from withing C?
> To do something like ___PRETTYPRINT(obj);
>
The official way would be to use c-define to create a wrapper around pp
and then call the generated C procedure from your C code. (I still
haven't figured out the inofficial way ;).)
Christian.
More information about the Gambit-list
mailing list