Pages

Sunday, 8 July 2012

Retro effects with Android

A few weeks back I was browsing Paweł Głowacki's blog and saw his write-up on implementing both a parallax starfield as well as a 3D starfield using Delphi and FireMonkey. Quite taken with the classic starfield demo idea I though it might be neat to have a go at putting together an Android implementation in Pascal using Oxygene for Java.

As Paweł did, I used the basic Python logic from http://codentronix.com/2011/04/27/a-cool-parallax-starfield-simulation-using-python to build up the parallax version from and then used the logic from http://codentronix.com/2011/05/28/3d-starfield-made-using-python-and-pygame for a basic 3D implementation. Then I bumped into a nice DHTML JavaScript implementation of an interactive starfield at http://www.chiptune.com/starfield/starfield.html, where moving the mouse changes the aspect of travel through the starfield.

So I knocked up some equivalent logic in Oxygene in an Android application and uploaded the result to the Android Market, oh, I mean Google Play. You find the free app through this link if you want to check out the results. The interactive starfield has some settings to customise the number and speed of stars as well as whether touching the screen changes the travel aspect.

StarfieldAppMenu StarfieldAppSettings

StarFieldApp StarFieldApp2

Clearly the logic is reasonably straightforward – in each case it’s really just dots of various sizes drawn on the Canvas of a SurfaceView. This is done again and again after having moved the positions of the stars in ‘space’, but of course care must be taken to be CPU-friendly. To that end an Android Handler is used and each time a frame is drawn, the next one is scheduled in for some small time period later using a call to its postDelayed() method.

Anyway, I was quite pleased with how this little app turned out, and how it gave me an opportunity to go through the Google Play application publication process. And then I started thinking about other old effects from the demos that used to be quite popular in the late 80s and early 90s…

My favourite was the plasma effect: an undulating oozing flow of colours shifting through the screen. A bit of research helped me understand the general principle of how the plasma effect is implemented, with a plasma function at the heart of it. I found a selection of different plasma functions and rolled them into a configurable Android plasma app built with Oxygene for Java, available for free at this link:

Plasma1 Plasma3 Plasma2

Plasma4 Plasma5 Plasma6

These plasma effects are implemented by pre-assigning an interesting palette of colours and then iteratively calculating the value of each pixel of the plasma surface and plotting it on a bitmap. To keep things performant the plasma surface is typically rather less dense than the pixel count of a smart phone screen and so this bitmap is then stretched onto the Canvas of the underlying SurfaceView. However, to minimise the evident pixellation some of the effects actually render virtual pixels and use a random jitter (x & y offset) in conjunction with opacity to smooth out the edges.

Having been bitten by the bug of retro demo effects I then bumped into the Demo Effects Collection Open Source project. This shows, in C++, how a whole raft of old demo effects are implemented, so I picked a few other effects I had a nostalgic affection for and concocted Retro Effects, another free Android app built with Oxygene for Java and available at this link. This one has a fire effect, a particle explosion, shadebobs and copper bars (as well as a starfield and plasma implementation for completeness):

Fire Shadebobs

Copperbars Explosion2

This has all been a terrific trip down memory lane for me, resurging my interest in the old demo effect scene, and using Oxygene for Java has allowed me to build the apps on my favourite mobile platform, Android, using my favourite language syntax, Pascal.

Oh, just in signing off I should mention that I also uploaded a couple of live wallpapers, one with the interactive starfield and one with the configurable plasma functions. Live wallpapers are quite a nice distraction and needn’t be as much of a battery hog as they are sometimes made out to be (especially if implemented carefully). The Android live wallpapers do, however, have a nominal download charge associated with them.

I’ve been porting some of the Android SDK demos from the original Java to Oxygene, including the live wallpaper demo (a rotating cube and a rotating dodecahedron), so I’ll probably blog some technical details on how these things are built at some point in the near future.

No comments:

Post a Comment