Game Development Reference
In-Depth Information
4.
All the work our new
SendMotion()
JNI callback has to do is pack the touch event
parameters into the queue:
Java_com_packtpub_ndkcookbook_game1_Game1Activity_SendMotion(
JNIEnv * env, jobject obj, int PointerID, int x, int y,
bool Pressed, int Flag)
{
sSendMotionData M;
M.ContactID = PointerID;
M.Flag = (eMotionFlag)Flag;
M.Pos = LVector2( (float)x / (float)g_Width,
(float)y / (float)g_Height );
M.Pressed = Pressed;
LMutex Lock( &g_MotionEventsQueueMutex );
g_MotionEventsQueue.push_back( M );
}
We can now process the touch events whenever we like.
How it works…
To handle the touch events in the queue, we extend the implementation of the
DrawFrame()
JNI callback:
Java_com_packtpub_ndkcookbook_game1_Game1Activity_DrawFrame(
JNIEnv* env, jobject obj )
{
Note the scope of the
Lock
variable inside the additional
{}
. We need it because the mutex
variable must be unlocked to prevent deadlocks, before proceeding with the game logic:
{
LMutex Lock(&g_MotionEventsQueueMutex );
for( auto m : g_MotionEventsQueue )
{
GestureHandler_SendMotion( m.ContactID, m.Flag,
m.Pos, m.Pressed );
}
g_MotionEventsQueue.clear();
}
GenerateTicks();
}
See the
jni/Wrappers.cpp
ile from the example
1_Game
for
the complete implementation, which can be retrieved from
www.
packtpub.com/support
.