Game Development Reference
In-Depth Information
To create a Java byte array, you can use
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 insert data into the array, use
(*env)->SetByteArrayRegion(env, ARRAY,
START, SIZE, (jbyte *) C_ARRAY)
, where
Byte
can be replaced with any Java
primitive type.
•
To call a static void method, use
(*env)->CallStaticVoidMethod(env, CLASS,
METHOD_ID, ARG1, ARG2,…)
.
•
To release resources for an array, use
(*env)->DeleteLocalRef(env, ARRAY)
.
•
Listing 7-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 ;
}
// Create a new char[] used by jni_send_pixels
// Used to prevent JNI ref table overflows
int iSize = strlen(name);
jbyteArray jSound = (*env)-> NewByteArray(env, iSize);
(*env)->SetByteArrayRegion(env, jSound, 0, iSize, (jbyte *) name);
// Call Java method
(*env)->CallStaticVoidMethod(env, jNativesCls
, jStartSoundMethod
, jSound //(*env)->NewStringUTF(env, name)