Game Development Reference
In-Depth Information
Finally, the last class to be updated is
GLThread
(see Listing 5-10), which contains the main loop.
Listing 5-10.
Changes for GLThread.java
public class GLThread extends Thread implements EventListener
{
// ...
@Override
public void GLSwapBuffers() {
if ( mEglHelper != null ) {
mEglHelper.swap();
}
}
@Override
public void OnMessage(String text) {
System.out.println("GLThread::OnMessage " + text);
}
}
GLThread
implements
EventListener
. This allows the C code to send text messages if something is
wrong. The method
GLSwapBuffers()
will be invoked when the C code requests a buffer swap.
This takes care of the Java portion of the sample. Now let's look at the C files:
cuberenderer.c
and
cube.c
.
Native Cube Renderer
The native cube renderer (
cuberenderer.c
) is similar to the Java class
CubeRenderer
. This file performs
the following tasks (see Listings 5-11 through 5-14):
•
It initializes the scene. This function is almost identical to
CubeRenderer.surfaceCreated()
.
It draws a frame using the
drawFrame()
function. This function is similar in nature
to
CubeRenderer.drawFrame()
.
•
•
It contains the native implementation of the native interface class
opengl.jni.Natives.NativeRender
(mapped in C as
Java_opengl_jni_Natives_NativeRender
). This function will be invoked every time
a frame is rendered from the
GLThread
Java class.
•
It contains the Java callbacks (functions that will invoke Java methods):
jni_printf(char *format, ...)
sends a text message back.
•
•
jni_gl_swap_buffers ()
requests a buffer swap within Java.