Game Development Reference
In-Depth Information
Handling asynchronous multi-touch input
In the previous chapter we learned how to handle multi-touch events on Android.
However, our simple example has one serious issue. Android touch events are sent
asynchronously and can interfere with the game logic. As such, we need to create a queue
to process events in a controllable way.
Getting ready
Check out the Processing multi-touch events on Android recipe from Chapter 7 ,
Cross-platform UI and Input System, before proceeding.
How to do it…
1.
In the previous chapter we invoked the touch handler directly from an asynchronous
JNI callback:
Java_com_packtpub_ndkcookbook_game1_Game1Activity_SendMotion(
JNIEnv * env, jobject obj, int PointerID, int x, int y,
bool Pressed, int Flag)
{
LVector2 Pos = LVector2( (float)x / (float)g_Width,
(float)y / (float)g_Height );
GestureHandler_SendMotion( PointerID, (eMotionFlag)Flag,
Pos,Pressed );
}
2.
This time, we have to store all the events in a queue rather then processing
them immediately. The queue will hold the parameters to GestureHandler_
SendMotion() in a struct:
struct sSendMotionData
{
int ContactID;
eMotionFlag Flag;
LVector2 Pos;
bool Pressed;
};
3.
The queue implementation relies on std::vector , holding touch events and
Mutex , providing queue access synchronization:
Mutex g_MotionEventsQueueMutex;
std::vector<sSendMotionData> g_MotionEventsQueue;
 
Search WWH ::




Custom Search