Game Development Reference
In-Depth Information
jmethod mSendStr = (*env)->GetStaticMethodID(env, jNativesCls, "OnMessage"
, "(Ljava/lang/String;)V");
(*env)->CallStaticVoidMethod(env, jNativesCls, mSendStr
, (*env)->NewStringUTF(env, text) );
Note that to call this method, you need to obtain its JNI method ID using its name (
OnMessage
) and
its signature
(Ljava/lang/String;)V
. The signature describes the method's arguments (a string in this
case) and the return type (
void
). With this information, you call the static void method sending the
corresponding arguments.
■
Note
C strings must be converted into Java strings before invoking Java methods, using
(*env)->
NewStringUTF(env, MyCString)
.
Native Interface Call
The native interface function (see Listing 5-14) is the C implementation of the Java native method
opengl.jni.Natives.NativeRender()
. This function performs the following tasks:
•
It saves a reference to the Java VM, required by the Java callbacks of the previous
section.
•
It initializes the scene.
•
It renders one frame. This function is meant to be called multiple times within the
rendering thread (implemented by
GLThread.java
).
Listing 5-14.
Native Interface Function from cuberenderer.c
/*
* Class: opengl_jni_Natives
* Method: RenderTest
* Signature: ()V
*/
JNIEXPORT jint JNICALL Java_opengl_jni_Natives_NativeRender
(JNIEnv * env, jclass cls)
{
(*env)->GetJavaVM(env, &g_VM);
static int initialized = 0;
if ( ! initialized ) {
jni_printf("Native:RenderTest initscene");
init_scene();
initialized = 1;
}