Game Development Reference
In-Depth Information
Table 5-5. C to Java Callbacks in jni_doom.c
C Method
Invoked Java Method
void jni_init_graphics(int width, int height)
static void OnInitGraphics(int w, int h)
void jni_send_pixels(int * data)
static void OnImageUpdate(int[] pixels)
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 following sections.
Graphics Initialization
Constants for the graphics initialization callback are defined in jni_doom.h , as shown in
Listing 5-15. CB_CLASS_IG_CB indicates the Java method name OnInitGraphics. CB_CLASS_
IG_SIG "(II)V" defines the signature of two integer parameters and a void return type, as
shown in the following 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) . This is where the global JVM reference comes
into play. Furthermore, the JNI environment (env) will be used to
invoke the callback.
1.
2.
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!
3.
 
 
Search WWH ::




Custom Search