Game Development Reference
In-Depth Information
•
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 a these tasks in more detail.
Native Method Implementations
Table 7-4 shows the Java native signatures and their C counterparts in
jni_doom.c
.
Table 7-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:
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 will use 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 next fragment:
// Global Java VM reference
static JavaVM *g_VM;