Saturday, April 30, 2011

Android NDK Profiler Example

I've added an example to the android-ndk-profiler project based on the native-activity from Google's Android SDK. As Google Code doesn't let you mix'n'match licences, and importing a bunch of random code into my SVN seemed a bit crap, I've not included the original source in the repository.

What I have done is supply a patch and a script to copy the code from your NDK installation, patch it up to use profiling, update the build scripts and then build the example. You'll need to have the "patch" program installed to get this working, or apply the changes manually.

Since this is Android 2.3 only, I've only been able to test it on the emulator. Also the emulator doesn't like armeabi-v7a, so that remains completely untested - I'd stick to using APP_ABI := armeabi for profiling for now.

Hopefully this'll clear up any misunderstandings like those mentioned in the comments on the last post I made on the subject. Full details are on this wiki page about getting the example working.

Saturday, April 09, 2011

Why are my textures not showing up on Android?

What a disaster the initial week of "3D Space Game" has been! I recieved lots of 1 star reviews with vague hints about what the problem was - "doesn't work", "goofed up". When I realised there was a problem and added a note on the market asking people to email their problems before "down voting" I got a couple of very helpful bug reports.

It turns out that on the Galaxy S and other larger-screened devices all of the textures I created from PNG images were not showing up. This meant that the control buttons and menu icons just show as white rectangles, completely unusable.

The usual reason for the white rectangle effect is that the size of the texture you're trying to use is not a power of 2. Textures have to be 32x32, 64x32, 128x512, etc - you can't have a 257x123 sized texture (unless it's on the Android emulator, which doesn't care about texture size). I even mentioned this in my "lessons learned" post in January. The code I use creates a texture straight from the PNG like this:

Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.my_image);
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0);

The size of the texture is taken from the PNG, which I had carefully made sure was a power of 2. On my HTC Magic the textures show up fine, so I thought everything was working correctly.

Well, it turns out that the BitmapFactory uses a default set of options that includes a "resize" flag. The flag is by default set to true, or "resize the image to screw up Open GL textures", as it should be known. The fix for this was to create my own set of Options and set the flag to false like this:

BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inScaled = false;
Bitmap bmp = BitmapFactory.decodeResource(getResources(),
                R.drawable.my_image, opts); /* pass in opts */
GLUtils.texImage2D(GL10.GL_TEXTURE_2D, 0, bmp, 0);

Problem solved. Let's hope for a reversal of the 1-star trend, eh readers?

Saturday, April 02, 2011

3D Space Game for Android

This is pretty much "Elite for Android" [edit - no longer available due to copyright concerns]. It's a port of a port of a remake of a fan-tweaked version of Elite. I first wrote this as a remake of Angus Duggans's Elite A for the GBA (Elite A being a tweaked version of the original Elite on the BBC Micro), ported that to the DS and now here it is on Android. It's not been without problems due to a severe lack of buttons on a phone. This version adds in "classic mode" to play the game without the Elite A modifications.

I started the Android port back in October 2010 and it was more or less complete by December 2010. I've spent the months since then squashing bugs and refining the controls. Hopefully it will work on a lot of devices - I've been testing on 2009 vintage hardware - but there's no way to know for sure. Try it and let me know. In particular Samsung really threw a spanner in the works by not supplying a d-pad or trackball on the Galaxy S.

There are still some known problems in this first release:
  • Yellow line under title is not always shown
  • Sprite text still shown on panels
  • Long press should show route to planet (bit flakey, ditto Galactic Hyperspace)
  • Text occasionally does not update
Most of these are due to the GBA/DS heritage, but none are show stoppers. I'll be updating with fixes as people complain :-) Also, the Nintendo DS and GBA "backports" of this code are not ready, so this is Android-only for now. There are still some outstanding problems that I need to fix on the DS, and the GBA code is further off, but I'm starting to get project fatigue here and need a change.

This video shows the gameplay, it's a little choppy - if anyone knows a good way to video off an Android device I'd love to know. I used droid@screen for this, which seems to be the best option out there, but it doesn't record video. I had to record the X11 window separately using xvidcap and this made things slightly worse. Anyway, on with the show: