Game Development Reference
In-Depth Information
We keep a reference to a
GLSurfaceView
instance as a member of the class. In the
onCreate()
method, we make our application go full-screen, create the
GLSurfaceView
, set our
Renderer
implementation, and make the
GLSurfaceView
the content view of our activity.
@Override
public void
onResume() {
super
.onPause();
glView.onResume();
}
@Override
public void
onPause() {
super
.onPause();
glView.onPause();
}
In the
onResume()
and
onPause()
methods, we call the supermethods as well as the respective
GLSurfaceView
methods. These will start up and tear down the rendering thread of the
GLSurfaceView
, which in turn will trigger the callback methods of our
Renderer
implementation at
appropriate times.
static class
SimpleRenderer
implements
Renderer {
Random rand =
new
Random();
public void
onSurfaceCreated(GL10 gl, EGLConfig config) {
Log.
d
("GLSurfaceViewTest", "surface created");
}
public void
onSurfaceChanged(GL10 gl,
int
width,
int
height) {
Log.
d
("GLSurfaceViewTest", "surface changed: "+width+"x"
+ height);
}
public void
onDrawFrame(GL10 gl) {
gl.glClearColor(rand.nextFloat(), rand.nextFloat(),
rand.nextFloat(), 1);
gl.glClear(GL10.
GL_COLOR_BUFFER_BIT
);
}
}
}
The final piece of the code is our
Renderer
implementation. It just logs some information in
the
onSurfaceCreated()
and
onSurfaceChanged()
methods. The really interesting part is the
onDrawFrame()
method.
As stated earlier, the
GL10
instance gives us access to the OpenGL ES API. The 10 in
GL10
indicates that it offers us all the functions defined in the OpenGL ES 1.0 standard. For now, we
can be happy with that. All the methods of that class map to a corresponding C function, as
defined in the standard. Each method begins with the prefix
gl
, an old tradition of OpenGL ES.
The first OpenGL ES method we call is
glClearColor()
. You probably already know what that
will do. It sets the color to be used when we issue a command to clear the screen. Colors in