Game Development Reference
In-Depth Information
The method renderItems() renders springs and coins. For springs, we just use the one
TextureRegion we defined in Assets , and for coins, we again select a keyframe from the
animation based on a coin's state time.
private void renderSquirrels() {
int len = world.squirrels.size();
for ( int i = 0; i < len; i++) {
Squirrel squirrel = world.squirrels.get(i);
TextureRegion keyFrame = Assets. squirrelFly .getKeyFrame(squirrel.stateTime,
Animation. ANIMATION _ LOOPING );
float side = squirrel.velocity.x < 0?-1:1;
batcher.drawSprite(squirrel.position.x, squirrel.position.y, side * 1, 1, keyFrame);
}
}
The method renderSquirrels() renders squirrels. We again fetch a keyframe based on the
squirrel's state time, figure out which direction it faces, and manipulate the width accordingly
when rendering it with the SpriteBatcher . This is necessary since we only have a left-facing
version of the squirrel in the texture atlas.
private void renderCastle() {
Castle castle = world.castle;
batcher.drawSprite(castle.position.x, castle.position.y, 2, 2, Assets. castle );
}
}
The last method, renderCastle() , simply draws the castle with the TextureRegion we defined in
the Assets class.
That was pretty simple, wasn't it? We only have two batches to render: one for the background
and one for the objects. Taking a step back, we see that we render a third batch for all the UI
elements of the game screen as well. That's three texture changes and three times uploading
new vertices to the GPU. We could theoretically merge the UI and object batches, but that would
be cumbersome and would introduce some hacks into our code.
We are finally done. Our second game, Super Jumper, is now ready to be played. According to
our optimization guidelines from Chapter 7, we should have lightning-fast rendering. Let's see
whether that's true.
To Optimize or Not to Optimize
It's time to benchmark our new game. The only place we really need to deal with speed is the
game screen. We simply placed an FPSCounter instance in the GameScreen class and called its
FPSCounter.logFrame() method at the end of the GameScreen.render() method. Here are the
results on a Hero, a Droid, and a Nexus One:
Hero (1.5):
01-02 20:58:06.417: DEBUG/FPSCounter(8251): fps: 57
01-02 20:58:07.427: DEBUG/FPSCounter(8251): fps: 57
01-02 20:58:08.447: DEBUG/FPSCounter(8251): fps: 57
01-02 20:58:09.447: DEBUG/FPSCounter(8251): fps: 56
 
Search WWH ::




Custom Search