JavaFX game on Raspberry PI

There seems to be a lot of cool things happening on Java SE embedded world.

Java SE for ARM has now been there for while, but it has been lacking support for properly accelerated 2D graphics — you could just dream running your AWT/Swing game in decent frame rates on embedded ARM platform. (**)

But that has just been solved by release of Java 8 ARM Early Access which includes JavaFX with OpenGL ES support on it. I have been reading that JavaFX is being recommended over AWT for creating UIs, and it seems like AWT is not going to be included in future releases of Java SE embedded.

So, sounds like there’s a chance for running Java based 2D game in Raspberry PI with decent (30-40) FPS, why not try it?

The plan

I thought transforming my trusty old Asteroid Race Android game would be nice way of trying this out, because it’s already Java, we just need to get rid of Android Graphics APIs and start using JavaFX instead.

Game will be developed+compiled in PC and then deployed to Raspberry PI for FPS analysis 🙂

Buildin’ it

I’ve put the full sources of JavaFX port available in GitHub — or you can just grab the prebuild JAR (asteroidhd.jar) from my Dropbox account.

If you’re planning to play with sources in Eclipse, make sure you need to add jfxrt.jar in classpath (Windows->Preferences->Available JREs->Edit). This game uses features of JavaFX 2.2, so check that you have right version. I used JDK 1.7.0_10 to compile and test it on PC.

To build the executable JAR file, I used javafxpackager which is included in JDK, so just run this command in Eclipse project folder after compiling project to produce the JAR file.

javafxpackager -createjar -appclass org.noxo.Game -srcdir bin -outdir out -outfile asteroidhd.jar

Gameloop & JavaFX canvas

With 2d sprite based games I personally prefer more maintaining sprites/bitmaps and drawing them via graphics context, instead of using scene based graphics.

Starting from JavaFX 2.2 there has been support for this via Canvas class, so you can do the stuff almost in good-old-AWT-way, by getting reference to graphics context and then do the drawing using methods (drawImage, drawString..) provided.

One difference in JavaFX (vs AWT) is that drawing operations by graphics context should be done from JavaFX application thread.

One way of implementing game-update-render loop, could for example look like this:

class Game extends javafx.application.Application
{
    Canvas canvas = new Canvas(1280,720);
    AnimationTimer animTimer;
    
    public void start(Stage stage) {
        
	Group gameNode = new Group(canvas);
        stage.setScene(new Scene(gameNode));
        stage.show();

        animTimer = new AnimationTimer() {

        @Override
            public void handle(long arg0) {
                gameLoop();
            }
        };

        // start running gameloop in javafx application thread
        animTimer.start();
    }
        
    public void gameLoop() {
        updateGame();
        renderGame();
    }

    public void updateGame() {
        // update your game logics here
    }

    public void renderGame() {
        GraphicsContext gc = canvas.getGraphicsContext2D();
        // draw your stuff using gc..
    }

    public static void main(String arg[]) {
        launch(arg);
    }
}

Installing to Raspberry PI

  • Have Raspberry PI with hard float Linux distribution installed on it
  • I had the 2012-12-16-wheezy-raspbian.zip installed from raspberrypi.org
  • Configure GPU memory to 128MB
  • Edit boot/config.txt => gpu_mem_256=128
  • Configuring this depends on board and Linux distro version
  • Change resolution to 720P (1280×720)
  • Edit boot/config.txt => hdmi_mode=4
  • Install Java 8 Early Access
  • Download from Oracle
  • Uncompress file anywhere (tar -xvf jdk-8-ea-b36e-linux-arm-hflt-29_nov_2012.gz)
  • Run the game
  • Push game .JAR file to Raspberry PI
  • Change in to Java 8 folder (jdk-1.8.0/bin)
  • ./java -Djavafx.platform=eglfb -jar /JAR_FILE_LOCATION/asteroidhd.jar

I did include average FPS counter in game. Raspberry PI seems to be able to run this game with stable ~35 FPS after bit of a jitter at startup (didn’t yet try to do any kind of optimization). I guess the lag at startup is due to cost of creating and initializing textures into GPU, but didn’t yet try to profile it.

I really like the new cool features available via JavaFX, it seems to also support H264 and video streaming nowadays, which has been lacking in Java for a long time. I’ve also been reading plans to have high level-scene-based 3d support in JavaFX – that’s really good news!