Game Development Reference
In-Depth Information
Removing Unnecessary State Changes
Let's look at the present() method of BobTest and see what we can change. Here's the snippet,
in which we add the FPSCounter and also use glRotatef() and glScalef() ):
@Override
public void present( float deltaTime) {
GL10 gl = glGraphics.getGL();
gl.glViewport(0, 0, glGraphics.getWidth(), glGraphics.getHeight());
gl.glClearColor(1,0,0,1);
gl.glClear(GL10. GL_COLOR_BUFFER_BIT );
gl.glMatrixMode(GL10. GL_PROJECTION );
gl.glLoadIdentity();
gl.glOrthof(0, 320, 0, 480, 1, -1);
gl.glEnable(GL10. GL_TEXTURE_2D );
bobTexture.bind();
gl.glMatrixMode(GL10. GL_MODELVIEW );
for ( int i = 0; i < NUM_BOBS ; i++) {
gl.glLoadIdentity();
gl.glTranslatef(bobs[i].x, bobs[i].y, 0);
gl.glRotatef(45, 0, 0, 1);
gl.glScalef(2, 0.5f, 1);
bobModel.draw(GL10. GL_TRIANGLES , 0, 6);
}
fpsCounter.logFrame();
}
The first thing we could do is move the calls to glViewport() and glClearColor() , as well as
the method calls that set the projection matrix to the BobScreen.resume() method. The clear
color will never change; the viewport and the projection matrix won't change either. Why not
put the code to set up all persistent OpenGL states like the viewport or projection matrix
in the constructor of BobScreen ? Well, we need to battle context loss. All OpenGL ES state
modifications we perform will get lost, and when our screen's resume() method is called, we
know that the context has been re-created and thus is missing all the states that we might have
set before. We can also put glEnable() and the texture-binding call into the resume() method.
After all, we want texturing to be enabled all the time, and we also only want to use that single
texture containing Bob's image. For good measure, we also call texture.reload() in the
resume() method, so that our texture image data is also reloaded in the case of a context loss.
Here are our modified present() and resume() methods:
@Override
public void resume() {
GL10 gl = glGraphics.getGL();
gl.glViewport(0, 0, glGraphics.getWidth(), glGraphics.getHeight());
gl.glClearColor(1, 0, 0, 1);
gl.glMatrixMode(GL10. GL_PROJECTION );
gl.glLoadIdentity();
gl.glOrthof(0, 320, 0, 480, 1, -1);
 
Search WWH ::




Custom Search