Game Development Reference
In-Depth Information
For improved performance, static references to the Java native interface class
(
doom.jni.Natives
) are kept in
jNativesCls
. References to the Java methods to send
the video image (
jSendImageMethod
) and sound file (
jStartSoundMethod
) are also kept.
The reason is that these methods are invoked multiple times, and looking up these
names every time can slow things.
static jclass jNativesCls;
static jmethodID jSendImageMethod;
static jmethodID jStartSoundMethod;
Also, because you may send a pixel buffer (image) multiple times per second, you should
keep a reference to the Java array and its size, as in the following fragment:
static jintArray jImage;
static int iSize;
extern int doom_main(int argc, char **argv);
The line
extern int doom_main
defines the main engine function and tells the compiler it is
defined somewhere else in the library. The header
jni_doom.h
included up front defines the
constants and method signatures required to invoke the C to Java callbacks. For example,
the following fragment of the header defines constants for the Java native interface class
(
doom/jni/Natives
) and the method names and signatures for the callbacks
OnImageUpdate
and
OnStartSound
(see the “C to Java Callbacks” section for more details):
#define CB_CLASS "doom/jni/Natives"
#define CB_CLASS_IU_CB "OnImageUpdate"
#define CB_CLASS_IU_SIG "([I)V"
#define CB_CLASS_SS_CB "OnStartSound"
#define CB_CLASS_SS_SIG "([BI)V"
Let's take a look at the actual implementations. They are divided into the following three
groups:
Native game loop
: This invokes the engine loop
doom_main
.
Key and motion events
: These post key and motion events to
the engine.
C to Java callbacks
: These callbacks are critical for the Java code to
receive information from the Doom engine.
Native Game Loop
The native game loop's job is to extract the arguments sent as a
jobjectArray
into a C
char
**
array and invoke the main Doom engine function (
doom_main
). This function performs the
following additional steps:
Obtains a reference to the JVM using
(*env)->GetJavaVM(env,
&g_VM)
. This reference will be used by the C to Java callbacks.
1.