July 05, 2008

cffi-sdl

I'm making progress on my "cffi-sdl" (haven't found a catchier name yet; ideas are welcome). The image above shows the latest version, which is capable of creating the surface on which we'll be rendering. If the surface seems somewhat empty, that's because I'm not issuing any OpenGL calls yet.

I'm taking my time with this, because I want to make sure that things are set up right. I'm having some (minor) problems with the binding though. For instance, SDL_Video.h defines SDL_VideoInfo as follows:

typedef struct SDL_VideoInfo {
 Uint32 hw_available :1;
 Uint32 wm_available :1;
 Uint32 UnusedBits1  :6;
 Uint32 UnusedBits2  :1;
 Uint32 blit_hw      :1;
 Uint32 blit_hw_CC   :1;
 Uint32 blit_hw_A    :1;
 Uint32 blit_sw      :1;
 Uint32 blit_sw_CC   :1;
 Uint32 blit_sw_A    :1;
 Uint32 blit_fill    :1;
 Uint32 UnusedBits3  :16;
 Uint32 video_mem;
 SDL_PixelFormat *vfmt;
 int    current_w;
 int    current_h;
} SDL_VideoInfo;

Note that the flags are defined as single bits in a 32 bit value. SWIG turns this into:

(cffi:defcstruct #.(lispify-sdl "SDL_VideoInfo" 'classname)
 (#.(lispify-sdl "hw_available" 'slotname) :unsigned-long)
 (#.(lispify-sdl "wm_available" 'slotname) :unsigned-long)
 (#.(lispify-sdl "UnusedBits1" 'slotname) :unsigned-long)
 (#.(lispify-sdl "UnusedBits2" 'slotname) :unsigned-long)
 (#.(lispify-sdl "blit_hw" 'slotname) :unsigned-long)
 (#.(lispify-sdl "blit_hw_CC" 'slotname) :unsigned-long)
 (#.(lispify-sdl "blit_hw_A" 'slotname) :unsigned-long)
 (#.(lispify-sdl "blit_sw" 'slotname) :unsigned-long)
 (#.(lispify-sdl "blit_sw_CC" 'slotname) :unsigned-long)
 (#.(lispify-sdl "blit_sw_A" 'slotname) :unsigned-long)
 (#.(lispify-sdl "blit_fill" 'slotname) :unsigned-long)
 (#.(lispify-sdl "UnusedBits3" 'slotname) :unsigned-long)
 (#.(lispify-sdl "video_mem" 'slotname) :unsigned-long)
 (#.(lispify-sdl "vfmt" 'slotname) :pointer)
 (#.(lispify-sdl "current_w" 'slotname) :int)
 (#.(lispify-sdl "current_h" 'slotname) :int))

Note that this time, the flags each take up a single 32 bit value. That is, we've lost the bit-locations for the fields.

The above is annoying, but I think that SWIG can't help it. I've checked the CFFI documentation, but I can't find any mention of dealing with single bits. (I still have to check the mailing lists and website though.) I can redefine this struct myself so that all flags are grouped into a single slot, and then use ldb calls to get the correct parts (this is what cl-sdl does), so it's no showstopper. It's just annoying.

Anyway, it's figuring out all of these little things which takes time to do, but I'm getting closer. The next step is to include the event-loop once the surface has been created, and then I can start doing some rendering. Once I'm there I'll also try to package everything up in an ASDF definition, just to make life easier.

1 comment:

Luke Crook said...

Correct, SWIG does not handle bitfields for CFFI. However the SWIG wrapper for lispbuilder-sdl contains the CFFI code for SDL_VideoInfo.

http://lispbuilder.svn.sourceforge.net/viewvc/lispbuilder/trunk/lispbuilder-sdl/build/sdlswig.i?revision=633&view=markup