Latest Tweets:

Android Initiative: A small victory!

WARNING: This blog will give you some insights into writing a game loop in Android but this is by no means a tutorial!

My first android application has been a good deal like my break into web development; plenty of dead ends (often revisited). Teaching yourself a technology is much like mapping out a room in the dark in hopes of finding something. The strangeness of working with an SDK/Framework is that you need to familiarize yourself with how the creators intended for you to use it. Tutorials are key and sample code is very nice, my eternal thanks to Google for not being selfish about knowledge.

So today I figured out how to make a “game loop” by dissecting the “Lunar Lander” example app. The key is in understanding the “personality” of the core components in the Android framework.

Activity - An activity is basically the start of your app. If your application was a splash in the water then the activity class is the object being thrown; you pick how the rock looks, how to throw it and then off it goes. The meat of the application is in the View though.

View - The View object is where interaction takes place. What you draw on the screen, user interaction (key presses, screen touches) can be traced here, etc can happen in the view. Using the “splash in the water” analogy I would say that the View is the behavior of the ripple, both on its own as well as how it behaves to other things.

Unfortunately though, the Android framework is designed to make event driven apps which means that the core concept of a “game loop” has to be created by spinning off a thread of your own. Override the Thread.run() function and VIOLA! Game loop.

Thread - So back to the analogy… so we have the rock of our Activity, the splash (our intention) called View but unfortunately we want our little ripples to behave in their own time and so we enter the Thread. Consider the Thread you spin to be where you can update the splash. The thread can house all the logic, drawing instructions, physics, state machines, etc.

A shorter analogy could be that the Activity is the doors to the Emerald City, the View is the holographic image of the Wizard with smoke, bells and whistles and the Thread is the small man behind the curtain.

Coming from a paradigm of straight “C/C++” where the framework sits on top of my app, where a game loop is wherever I choose to place the while loop, the journey in search of an equivalent in a very well defined framework can be a bit daunting but now I have figured it out. I found the man behind the curtain, now I’m his boss… time to figure out what to do!