Android and OpenGL

The last couple of days I’ve been digging myself into OpenGL development. So far I build all my games using the Android Canvas, but I thought it would be time to make the jump to OpenGL. Since it’s supposed to be faster..

I’m really a noob when it comes to OpenGL coding, so I had to learn everything and spend most of my time reading up on the finer details of how to get an OpenGL app running. I wrote a simple test project and probably rewrote it close to a hundred times.

My first conclusion: there are many problems when it comes to getting fast drawing going!   Once I finally managed to get sprites drawn onto the screen, I quickly found out the various problems with drawing multiple sprites onto the screen.

The first solution to the speed problem was what’s most common known as “Sprite batching”. This basically means you collect all the data for all the sprites and then dump them as one collection into OpenGL.  This gave a great speed up, but still shows a huge weakness: this batching only works if all your images are in the same texture.. cause swapping textures is another bottleneck and not possible when batching sprites.

The texture problem is something that you can work around: simply put all your monster sprites or all your background-tile sprites in one bitmap/texture. Not uncommon for a game to have that, so not a big problem.

Another problem with drawing the “sprite batch” quickly is having to use FloatBuffers and their speed on Android is non-existing.. no speed.. it’s slow, extremely slow.  A trick that I found used in most cases is to use native code to memcpy the data. I borrowed the code from the excellent LibGDX (check it out) and this gave another speed bump.

Now my engine was really going, and doing a little public beta test gave most results to be positive.

BUT it also showed some weakness. First: my own Xperia X10 device should be pretty powerful, yet it didn’t get both my benchmark as my game code running at a 60fps.. strangely even a single sprite feels sluggish. Might just be my coding skills, or doing something wrong that is only noticed on specific devices.. I have no idea!

Another weakness: some people, including me, testing the benchmark on a Nexus1 saw nothing but smooooooooth movement with 1024 sprites bumping around.. some other people, also testing on Nexus1 devices, saw slowdowns happening with as low as ~500 sprites.  This happened to a couple of devices, running stock android versions. Go figure!

My current conclusion is one full of frustration and I might get to new insights in the near future. For now it seems that the same devices that had problems running my canvas engine at 60fps are also having problems running my new OpenGL engine at 60fps. It might run a bit faster, but the unstable results of the OpenGL on same-type-devices makes me afraid of using it.  I probably am missing something, seeing as games who do a lot more are running a lot smoother.. but for now I’m done with it.

I’ll have to do some cleaning up to the code, and then I’ll post it on this brand new blog for others to download and tinker with it.  Worst case is that someone finds ways to improve my code and fixes it for everyone ;)


Bookmark the permalink.