Game Development Reference
In-Depth Information
Table 7-5.
C to Java Callbacks in jni_doom.c (continued)
C Method
Invoked Java Method
void jni_start_sound (const char * name,
int vol)
static void OnStartSound(byte[] name, int vol)
void jni_start_music (const char * name,
int loop)
static void OnStartMusic(String name, int loop)
void jni_stop_music (const char * name)
static void OnStopMusic(String name)
void jni_set_music_volume (int vol)
static void OnSetMusicVolume(int volume)
void jni_fatal_error(const char * text)
static void OnFatalError(String message)
The callbacks must be inserted in the C code as explained in the next sections.
Graphics Initialization
Constants for the graphics initialization callback are defined in
jni_doom.h
, as shown in Listing 7-15.
CB_CLASS_IG_CB
indicates the Java method name
OnInitGraphics
.
CB_CLASS_IG_SIG "(II)V"
defines the
signature: two integer parameters and a
void
return type, as shown in the next fragment:
#define CB_CLASS_IG_CB "OnInitGraphics"
#define CB_CLASS_IG_SIG "(II)V"
This callback also performs some critical steps:
It attaches to the current thread with
(*g_VM)->AttachCurrentThread (g_VM,
&env, NULL)
. Here is where the global JVM reference comes into play.
Furthermore, the JNI environment (
env
) will be used to invoke the callback.
•
It allocates space for the Java pixel array (video buffer) used by
jni_send_pixels
using the width and height of the display:
jImage = (*env)-> NewIntArray(env,
width * height)
.
•
It invokes the static void method
doom.util.Natives.OnInitGraphics(width,
height)
using its method ID:
(*env)->CallStaticVoidMethod(env, jNativesCls,
METHOD_ID, ARGUMENTS)
, where
ARGUMENTS
are the width and height of the display.
Note that the arguments must match the arguments in the Java method!
•
Listing 7-15.
Graphics Initialization
/**
* Fires when Doom graphics are initialized.
* params: img width, height
*/
void jni_init_graphics(int width, int height)