Jason’s babblings.

More awesome than a ten pound bag of flapjacks.

Archive for March 28th, 2010

Day 2: Let’s draw a map.

Posted by Plaidman on 28th March 2010

Today’s goal is to change the screen drawing method to use the second method described yesterday, and implement a tile-based map that Red could be drawn on and move around.

Taking a look at the sample code is overwhelming. It’s important to take code like this and poke with it, intentionally break things to see how they break, and just have fun. The debugger that comes with the Android Dev Kit is really nice and works well to see what’s going on in the sample. Once we’re done messing around, we’ll take the code and clean it up to its most basic elements: the game setup, the keyboard movement routine, and the drawing routine.

The map for the game is a grid of tiles, with each tile being a wall tile, floor tile, trap tile, switch tile, etc.

The first experiment was to draw the entire map on the screen at one time – even the tiles that weren’t drawn on the screen would be run through the drawing routine and put into memory. The map held 40,000 tiles (200×200) where each tile is 400 pixels (20×20). That means the routine would process and draw sixteen MILLION pixels with each time the screen would redraw. This resulted in a very poor frame rate -  4 frames per second – and would probably kill the battery dead in no time.

We should probably optimize this to calculate the number of visible tiles on the screen based on the device’s screen resolution (more on this later), and only draw those tiles for each frame. On the emulator, the screen fit a 23×17 section of the map, so we really only need to draw approximately 156,000 pixels each frame. This will improve the frame rate to faster than the LCD is physically able to draw, with time to spare for the inevitable extra logic in the game for enemies’ artificial intelligence and such. Reaching a hardware limitation is a very good thing.

As an added note, this graphic routine should automatically resize the visible map based on the hardware configuration of the device you’re using. If we were to configure the game based on the screen resolution of the lower resolution G1, the game would look like crap on the higher resolution Droid. Taking the resolution and screen size into consideration early will save a huge headache later when testing on multiple devices.

Current demo: Red is displayed with a two frame animation on a grid map. Pressing a direction will move him to a new grid location and update the direction he’s facing.

Posted in Android | No Comments »