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!
In this blog entry we'll build H/W accelerated version of MonoGame.Tetris example for Raspberry PI.
Raspberry PI does not have HW OpenGL libraries for accelerated graphics, instead it has OpenGL ES 2.0 libraries (and ES 1.1 embedded in same libraries) for acceleration. However it's possible to run OpenGL applications on RPI, but rendering will fallback to software OpenGL renderer (Mesa).
By default MonoGame for Linux uses OpenGL and can be executed with few tricks, but performance is not good.
I've baked an experimental MonoGame (based on MonoGame 2.0) for OpenGL ES 2.0 based desktop environments, in practice implementation was only about mixing Android GLES and Linux versions of MonoGame, so I don't take any credits for this :)
There's now also fork of MonoGame 3d in GitHub. Start observing it, since it's based on forthcoming release on MonoGame which has a lots of new cool features :)
MonoGame 3d for RPI is now out, check out: http://www.youtube.com/watch?v=jwbYe6eoZgE&feature=youtu.be
Here's a short checklist for running prebuild Tetris which is available as tetris_raspberry.gles2.zip at the end of this page.
After the required software has been installed start X, open terminal, and "cd" into directory that tetris_raspberry_gles2.zip is unpacked and type:
Reason why I'm using the Debian 6 instead of 7/Wheezy is that Mono seems have issues handling floating points on ARMHF (hardware floating point architechture).
I've been using MonoDevelop 126.96.36.199 and Windows 7 to build the project.
Make sure build target is set to Net 3.5 in MonoGame and Tetris projects, since Mono 2.6 does not support Net 4.0.
First we need to checkout source code for the modules:
Compile MonoGame against OpenTK and then Tetris against MonoGame, if you were lucky there were no errors, just
lots of meaningless warnings :)
Copy the compiled binaries to Raspberry PI.
We need also to copy VideoCore GPU libraries to folder were compiled binaries are from /opt/vc/lib. Copy libEGL.so and libGLESv2.so.
This step is not needed if you trying prebuild Tetris, since tetris_raspberry_gles2.zip already has patched libEGL.so included.
Default Raspberry PI VideoCore OpenGL ES drivers (/opt/vc/lib) does not support attaching into X11 surface, and
we need to tweak one of GLES library (libEGL.so) file to get things working.
This hack is not complete since it's hardcoded to create 800x600 window, so some work is still needed.
Now cross your fingers, and try executing Tetris.exe -- works for me :)
MonoGame sound system is based on OpenAL, so if you plan to use xna.SoundEffect (even without invoking play),
the following stuff is needed:
The last step is not necessarily needed on all distros, since sound module is probably preloaded.
That's it :)
1-2 of 2