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.

The beginning....

So here I am! I've been writing video games professionally for the last 6 years or so and at the same time have started to get interested in LISP, probably as result of the smug LISP weenies on

I've got started...I've read Practical Common Lisp online and bought a copy of ANSI Common Lisp. Of course the next big problem after that is choosing an implementation. Since I run Windows at home (it's just too convenient to have the same OS at work and at home) I'm not exactly spoiled for choice. I don't want to use a commercial implementation, so I'm playing around with CLISP, at least until the Windows port of SBCL becomes more stable. My rough guess (not based on anything concrete) is that CLISP have the same sort of performance as something like Ruby or Python. We'll see!

My first exercise was to get an accounting system for dealing with all the rent at my house written. Quite simple stuff really, but it got me familiar with writing, reading and editing LISP. At the same time I found myself playing a lot of multiplayer minesweeper under MSN messenger. I started thinking that it'd be quite cool to try and write a real-time (ie not turned based) version of this. So that's my current project, and this blog is going to try and track my progress.

Current status is a few ideas, and no visible working graphics library for clisp. That's my first task. I figured I'd try and get some bindings sorted out for Allegro. Why Allegro? Well, firstly I've used it before and secondly it was written by my mate and I think he'd find it quite cool to have lisp bindings for it!

So - I've gotta get Allegro built on my machine and then start hooking up functions for it. CFFI seems to be worth looking at for this.

Lets see how far I get before I get bored!