[gambit-list] FFI and SCMOBJ to C representation

Adrien Pierard pierarda at iro.umontreal.ca
Sat Dec 29 10:25:02 EST 2007


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));
    }
    // 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))

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 ?




Subsidiary question: how can I call pp or pretty-print from withing C?
To do something like ___PRETTYPRINT(obj);


Cordially,

Adrien.

-- 
"I am not a Church numeral; I am a free variable!"
(The Scheme Underground)
For every complex problem, there is a solution that is simple, neat, and wrong.



More information about the Gambit-list mailing list