Libgdx goes HTML5

update #2: and another port, basilisk ported his game bumble, see below. F’ing A!
update: twbompo already ported his game QB :D, see below

I wrote a GWT backend for libgdx over the weekend and ported Super Jumper. Click on the below screenshot to try it out. You need Firefox/Safari/Chrome/Opera 11 with WebGL support. You can also run it in Opera Mobile on Android or on the Xperia Play browser. If nothing shows up, please check if WebGL works on your browser/OS/GPU combination, e.g. by trying some of the WebGL experiments. If nothing helps, i’m afraid your browser/OS/GPU isn’t cool enough. Use your mouse to navigate through the “menus”, use the left and right arrow key to move the dude ones you clicked on “Ready?”.

Stefan Wagner, alias twbompo, already ported his game QB. Click the image below:

And here’s another quick port by basilisk from Molded Bits.

This means we can now compile libgdx apps to Javascript and have the run natively in the browser. The code runs totally unmodified (save for 2 fixes due to Super Jumper formally being a GLES 1.0 game. Just removed glEnable(GL_TEXTURE_2D) and cam.apply(gl10). Check the SVN if you don’t trust my word, the gwt project for Super Jumper is in there as well :)). The port took 10 minutes, setting up the GWT project in Eclipse took most of that time.

What’s done

  • Graphics fully implemented, full GL ES 2.0 capabilities. There’s some space for optimizations.
  • Input 90% implemented, need a solution for Input#getTextInput()
  • Files 80% implemented, files are preloaded before the app starts. They will be fetched from the browser cache on second startup. Files are accessed via Gdx.files.internal, so your code can stay the same. Currently you can’t read binary files, i’ll try to work around that with a few tiny tricks, no promises.
  • Most of the graphisc classes like Mesh, ShaderProgram, SpriteBatch, TextureAtlas, BitmapFont and so on are fully working
  • All of the math classes are fully supported.
  • Almost all collections in utils/
  • More stuff i forgot.

You can check out this file to see which libgdx classes you can currently use. I have to gradually include more classes and check if everthing still works.

What’s left to do

  • Audio, most likely leveraging Sound Manager 2 which falls back to Flash if the audio tag doesn’t work
  • Preferences and local files, most likely implemented via the Web Storage APIs.
  • Binary file support. This one is a bit harder to do with regards to caching, but it should be possible. XmlHttpRequest to the rescue (screw IE)
  • Making sure the rest of libgdx compiles and works (scene2d, some stuff in the packages utils and g3d).
  • Figuring out a good way to embed GWT apps into any web page

What i’d like to integrate eventually

  • Web Sockets. The GWT Quake 2 port did i, so can we. This would mean the inclusion of a new module for libgdx that provides networking capabilities. So far this was unnecessary as all backends have access to the standard Java networking classes.
  • Mobile HTML5 apis, e.g. anything that PhoneGap exposes (multi-touch, accelerometer etc).
  • App cache support, so you can play offline.

What you won’t be able to do with the GWT backend

  • Native code, obviously. That also means no Box2D, unless you use JBox2D or the cleaned up version by the PlayN guys.
  • Concurrency, Javascript is single threaded, there’s no way to do anything fancy. However, we can easily abstract things like http requests via callbacks. Web workers seem to be in their infancy at the moment, and communication is done via message passing. A Java-compatible version of that is unlikely (e.g. concurrency primitives you usually work with won’t be available)
  • All the caveats of GWT apply. Reflection is not available, which is a pain because our awesome Json class relies heavily on reflection. A lot of standard Java classes aren’t available either in GWT. I did my best to port over a few crucial ones, like most of the java.io and java.nio package from Harmony/Avian/gwt-quake-2. Big shout out to those projects, and Stefan Haustein in particular, who did a lot of ground-work with gwt-quake. Standing on the shoulder of giants here 🙂
  • Local file storage will be limited. I’m new to this html5 stuff, but if i’m correct, then you can store just a few mb on the client side.
  • Other things i overlooked so far, e.g. integration of in-app purchases and so on. Given that we use GWT, the simple interface approach you already use on Android/desktop should work here as well.

Where’s the code?

In SVN. Check out the gdx-tests-gwt project and the superjumper projects. If you want to play around with things, check out the SVN trunk, import everything into Eclipse, make sure GWT is installed and away you go. I’ll post a full guide how to setup a desktop/android/gwt project once i’m done with the above list. Note that running GWT projects in hosted mode is terribly slow and not representative of the real performance. Think Android emulator…

What about PlayN?

After Google I/O last year i contacted the guys. They were not really interested in a collaboration (yeah, i was kinda disappoint). They were aware of libgdx at the time, which is unsurprising given both frameworks have a very similar architecture and target audience (it’s a pretty obvious design choice :)). Here’s why i think that libgdx might be more valuable in the end. It is also not meant as an attack on the project, i sincerely like what they guys pulled off (especially their non-Google contributors which seem to keep the project going). The work Ray Cromwell did on the GWT backend is fantastic, he’s a beast 🙂 Take the following as a justification for me to invest time in the GWT backend.

