Saturday, February 18, 2012

Episode 4 - Grinding through a Sprite System Prototype

Coordinate woos

Picking a convenient coordinate system for a 2D game environment turned out to be a bit harder than expected. Working purely pixel-based sounds nice in theory (coming from an ancient C64 / VGA background), until you realize that you are designing things to run on a wild variety of screen dimensions.

So then I tried working with pure abstract floating point coordinates, assigning the sprites a completely free scale, only maintaining their ratio.  As it turns out, this doesn't work as well as in a pure 3D environment: rescaling bitmap-based sprites to arbitrary sizes does not always come out pretty.

I ended up with a hybrid solution where viewport coordinates are based around the ratio-adjusted unit square, but individual sprite size is in pixels. We'll have to see if it holds up once I have to implement real game stuff.

Alpha woos

All my alpha-blended sprites had ugly black edges around the corner. As it turns out, Android's default approach to texture loading (GLUtils.texImage2D) premultiplies the alpha channel.

Fixing the texture loading seems a bit tricky to implement efficiently, but as a workaround I simply changed the rendering blend function into (GL_ONE, GL_ONE_MINUTES_SRC_ALPHA). This might come back to hunt me, as premultiplying alpha kills information in the other channels that might be important when using a texture for other purposes than simple alpha-blended display.
Proper alpha-blended sprites go in...
...weird borders come out!

The final result looks like the screenshot below. Notice how the pixel aspect ratio of the physical phone is different, just pretend the screenshot is in proper 16:9 ratio. Also notice how the graphics are all placeholders, just building the technology here...
These sprites move around. That's kinda the point.

- Created a local Subversion repository for my Android work-in-progress using TortoiseSVN. Man, I'd almost forgotten how Subversion is cool that way. Considered experimenting with Mercurial in stead, but decided not to shoot myself in the (distributed) foot at this point.
- Replaced the default TortoiseSVN merge/diff tool with WinMerge. Seriously, if you are still on TortoiseMerge, make sure to do the same... cause you are missing out! Make sure to set the WinMerge font to something sensible (like Consolas 10), and you'll never want to go back. The syntax highlighting alone is worth the switch.
- Learned that it is really hard to predict how colors will come out on the Samsung Galaxy SII AMOLED screen. The screen of my little Dell Inspiron Laptop literally pales in comparison.
- Learned that it is rather difficult to get a proper movie recorded from an Android application without dedicated hardware. Might end up recording the actual phone with a camera.