Game Development Reference
In-Depth Information
| (pal[colIdx].blue);
}
// send thru JNI here
jni_send_pixels(pixels,0,0, vwidth, vheight);
}
Cascading Sound and Music Requests
Sound and music requests are cascaded by two C to Java callbacks and changes to the native file
sd_null.c :
jni_start_sound (int idx) : This callback calls the Java method
wolf.jni.Natives.OnStartSound(int idx) , where idx is the native sound ID.
jni_start_music (int idx) : This callback calls the Java method
wolf.jni.Natives.OnStartMusic(int idx) , where idx is the native music ID.
Listing 6-18 shows the implementation of these callbacks. Remember that you must attach to the
current thread to prevent a JNI invalid thread access with
(*g_VM)->AttachCurrentThread (g_VM, (void **) &env, NULL)
Once the thread is attached, you can call the Java method using its ID plus the argument (the sound
ID in this case):
(*env)->CallStaticVoidMethod(env, jNativesCls, jStartSoundMethod, (jint) idx);
Note that the method IDs for sound and music, jStartSoundMethod and jStartMusicMethod
respectively, were loaded at startup for increased performance.
Listing 6-18. Cascading Sound and Music Requests
// In jni_wolf.c
void jni_start_sound (int idx)
{
/*
* Attach to the curr thread otherwise we get JNI WARNING:
* threadid=3 using env from threadid=15 which aborts the VM
*/
JNIEnv *env;
if ( !g_VM) {
return;
}
(*g_VM)->AttachCurrentThread (g_VM, (void **) &env, NULL);
if ( jStartSoundMethod == 0 ) {
Search WWH ::




Custom Search