Game Development Reference
In-Depth Information
public void
renderBackground() {
batcher.beginBatch(Assets.
background
);
batcher.drawSprite(cam.position.x, cam.position.y,
FRUSTUM
_
WIDTH
,
FRUSTUM
_
HEIGHT
,
Assets.
backgroundRegion
);
batcher.endBatch();
}
The
renderBackground()
method simply renders the background so that it follows the camera. It
does not scroll but instead is always rendered so that it fills the complete screen. We also don't
use any blending for rendering the background, so that we can squeeze out a little bit more
performance.
public void
renderObjects() {
GL10 gl = glGraphics.getGL();
gl.glEnable(GL10.
GL
_
BLEND
);
gl.glBlendFunc(GL10.
GL
_
SRC
_
ALPHA
, GL10.
GL
_
ONE
_
MINUS
_
SRC
_
ALPHA
);
batcher.beginBatch(Assets.
items
);
renderBob();
renderPlatforms();
renderItems();
renderSquirrels();
renderCastle();
batcher.endBatch();
gl.glDisable(GL10.
GL
_
BLEND
);
}
The
renderObjects()
method is responsible for rendering the second batch. This time we use
blending, as all our objects have transparent background pixels. All the objects are rendered in
a single batch. Looking back at the constructor of
GameScreen
, we see that the
SpriteBatcher
we use can cope with 1,000 sprites in a single batch—more than enough for our world. For each
object type, we have a separate rendering method.
private void
renderBob() {
TextureRegion keyFrame;
switch
(world.bob.state) {
case
Bob.
BOB
_
STATE
_
FALL
:
keyFrame = Assets.
bobFall
.getKeyFrame(world.bob.stateTime,
Animation.
ANIMATION
_
LOOPING
);
break
;
case
Bob.
BOB
_
STATE
_
JUMP
:
keyFrame = Assets.
bobJump
.getKeyFrame(world.bob.stateTime,
Animation.
ANIMATION
_
LOOPING
);
break
;
case
Bob.
BOB
_
STATE
_
HIT
:
default
:
keyFrame = Assets.
bobHit
;
}