Game Development Reference
In-Depth Information
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 5-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:
jbyteArray ARRAY = (*env)->
NewByteArray(env, SIZE) where the words byte / Byte can be replaced
with boolean / Boolean , int / Int , object / Object , and other primitive types,
depending on your needs.
To create a Java byte array, you can use
(*env)->SetByteArrayRegion(env,
ARRAY, START, SIZE, (jbyte *) C_ARRAY) where Byte can be replaced with
any Java primitive type.
To insert data into the array, use
(*env)->CallStaticVoidMethod(env,
CLASS, METHOD_ID, ARG1, ARG2, ...) .
To release resources for an array, use
To call a static void method, use
(*env)->DeleteLocalRef(env,
ARRAY) .
Listing 5-17. Cascading Sound and Music Requests Back to Java
/**
* Fires multiple times when a sound is played
* @param name Sound name
* @param volume
*/
void jni_start_sound (const char * name, int vol)
{
/*
* 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 ) {
jni_printf("BUG: Invalid Doom JNI method OnStartSound %s"
, CB_CLASS_SS_SIG);
return ;
}
Search WWH ::




Custom Search