Content tagged tutorial

Graphics in Chicken Scheme on OS X

posted on 2015-01-29 11:14:00

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>\n"))
(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:

(use matchable doodle)

;; Required on OS X.
(declare (foreign-declare "#include<SDL/SDL.h>\n"))
(foreign-code "SDL_Init(SDL_INIT_EVERYTHING);")

(define *paint* #f)

(define red '(1 0 0 0.3))

 (lambda ()
   (set-font! "Vollkorn" 18 red)
   (text (/ doodle-width 2)
         (/ doodle-height 2) '("Welcome to doodle!"
                               "Click the left mouse button to draw circles"
                               "Press ESC to leave")
         align: #:center)))

 (lambda (events dt exit)
    (lambda (e)
      (match e
       (('mouse 'pressed x y 1)
        (set! *paint* #t)
        (filled-circle x y 10 red))
       (('mouse 'released x y 1)
        (set! *paint* #f))
       (('mouse 'moved x y)
        (when *paint*
          (filled-circle x y 10 red)))
       (('key 'pressed #\esc)
        (exit #t))
       (else (void))))

(new-doodle title: "Doodle paint" background: solid-white)

This blog covers tutorial, scheme, retro, programming, math, lisp

View content from 2014-06, 2015-01, 2015-02

Unless otherwise credited all material Copyright © 2015-2018 by Robert Smith