Coding Naked in a Box of Rats

July 23, 2009

A template for building Scheme applications for iPhone/Mac OS X in Xcode

I have often played with the idea of writing Mac and iPhone applications in Scheme, but as a result of not knowing the right compiler to embed (and lack of experience in Scheme), I could not execute on it. While programming in Objective-C is definitely doable (and of course you can use Objective-C++), I like to have more options for native application development.

Luckily, this month, there was one blog entry about Gambit-C on the iPhone which was a great starting point (and James has since put out more interesting posts on the topic). In addition to the fantastic advice found there, I wanted a better integration within Xcode, since for now, it seems impossible to develop (legally) for the iPhone outside of the IDE (personally, I prefer setting up builds based on cmake and make them as cross-platform as possible). In particular, I wanted a build phase to compile from Scheme to C without having to invoke the Scheme compiler manually. I have created a template for this purpose for later use and have put it on github in case other people might be interested to use it. The source is released under the new BSD license, so feel free to experiment !

The principle used is similar to the one described in James blog – all Scheme modules defined in the project are compiled to C files and linked into a single executable. When using the Gambit-C compiler, one has to pay attention to the code that it generates: when linking statically, we need to:

  • provide the -link switch to the compiler command
  • include all the Scheme source files we want to link into the executable

If we would compile each file separately with the link option there would be a flat link file for each Scheme module and we would get linker errors due to duplicate symbols. Calling gsc with all the needed Scheme modules provides it with the necessary information to generate a single flat link file. The template assumes that all the Scheme files are in the scheme sub directory. In order to invoke the compiler with the correct parameters, I added an external target (a small Ruby build script) that scans the scheme directory and feeds it to the gsc command and it also includes a clean action that removes the generated C files.

To use the template in a different environment, the user simply needs to set the variable GAMBITC_BASEPATH, which could be as simple as /usr/local. This can be set either in the .MacOSX/environment.plist or the bash .profile script in the home directory. After a change to these, the user needs to log out and log in again so Xcode can recognize the change to the user settings. I personally prefer to install development tools somewhere in my home directory, which has the advantage that I can use different configurations and don’t pollute my system directories.

The contents of my personal GAMBITC_BASEPATH look like this:

$(GAMBITC_BASEPATH)/Debug
$(GAMBITC_BASEPATH)/Release
$(GAMBITC_BASEPATH)/Debug-iphonesim
$(GAMBITC_BASEPATH)/Release-iphonesim
$(GAMBITC_BASEPATH)/Debug-iphoneos
$(GAMBITC_BASEPATH)/Release-iphoneos

These are the directories that are the parameters to the –prefix option in the configure call when compiling gambit-c (see James’ blog). In fact, I simply built a version for each platform (Mac OS X, iPhone Simulator and iPhone Device) and symlink’ed the directories to reflect the layout above, since I do not need separate Debug and Release versions for Gambit-C.

In the build rules for the main target, I added a dummy rule which specifies the output files of the “compile scheme” target. This is the only part that I find a little ugly, because whenever the list of Scheme modules changes, this list needs to be changed, so the C compiler can include these files in the builds. I still find this solution better than checking in generated files into version control or including C files in the project tree which do not exist.

The github repository contains templates for a Mac command line tool and one for an OpenGL ES application on the iPhone. Since I am a game developer, the OpenGL ES template is most useful to me, but it is pretty easy to adapt the setup for other kinds of iPhone (or Mac OS X) applications. The initialization point in my template differs from the one described in James Long’s blog: the main.mm is unmodified, instead, the Scheme system is initialized when the view is initialized, because that is the place where I currently pull up my engine.

Happy Hacking :-)

June 18, 2009

A “great” game idea

Filed under: Box of Rats, iPhone — Wei-ju Wu @ 9:54 pm

I am currently working on the core mechanics for my current game project. I had the idea that it could be used as a base for a quick implementation of an air hockey game. A search for “air hockey” in AppStore returned 14 air hockey games. Oh well, that was too simple…

April 10, 2009

Bitmap Font Machine – Box of Rats’ first product

Filed under: Box of Rats — Wei-ju Wu @ 8:39 am

As previously mentioned, I have lately been pushing hard to get Bitmap Font Machine released. This is the first product by Box of Rats, which owes its existence to my inability finding a suitable bitmap font generator to support me in my current game development.

Bitmap fonts are fonts that are, as opposed to vector based fonts such as TrueType, stored in a bitmap. This has the disadvantage that scaling such fonts can result in quality loss, but when used in games, especially those based on 3D technology, they are pretty useful. Each glyph of a bitmap font can be texture mapped on a rectangle by specifying the proper coordinates within that bitmap. On systems with 3D display hardware (nowadays this means most systems), this is significantly faster than rendering a vector font. Another advantage is that the game does not need to be shipped with that that font in case a custom font is used, nor does the target system need to be able to display the original font format.

Now the straightforward way would be to create that font in Photoshop (or in my case, GIMP) and I did that for the first three times or so. When I write code, I get tired and bored after doing the same thing for the third time (and will start to look for a better way). Another thing was that this procedure was only practical for fixed width fonts like Courier, where I could simply derive the glyph positions from the equidistant grid, so I could not use proportional fonts.

As a result of this, I started a research on bitmap font generators and quickly found quite a number of tools for the purpose, but none of those quite fit was I was looking for. Practically every one was written for the same operating system. Redmond is around the corner, but I am nowadays mainly a Mac and Ubuntu User. I also wanted a very simple user interface where I could just hit a few buttons and immediately get a good feeling about how text would look like in my game.

After coming to the conclusion that I would not find the tool I was looking for, I decided to write it myself. I also decided to write it in Java, because I knew that it would allow me to control font rendering and writing on client machines easily and in a platform-independent way. I also thought that I should make the tool available for other game developers as well and that’s how Bitmap Font Machine came alive. I have retargeted it to run as a browser applet, so I did not need to worry about distribution  and provided some sample code (which is currently more for demonstration than for production purposes) to show how to use the bitmap fonts on Mac OS X or SDL/OpenGL based systems.

So far, this tool has been very useful to myself, and it allows me to try out different fonts much easier and quicker. I hope that it can be useful to other people as well. I plan to improve Bitmap Font Machine over time as I progress further with my game development projects, and additional suggestions are definitely welcome.

April 5, 2009

My first business: Box of Rats

Filed under: Box of Rats — Wei-ju Wu @ 8:37 am

I haven’t written any blog entries in a while. The main reason is that I have been busy forming my first business. Box of Rats is a game development studio, something I have been dreaming to do for a very long time – I was in primary school, when I drafted my first plans.

Admittedly my plans look more sophisticated 24 years later, but that’s probably not a bad thing.

I have now opened my business website and will hopefully release it’s first product (which is not a game !) in a few days.

Blog at WordPress.com.