Game Development Reference
In-Depth Information
Coding the Native Layer
Even though Wolf3D is an old game written in the 1980s, it is still big for a mobile device. The C code is
roughly 30,000 lines of C code that's very hard to understand. This section will take a look at some tiny
changes made to the core to add JNI functionality. You should look at the source code of the chapter
when going through this section.
The changes made to the original game for JNI can be summarized as follows:
New files:
jni_wolf.c : This file contains all JNI native method implementations plus C
to Java callbacks
jni_wolf.h and wolf_jni_Natives.h . These are the C headers that support
jni_wolf.c .
Updated files:
sd_null.c : This file will be updated to cascade sound and music requests
back to Java.
vi_null.c : This file will be updated to cascade video buffer events back to
Java.
Let's take a look at sections of jni_wolf.c . This is the most important file and must be understood
completely. Table 6-1 shows the name mappings of the native Java methods to their corresponding C
counterparts:
Table 6-1. Native Methods and Their C Names for Wolf 3D.
Java
C
public static native int
wolf.jni.Natives.WolfMain(String[] argv)
JNIEXPORT jint JNICALL Java_wolf_jni_Natives_WolfMain
(JNIEnv * env, jclass cls, jobjectArray jargv)
public static native int
wolf.jni.Natives.keyPress(int key)
JNIEXPORT jint JNICALL Java_wolf_jni_Natives_keyPress
(JNIEnv * env, jclass cls, jint scanCode)
public static native int
wolf.jni.Natives.keyRelease(int key)
JNIEXPORT jint JNICALL Java_wolf_jni_Natives_
keyRelease (JNIEnv * env, jclass cls, jint scanCode)
Consider the native wolf.jni.Natives.WolfMain . Internally, it gets translated to Java_
wolf_jni_Natives_WolfMain . This must be done using the javah command (see the section
“Compiling the DSO” for details). This function starts the main game loop.
Search WWH ::




Custom Search