Game Development Reference
In-Depth Information
// Load callback cclass
LOGD("Loading callback class %s", CB_CLASS);
jclass clazz = (*env)->FindClass(env, CB_CLASS);
// Global ref for other threads
jCbClass = (jclass)(*env)->NewGlobalRef(env, clazz);
if ( jCbClass == 0 ) {
LOGE("Unable to find cb class: %s", CB_CLASS);
return;
}
}
After the initialization step is complete, you can proceed to the most important method:
DiscoverAndConnect . This is where all the work gets done (see Listing 8-9). The process starts by
initializing the Wiimote data structure (which holds information about all controllers) by invoking
wiimotes = wiic_init(MAX_WIIMOTES);
The WiiC library supports up to four simultaneous Wiimotes.
Next is the discovery process. To do so, you invoke the WiiC function wiic_find
(wiimotes, MAX_WIIMOTES, 10) as shown in the next fragment:
static int discover()
{
// Find, timeout 10 secs
nmotes = wiic_find(wiimotes, MAX_WIIMOTES, 10);
if ( !nmotes ) {
jni_send_error(102, "No wiimotes found.");
return 0;
}
jni_send_message("Found %d wiimote(s)", nmotes);
return 1;
}
Wiic_find takes as arguments the global Wiimote data structure from the previous step, the
max number of Wiimotes, and a timeout in seconds. It returns the number of Wiimotes found.
If you get some Wiimotes online, then DiscoverAndConnect (see Listing 8-9) turns on the first
LED for the first Wiimote and rumbles for one second. This tells the player he is ready to go.
Listing 8-9. WiiC JNI Connection
JNIEXPORT void JNICALL Java_game_engine_controller_Wiimote_DiscoverAndConnect
(JNIEnv * env, jclass cls)
{
int i;
if ( ! wiimotes ) {
LOGD("Creating wiimotes");
wiimotes = wiic_init(MAX_WIIMOTES);
}
 
Search WWH ::




Custom Search