PlayN doesn’t expose OpenGL directly, as they fall back to Canvas or a DOM renderer if WebGL is not available, a good choice if you want to stay as compatible as possible. This might also be a wise decision should WebGL not get the adoption it deserves. With the GWT backend i do not aim for maximum compatibility. WebGL is a requirement, so IE is out (unless you use Google Chrome Frame maybe). I place my bet on WebGL, hence the full exposure of the GLES 2.0 like API in the backend, which enables you to do anything graphically. update: Stefan Haustein just send me a mail, he’s now working on PlayN (a good thing ™) and will add a GL ES 2.0 interface.

The Android port seems to have a few performance problems, which given the age of the framework is more than forgivable. This is something that can be solved over time. Libgdx does not sacrifice anything in that regard, it’s probably as fast as can be given it’s Dalvik/JVM heritage.

The Flash port is basically dead, which is kinda sad. In my opinion this was the big point for PlayN, Flash is everyhwere, HTML5 not yet.

There’s currently an iOS port in the works, based on IKVM and MonoTouch (sounds familiar?). I’m not sure if this will allow to debug the transformed Java/CLI code in MonoDevelop, but if it does it’s a pretty cool thing. What’s less cool is that MonoTouch costs 399$ in its most basic version. My work on the Avian based iOS backend is currently on ice, i can’t stem all of the work i’d like to do at the moment. Avian would mean no debugging support (unless i can add a JDWP bridge, which is unlikely, Avian is a complex beast). It currently also means no FPU support, the ARM backend emits software float code. An ARM emitter with FPU support is in the works it seems, not sure about it’s current state. There’s a lot of drawbacks using Avian, but in the end it is fun to do something a tad bit more complex from time to time, and it would mean i don’t have to pay for MonoTouch. PlayN has an easier time targeting other platforms like consoles should the Mono port come to fruitation. Avian can be made to work there as well, however, the dependency on OpenGL is a major drawback.

The build system of PlayN seems to be based on Maven with all its benefits and complexities. At the moment it seems to put off beginners, eventually that might be a big plus for PlayN. The more platforms you support, the harder it can be to setup a project. Whether Maven is the way to go in terms of dependency managment and project setup is another story. At the moment it seems to be hard to escape the claws of Maven.

Documentation is a big issue, for both projects. I like to think that we are a tad bit ahead in that regard, i might be totally wrong. Our forums seem to be filled with a lot of knowledgable people that actually help out, we have a few video tutorials to help absolute newcomers and mostly complete java docs. For libgdx, i think we are on the right track. We still lack a unified tutorial/dev guide system. PlayN is still in the process of gathering a community. Again, the age of the project probably plays a major role in that regard, and given time this can and will improve.

In the end it’s a matter of taste and targets. Choose your poison 🙂

Local files for libgdx

It’s been a while since i checked on this topic, and our own Nex provided a solution. I had a slight missunderstanding on how internal files work on Android, Nex thankfully corrected my stupidity 🙂

What are local files? On Android, your application has a private directory it can store files in. These are usually refered to as internal files. Since we use the “internal” name for the assets/ directory, we decided to call these type of files “local” instead. Other applications or the user do not have access to this directory (unless they are rooted, then all bets are off). The local files directory is usually stored on the internal storage as far as i understand, so you shouldn’t go crazy with storing GBs of data there.

Our new FileType#Local allows you to write to that directory. On the desktop it will resole to the working directory, usually the directory you started the jar from. On Android it resolves to the internal directory of the application.

The local storage is always available, which is not true for external storage. You can create directories, list directories and so on and so forth, just as with external or absolute files. Local files are read/write.

Thanks for the addition Nex!

Create BitmapsFonts on the fly with gdx-freetype

Hiero or BMFont are nice tools when it comes to generating bitmap fonts offline, adding fancy effects like dropshadows and similar things. However, this also means that you have to generate various versions of your fonts, for different screen sizes and resolutions, a tedious process, that is not exact.

For the Reddit app i’m working on i needed something more flexibel. Thus i wrapped FreeType, the goto FOSS solution when it comes to font rendering. The code can be found in the gdx-freetype project in SVN. It allows you to generate BitmapFontData and BitmapFont instances on the fly from TrueType font files. Here’s how you use it:

Remember to dispose the generator once you are done generating fonts for a specific TrueType file. The generateData method has overloads that allow you to specify more things, such as the characters you want the BitmapFont to contain and so on.

To use gdx-freetype, grab the latest nightlies, link gdx-freetype.jar and gdx-freetype-natives.jar to your desktop project, link gdx-freetype.jar to your Android project, and copy the armeabi/libgdx-freetype.so and armeabi-v7a/libgdx-freetype.so files to your Android project’s libs/ folder, just like with the libgdx.so files.

Caveats:

  • If you use to big of a size, things might explode. BitmapFonts still only support a single atlas page at the moment, something i’ll try to fix in the next couple of weeks.
  • Asian scripts “might” work, see caveat above though. They contain just to many glyphs. I’m thinking about ways to fix this.
  • Right-to-left scripts like arabic are a no-go. The layouting “algorithms” in BitmapFont and BitmapFontCache have no idea how to handle that.
  • Throwing just any font at FreeType is not a super awesome idea. Some fonts in the wild are just terrible, with bad or no hinting information and will look like poopoo.

That being said, i’ll try to improve support of more complex scripts, arabic would be nice 🙂 But i guess there’s a reason for this bug on the Android tracker.