Nov 18, 2009

Using sdljava with Scala

After fixing up sdljava, I decided to try it out with Scala. Turns out it works really well!

Here is the code to get a simple window that closes when you click the close button:
import sdljava.SDLMain
import{SDLVideo, SDLSurface}
import sdljava.event._

object SDLTest{
def main(args: Array[String]){

var framebuffer = SDLVideo.setVideoMode(800, 600, 32, SDLVideo.SDL_HWSURFACE)

var ev : SDLEvent = new SDLQuitEvent()
var loop = true

while (loop){
if ((ev = SDLEvent.pollEvent) != null){
if (ev.isInstanceOf[SDLQuitEvent]){
loop = false

This looks almost exactly like it does in C (I probably code Scala/Java with a C accent anyway). I might decide to start adding stuff to sdljava so that you don't have as much boilerplate as you do here, or I could even add in some Scala-only stuff to bind functions as callbacks or something. But I probably won't, sorry...

Anyway the hard part is actually getting this junk to compile. If you check out my sdljava repo here you can grab all the files needed for compiling and executing provided you're running 64-bit Linux, otherwise you'll probably have to download and compile the library yourself.
Drop those into the same folder as your Scala file, and do this to compile:
scalac -cp ./sdljava.jar MyFile.scala
That should work fine (if your code doesn't have any errors). The more annoying part is getting it to actually execute. Unfortunately it seems like when you use the actual scala program, it does not pass parameters to java. So you have to call java directly:
java -Djava.library.path=. -cp /path/to/scala/lib/scala-library.jar:./sdljava.jar:. MyClass
This is because sdljava uses native libraries which are not looked for in the classpath, you need to pass them in the java.library.path variable. Scala does not pass this to java when it is executed, so you have to call java manually.

Anyway, this should get you started to whatever SDL program you want!

No comments: