Saturday, February 25, 2006

Hello World!

I've got Allegro's exhello.c working from within clisp. It's quite cool to be able to type stuff in a SLIME REPL and get an Allegro window up.

For what it's worth here's the code:

;;; The hello world example
(defun test-hello-world ()

(unless (set-gfx-mode *gfx-autodetect-windowed* 320 200 0 0)
(set-gfx-mode *gfx-text 0 0 0 0)
(error "Unable to set graphics mode"))

;; set_palette(desktop_palette)

(clear-to-color *screen* (make-col 255 255 255))

(textout-centre-ex *screen*
"Hello, world!"
(/ (screen-w) 2)
(/ (screen-h) 2)
(make-col 0 0 0)


Things that are slightly different from the straight forward C version are:
  • the with-allegro and with-acquire-screen macros that wrap up the install_allegro / exit_allegro calls inside unwind-protects
  • screen-w and screen-h are actually macros (just like they are in the C version) but unlike the C version macros look different whereas in C they just look like variables
install_allegro requires a pointer to errno. I couldn't find a way to get hold of that from CFFI, but it looks like passing a NULL pointer works. Dodgy!

Also read-key doesn't seem to actually work until I've switch focus a few times. It's fine in full screen mode. Odd.

Wrapping stuff up with CFFI is an absolute breeze. Here's a few examples:

(defcvar "screen" :pointer)

(defcfun "clear_to_color" :void
(bitmap :pointer)
(color :int))

(defcfun "acquire_screen" :void)

CFFI automatically converts the C-style names into more LISP-style ones. So screen becomes *screen* and clear_to_color becomes clear-to-color. You can provide your own LISP name as well if you want. CFFI just smells of quality.

So what problems did I have? Well they were all to do with getting set up - once that was done it was a breeze actually hooking things up.

The first mistake was to try and build Allegro from sources in Microsoft Visual Studio 2005 express. VS2005 doesn't come with the platform SDK or DirectX SDK and so I had to download and install those. Then it turned out that the Allegro script didn't have the changes for msvc8 that the fix.bat script has. Then I considered that I'd have to try and set my environment variables up. Then I downloaded the binary distribution of Allegro for VC8!

That was my second mistake. I don't know about manifests and at this point I don't care what they are or why they are. But I do care that every time I tried to link against alleg42.dll I'd get an error about not being able to find msvcrt80.dll. This seems to be a well known problem and the quickest fix of course would be to just download the allegro binary compiled in an earlier version of VC.

Armed with that I tried to get CFFI working. That needed ASDF so I had to get that working. Now all of the docs do say how to set up search paths, but it is kinda buried deep. It took a bit of fiddling, but I finally got them all talking to each other.

All in all I'm quite pleased with that. It looks like getting the Allegro interface isn't going to be too hard to do. I think my next task might be to try and get bits of the main test app up and running.


Post a Comment

<< Home