Game Development Reference
In-Depth Information
Original Game Changes
In order for the JNI glue to work, changes are required to the original game engine. Some are simple,
such as inserting calls to the C to Java callbacks; some are not so simple, such as removing invalid
dependencies. Table 7-6 shows the original files and the changes required. Considering that the engine
has a total of 90,000 lines of code, these changes are not that bad.
Table 7-6.
Changes Required to the Original Engine to Insert the JNI Glue
File
Changes
i_main.c
Rename the main subroutine to
doom_main
.
i_system.c
In
I_Error
, insert
jni_fatal_error
.
i_sound.c
Comment SDL dependencies. In
I_StartSound
, insert start sound callback
jni_start_sound
.
s_sound.c
In
S_SetMusicVolume
, insert volume callback
jni_set_music_volume
.
i_video.c
Comment SDL dependencies. Insert code to build an Android ARBG pixel array from the
video buffer. In
I_SetRes
, add JNI callback to initialize graphics. In
I_FinishUpdate
, send
pixels to Java with
jni_send_pixels
.
These changes are explained in more detail in the next sections.
Renaming main
Let's start with the simplest change: renaming the
main()
subroutine in
i_main.c
so it can be invoked
from the Java native
Java_doom_jni_Natives_DoomMain
, which will start the game from Java, as shown in
the next fragment:
// In i_main.c
int main(int argc, char **argv)
int doom_main(int argc, char **argv)
// In jni_doom.c
extern int doom_main(int argc, char **argv);
JNIEXPORT jint JNICALL Java_doom_jni_Natives_DoomMain
(JNIEnv * env, jclass class, jobjectArray jargv)
{
...
doom_main (clen, args);
...
}