Game Development Reference
In-Depth Information
will be used to display the video on the device. Note that this callback fires outside of the current JNI
thread. When the callback fires, you cannot touch the JNI environment directly ( JNIEnv *env ), or you will
cause an invalid thread access error that will crash the application. Instead, you must attach to the
current thread with the following call:
(*g_VM)->AttachCurrentThread (g_VM, (void **) &env, NULL)
where env will get a reference to the current thread JNI environment. Furthermore to do this, you must
save a global reference to the Java virtual machine (usually from the very first native call):
(*env)->GetJavaVM(env, &g_VM);
Once you have attached to the current thread, you can load the
wolf.jni.Natives.OnInitGraphics(width, height) using the method name and signature and execute it
with the width and height of the video buffer:
jmethodID mid = (*env)->GetStaticMethodID(
env, jNativesCls, "OnInitGraphics", "(II)V");
(*env)->CallStaticVoidMethod(env, jNativesCls, mid, width, height);
The second part of the listing (a section of the file vi_null.c ) shows where jni_init_graphics is
called from VL_Startup() . This is the native video startup callback of the game (see Listing 6-16).
Listing 6-16. Graphics Initialization from wolf_jni.c and vi_null.c.
// This function is new code for Android and lives in wolf_ini.c
void jni_init_graphics(int width, int height)
{
JNIEnv *env;
if ( !g_VM) {
ERROR0("I_JNI: jni_init_graphics No JNI VM available.\n");
return;
}
(*g_VM)->AttachCurrentThread (g_VM, (void **) &env, NULL);
iSize = width * height;
// Create a new int[] used by jni_send_pixels
jImage = (*env)-> NewIntArray(env, iSize);
// call doom.util.Natives.OnInitGraphics(w, h);
jmethodID mid = (*env)->GetStaticMethodID(env, jNativesCls
, "OnInitGraphics"
, "(II)V");
if (mid) {
(*env)->CallStaticVoidMethod(env, jNativesCls
Search WWH ::




Custom Search