Game Development Reference
In-Depth Information
Main loop : The main loop can be a combination of a Java thread that calls a native
game loop. Here is where things get interesting.
Note OpenGL operations can be performed natively after the GLContext is initialized by an Android activity if,
and only if, the native code is loaded by the activity as a shared library through JNI.
Swap buffers : This step can be performed in Java, provided that the native library
issues a callback after all GL operations have been completed. This is simply using
JNI callbacks and will result in a rendered surface on the screen.
This is great news. You don't need to rewrite large portions of an OpenGL game. You simply need to
initialize the GLContext within your Java activity, load the shared library, do all the rendering operations
natively, and issue a swap buffers callback to Java on each iteration of the game loop.
Let's apply this concept by rewriting portions of the GL cubes Java sample in C. The portion that will
be rewritten is the rendering of the cubes. The rest—initialization, main loop, and swap buffers—will
remain in Java. To accomplish this, you must make some simple changes to the sample classes and add
a new native activity.
Main Activity
You must create a new activity (with its own launcher) to load the native code (see Listing 5-7). This
activity is almost identical to its Java counterpart, except for the following:
A native library is loaded using System.load("/data/libgltest_jni.so") .
The Renderer constructor has been modified to accept a second Boolean
argument (use native rendering): mGLSurfaceView.setRenderer(new
CubeRenderer(true, true)) . This tells the cube renderer to use a translucent
background and native rendering.
Listing 5-7. Native Cubes Activity
package opengl.test;
import opengl.scenes.GLSurfaceView;
import opengl.scenes.cubes.CubeRenderer;
import android.app.Activity;
import android.os.Bundle;
public class NativeGLActivity extends Activity {
private GLSurfaceView mGLSurfaceView;
{
final String LIB_PATH = "/data/libgltest_jni.so";
System.out
Search WWH ::




Custom Search