Game Development Reference
In-Depth Information
Original game changes
: The original Doom engine needs to be modified
slightly to accommodate the JNI glue. This consists of inserting calls to
the C to Java callbacks in the correct files.
Removal of invalid dependencies
: Invalid dependencies in the original
code must be removed. For example, the original Simple DirectMedia
Layer (SDL) dependency used by the PC code must be deleted.
Let's look at these tasks in more detail.
Native Method Implementations
Table
5-4
shows the Java native signatures and their C counterparts in
jni_doom.c
.
Table 5-4.
Java Native Methods and Their Native Counterparts
Java Method
C Method
static native int
DoomMain(String[] argv)
JNIEXPORT jint JNICALL Java_doom_jni_Natives_
DoomMain(JNIEnv * env, jclass class, jobjectArray jargv)
static native int
keyEvent(int type, int key)
JNIEXPORT jint JNICALL Java_doom_jni_Natives_
keyEvent(JNIEnv * env, jclass cls, jint type, jint key)
static native int
motionEvent(int btn, int x, int y)
JNIEXPORT jint JNICALL Java_doom_jni_Natives_
motionEvent(JNIEnv * env, jclass cls, jint btn, jint
x, jint y)
Before you can proceed with the implementation, the
javah
command must be used to
generate the required header files and signatures, like so:
javah -jni -classpath PATH_TO_PROJECT_FOLDER/bin -d include doom.jni.Natives
Note that a class path to the
bin
folder is required for
javah
to find the
doom.jni.Natives
class. The output file
doom_jni_Natives.h
will be dumped in the
include
folder by using
-d
.
The header file is then used by
jni_doom.c
, as shown in this fragment:
#include <stdio.h>
#include "include/doom_jni_Natives.h"
#include "include/jni_doom.h"
#include "doomdef.h"
#include "d_event.h"
The code uses Doom code, thus the inclusion of
doomdef.h
and
d_event.h
. The header
jni_doom.h
defines prototypes for the C to Java callbacks and miscellaneous constants.
You also need a static reference to the JVM used by the C to Java callbacks, as in the
following fragment:
// Global Java VM reference
static JavaVM *g_VM;