Game Development Reference
In-Depth Information
invoked (see Listing 2-2). CB_CLASS_MSG_SIG is a critical constant that defines the Java signature of the
OnMessage Java method. Let's take a closer look at this signature:
(Ljava/lang/String;I)V
A Java method signature has the format (ARGUMENTS)RETURN_TYPE , where the arguments can be
encoded as follows:
I = Integer
B = Byte
S = Short
C = Char
LJava_Class; = For Java classes enclosed by : L and ;
In our case, the first argument is a Java string ( Ljava/lang/String; ), and the second is an integer ( I ).
Note that all arguments are defined by a single character (except for classes that are enclosed by L; ), and
there are no separators between them. Finally, V is the return type defined as void.
Caution Method signatures are a major pain when coding in JNI. Any mistake in this string, and the library will
not be able to find the method at runtime.
Next, the file defines the prototypes for the functions within the library:
int lib_main(int argc, char **argv) : This is the entry point to the library. It
receives the number of arguments ( argc ) and a list of arguments ( argv ), similar to
the standard C main() function.
int getArrayLen(JNIEnv * env, jobjectArray jarray) : This function is used to
get the length of a Java array, which will be translated into a C array for use by the
library.
void jni_printf(char *format, ...) : This function is used by the library to send
a text message back to Java. Note that ... indicates that the function will receive a
vector of arguments.
Finally, we need two global references:
static JavaVM *g_VM;
static jclass jNativesCls;
g_VM is a reference to the JVM, and it will be used make JNI system calls. jNativesCls is a reference to
the jni.Natives Java class used to invoke the Java method OnMessage . Note that the static keyword tells
the compiler that these variables should be visible only within code in lib.c .
Search WWH ::




Custom Search