Game Development Reference
In-Depth Information
Tip As you may know, using JNI, you can invoke C functions from Java. You may not know that you can also
load classes and invoke Java methods within C.
Listing 5-8. Native Interface for the GL Cubes Sample
package opengl.jni;
public class Natives {
private static EventListener listener;
public static interface EventListener {
void OnMessage(String text);
void GLSwapBuffers();
}
public static void setListener(EventListener l) {
listener = l;
}
/**
* Native Render test
*
* @return
*/
public static native int NativeRender();
@SuppressWarnings("unused")
private static void OnMessage(String text) {
if (listener != null)
listener.OnMessage(text);
}
@SuppressWarnings("unused")
private static void GLSwapBuffers() {
if (listener != null)
listener.GLSwapBuffers();
}
}
This class needs a way to notify components (the activity, for example) that some message has been
received from the native layer. You do this by creating the interface EventListener . In this way, a class
that wants to receive messages must implement EventListener and issue a call to
Natives.setListener(this) .
Before we jump to the C code, let's take a look at the Java changes required to the classes
CubeRenderer , GLSurfaceView , and GLThread for the sample.
Search WWH ::




Custom Search