Graphics in Chicken Scheme on OS X

Tagged as scheme, programming, tutorial

CHICKEN Scheme is a fantastic Scheme system. It is pretty portable, and lets one interact with C relatively painlessly.

Getting graphics, specifically SDL, to work on OS X is rather painful. It's not really CHICKEN's fault though; SDL has requirements on which thread owns what.

Here's the quick way to get started using graphics in CHICKEN Scheme using the doodle library.

First, as a one-time thing, install all of the system dependencies.

# Install Cairo brew install cairo # Install SDL requirements brew install sdl sdl_ttf sdl_image sdl_gfx sdl_net

Next, install some CHICKEN dependency libraries ("eggs"):

chicken-install sdl matchable

The library matchable isn't a strict requirement, just a requirement for the example below.

You should have XQuartz installed. If you don't, go do that.

We need to install doodle separately as follows. We need to tell it where pkgconfig is from X11 so doodle can be built properly.

PKG_CONFIG_PATH=/opt/X11/lib/pkgconfig chicken-install doodle

We should be done with all of the one-time bits. Now we can write graphics code. Unfortunately, there are a few hitches. As mentioned previously, SDL has certain requirements on what thread main is in, and so on. This means that interactive graphics development on OS X is a bit of a pain, if not currently impossible.

In your Scheme file, say graphics.scm , you must have the following at the top:

(use matchable doodle) ;; Required on OS X. (declare (foreign-declare "#include<SDL/SDL.h>

")) (foreign-code "SDL_Init(SDL_INIT_EVERYTHING);") ;; graphics code follows...

Now we can write graphics code. See the doodle API for the functions you can use, and see the end of this post for an example application.

Once we've written our application, we can compile it. We need to link it with SDL and provide the necessary SDL compilation configuration parameters, which can be injected using the sdl-config utility program. The compilation line looks like this:

csc graphics.scm -lSDLmain `sdl-config --cflags --libs`

Now we should have an executable which we can run: ./graphics .

An example application, taken from the example on the doodle API page: