Android Engine on the way?

For a while I’ve wanted to make a simple game in Android. I’ve done simple apps before, such as a scalable test app for A* Pathfinding (which I will write about later) and silly soundboards, but nothing that can actually be played like a game.

The main element any game needs is a 2D canvas on which all the graphics are drawn. It is understanding how to make one, update the contents and render it that kept me back. I’ve also created a few simple Live Wallpapers, but with these all the hard work is done for you by extending an existing class, android.service.wallpaper.WallpaperService (and in-turn extending WallpaperService.Engine).

In order to create an interactive graphical experience outside a pre-built Live Wallpaper Engine, I had to understand how Android handles Canvases, drawing calls and how these are displayed. I had tried doing a similar update() -> render() -> fpsDelay() approach as in my Java games, but an endless loop such as this created in the Constructor of an Android app halts everything.

And I mean everything.

You can’t do any other work in an Android app until the Constructor is done, unlike Java. This is because all the drawing activity is done asynchronously AFTER the application has been created (that is, onCreate() has completed).

To get round this, I found out I could extend SurfaceView and call my update() function in the onDraw() call to keep a cycle going. After some attempts at this, I am pleased to discover it works! Maybe I could make a port of ‘dungeons’ or something similar…

Here is a screenshot from my AVD (Android 2.3.3 Gingerbread), next to a screenshot from my Galaxy S (Android 4.1.2 Jelly Bean, thanks to Cyanogenmod) running the engine:

EngineTestFPSEmu

You can see that performance is much greater on actual hardware!

In the end, the line that gave me most trouble was not including this:

setWillNotDraw(false);

Huh.

I also found that after I’d ‘closed’ the app, my phone was extremely unresponsive until I re-opened it and killed it manually. Hence, this is needed for now:


@Override
public void onPause() {
finish();
}

Which causes a Force Close but I can live with that for now.

Advertisements
1 comment

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: