Game Development Reference
In-Depth Information
if ( !g_VM) {
return;
}
(*g_VM)->AttachCurrentThread (g_VM, (void **) &env, NULL);
// Send img back to Java.
if (jSendImageMethod) {
(*env)->SetIntArrayRegion(env, jImage, 0, iSize, (jint *) data);
// Call Java method
(*env)->CallStaticVoidMethod(env, jNativesCls
, jSendImageMethod
, jImage);
}
}
Sound and Music Callbacks
The sound and music callbacks fire from the engine when a sound or background music must be played.
In a perfect world, sound would be handled in the native layer; however, due to the lack of
documentation and support for open audio standards in Android, requests are cascaded back to Java for
processing.
There are four sound and music callbacks in Doom, with their names and signatures defined in the
header jni_doom.h :
// doom.jni.Natives.OnStartSound(byte[] name, int volume)
#define CB_CLASS_SS_CB "OnStartSound"
#define CB_CLASS_SS_SIG "([BI)V"
// doom.jni.Natives.OnStartMusic (String name , int loop)
#define CB_CLASS_SM_CB "OnStartMusic"
#define CB_CLASS_SM_SIG "(Ljava/lang/String;I)V"
// doom.jni.Natives.OnStopMusic (String name )
#define CB_CLASS_STOPM_CB "OnStopMusic"
#define CB_CLASS_STOPM_SIG "(Ljava/lang/String;)V"
// doom.jni.Natives.OnSetMusicVolume (int volume)
#define CB_CLASS_SETMV_CB "OnSetMusicVolume"
#define CB_CLASS_SETMV_SIG "(I)V"
Note the method signature for OnStartSound with ([BI)V , where [B represents an array of bytes (the
name of the sound), I represents an integer (volume), and V is the return type of the method ( void ).
Another interesting signature is OnStartMusic with (Ljava/lang/String;I)V , where Ljava/lang/String;
means the class java.lang.String (enclosed in L; ).
Listing 7-17 shows the implementation of these callbacks. They are pretty similar in nature, in that
they all must attach to the current thread using the global JVM ( g_VM ). The following are some of the key
aspects of the code:
Search WWH ::




Custom Search