Game Development Reference
In-Depth Information
, "GLSwapBuffers"
, "()V");
if (mid) {
(*env)->CallStaticVoidMethod(env, jNativesCls
, mid
);
}
}
/**
* Printf into the java layer
* does a varargs printf into a temp buffer
* and calls jni_sebd_str
*/
void jni_printf(char *format, ...)
{
va_list argptr;
static char string[1024];
va_start (argptr, format);
vsprintf (string, format,argptr);
va_end (argptr);
jni_send_str (string);
}
Let's take a closer look at the anatomy of a JNI Java callback. To start using JNI, a C program must
include the system header:
#include <jni.h>
Now, if your function is called from a different place than the one that started
Java_opengl_jni_Natives_NativeRender , you must attach to the current thread with the following:
(*g_VM)->AttachCurrentThread (g_VM, (void **) &env, NULL);
This is required if, for example, your program implements its own game loop, and then sends
messages back to Java through JNI. This isn't the case in our example, but I've included it so the function
can be invoked either way. g_VM is a global reference to the JVM, which must be saved within the very
first call to Java_opengl_jni_Natives_NativeRender . Next, to load a Java class opengl.jni.Natives within
C, you use the following:
jclass jNativesCls = (*env)->FindClass(env, "opengl/jni/Natives");
Here, env is a reference to the JNI environment obtained from then previous call. Note that the class
name must be separated using / , not . .
Now, with a reference to the natives class, you can call the static void method OnMessage :
Search WWH ::




Custom Search