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?
** [There were few lower level Java OpenGL wrapper libraries (like JOGL) already available for ARM accelerated graphics.]
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 :)
I've put the full sources of JavaFX port available in GitHub -- or you can just grab the prebuild JAR one from end of blog.
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.
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:
Group gameNode = new Group(canvas);
// update your game logics here
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!