C++
File main.cpp
void android_main(struct android_app* pAndroidApp)
{
...
pAndroidApp->onAppCmd = engine_handle_cmd;
pAndroidApp->onInputEvent = engine_handle_input;
...
}
// Process touch events
static int32_t engine_handle_input(struct android_app* app, AInputEvent* event)
{
// touch event
if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION)
{
// get x and y coordinates from screen
int x = AMotionEvent_getX(event, 0);
int y = AMotionEvent_getY(event, 0);
// is touch pressed
if ( (AMOTION_EVENT_ACTION_MASK & AMotionEvent_getAction(pEvent)) == AMOTION_EVENT_ACTION_DOWN)
{
...
}
// is touch unpressed
else if ( (AMOTION_EVENT_ACTION_MASK & AMotionEvent_getAction(pEvent)) == AMOTION_EVENT_ACTION_UP)
{
...
}
return 1; // return 1 means that we handled event
}
return 0;
}
// Process window events
static void engine_handle_cmd(struct android_app* app, int32_t cmd)
{
switch (cmd) {
// The window is being shown, get it ready.
case APP_CMD_INIT_WINDOW:
...
break;
// The window is being hidden or closed
case APP_CMD_TERM_WINDOW:
...
break;
// When our app get focus
case APP_CMD_GAINED_FOCUS:
...
break;
// When our app lost focus
case APP_CMD_LOST_FOCUS:
...
break;
default:
break;
}
}