Game Development Reference
In-Depth Information
// In C
// Push key to the event queue in Quake
extern void Key_Event (int key, qboolean down);
/*
* C Implementation of Java native int keyPress(int key);
*/
JNIEXPORT jint JNICALL Java _keyPress
(JNIEnv * env, jclass cls, jint key)
{
Key_Event((int)key, 1);
return key;
}
/*
* C Implementation of Java native int keyRelease(int key);
*/
JNIEXPORT jint JNICALL Java _keyRelease
(JNIEnv * env, jclass cls, jint key)
{
Key_Event((int)key, 0);
return key;
}
Cascading Touch Events
Touch events work in a similar way as key events. When the user touches the device screen,
the Java activity overrides onTouchEvent , which receives a MotionEvent . The event contains
the coordinates of the pointer where the top-left corner of the device represents the
origin (0,0). The type of event, ACTION_DOWN , ACTION_UP or ACTION_MOVE , can be obtained by
calling event.getAction() . Based on this value, you save the start XY coordinates.
Finally, when you drag a finger, the XY increments (dx, dy) are calculated and sent to the
native layer for consumption. When the finger is lifted, the start XY coordinates are reset, as
shown in Listing 2-7. The final effect is a sequence of delta XY increments, which the native
engine can use to either move a character in 3D space or look around the surroundings. This
is how Quake handles movement.
Listing 2-7. Cascading Touch Events Between Java and C
// Java: Natives to be implemented in C
public static native int mouseLook(int deltaX, int deltaY);
public static native int mouseMove(int deltaX, int deltaY);
// down coordinates
float startX = -1, startY = -1;
public boolean onTouchEvent(MotionEvent event)
{
 
Search WWH ::




Custom Search