Game Development Reference
In-Depth Information
WiiC JNI Interface
This is the glue that binds the two previous components together. As mentioned, the native
methods described in the Wiimote Java class require their C counterparts (see Table 8-5 ).
Table 8-5. WiiC JNI Interface Methods
Native Java
JNI Implementation
static void Initialize( )
Java_game_engine_controller_Wiimote_Initialize
static void DiscoverAndConnect( )
Java_game_engine_controller_Wiimote_DiscoverAndConnect
static void DisconnectAll( )
Java_game_engine_controller_Wiimote_DisconnectAll
static void ToggleRumble( )
Java_game_engine_controller_Wiimote_ToggleRumble
static void SetLed(int led)
Java_game_engine_controller_Wiimote_SetLed
Plus the interface contains a simple callback system used to send messages and core
button or Nunchuk events back to Java. This callback system uses the following C methods:
void jni_send_message(char *format, ...) : It sends a string message
back to tell Java what is going on behind the scenes.
void jni_send_event(int wiimoteNum, char *format, ...) : This is a
critical method that sends button or Nunchuk events back encoded as
a set of key/value pairs. Let's take a closer look, starting with the native
methods and then the callbacks.
Native Methods
In the initialization step (see Listing 8-8), you simply load the Java callback class used to
send messages back by invoking
(*env)->FindClass(env, "game/engine/controller/Wiimote")
This creates a local reference to the Java class game.engine.controller.Wiimote . With the
local reference you obtain a new global reference (which can be used within other threads).
This is very important because as callback functions get called from other threads, you
must use the global reference to the callback class. Local references may be removed from
memory at any time by the Java virtual machine (JVM), which may result in all sorts of weird
behavior, crashes, and headaches.
Listing 8-8. WiiC JNI Interface Initialization
JNIEXPORT void JNICALL Java_game_engine_controller_Wiimote_Initialize
(JNIEnv * env, jclass cls)
{
// save vm
(*env)->GetJavaVM(env, &g_VM);
 
 
Search WWH ::




Custom Search