Game Development Reference
In-Depth Information
gl.glEnable(GL10.
GL_BLEND
);
gl.glBlendFunc(GL10.
GL_SRC_ALPHA
, GL10.
GL_ONE_MINUS_SRC_ALPHA
);
guiCamera.setViewportAndMatrices();
batcher.beginBatch(buttonTexture);
batcher.drawSprite(32, 32, 64, 64, buttonRegion);
batcher.endBatch();
gl.glDisable(GL10.
GL_BLEND
);
gl.glDisable(GL10.
GL_TEXTURE_2D
);
}
The
present()
method is surprisingly simple, thanks to the work we put into all of those
little helper classes. We start off with the usual things like clearing the screen and setting the
viewport. Next, we tell the
EulerCamera
instance to set the projection matrix and model-view
matrix. From this point on, we can render anything that should be 3D onscreen. Before we do
that, we enable depth testing, texturing, and lighting. Next, we bind the crate texture and the
cube vertices and also enable the point light. Note that we bind the texture and cube vertices
only once, since we are going to reuse them for all of the crates that we render. This is the
same trick that we used in our
BobTest
in Chapter 8 when we wanted to speed up rendering by
reducing state changes.
The next piece of code just draws the 25 cubes in a grid formation via a simple nested
for
loop. Since we have to multiply the model-view matrix with a translation matrix to put the cube
vertices at a specific position, we must also use
glPushMatrix()
and
glPopMatrix()
so that we
don't destroy the camera matrix that's also stored in the model-view matrix.
Once we are done with rendering our cubes, we unbind the cube vertices and disable lighting
and depth testing. This is crucial, since we are now going to render the 2D UI overlay with the
button. Since the button is actually circular, we also enable blending to make the edges of the
texture transparent.
Rendering the button is done in the same way that we rendered the UI elements in Super
Jumper. We tell the
Camera2D
instance to set the viewport and matrices (we wouldn't really need
to set the viewport here again; feel free to “optimize� this method) and tell the
SpriteBatcher
that we are going to render a sprite. We render the complete button texture at (32,32) in our
480×320 coordinate system that we set up via the
guiCamera
instance.
Finally, we just disable the last few states we enabled previously, blending and texturing.
@Override
public void
pause() {
}
@Override
public void
dispose() {
}
}
The rest of the class is again just some stub methods for
pause()
and
dispose()
. Figure
11-11
shows the output of this little program.