_init_display(engine);
? ? ? ? ? ? }
? ? ? ? ? ? break;
? ? ? ? case APP_CMD_TERM_WINDOW:
? ? ? ? ? ? // The window is being hidden or closed, clean it up.
? ? ? ? ? ? engine_term_display(engine);
? ? ? ? ? ? break;
? ? ? ? case APP_CMD_GAINED_FOCUS:
? ? ? ? ? ? break;
? ? ? ? case APP_CMD_LOST_FOCUS:
? ? ? ? ? ? break;
? ? }
}
/**
?* This is the main entry point of a native application that is using
?* android_native_app_glue.? It runs in its own thread, with its own
?* event loop for receiving input events and doing other things.
?*/
void android_main(struct android_app* state) {
? ? struct engine engine;
? ? // Make sure glue isn't stripped.
? ? app_dummy();
? ? memset(&engine, 0, sizeof(engine));
? ? state->userData = &engine;
? ? state->onAppCmd = engine_handle_cmd;
? ? state->onInputEvent = engine_handle_input;
? ? engine.app = state;
? ? // Prepare to monitor accelerometer
? ? engine.sensorManager = ASensorManager_getInstance();
? ? engine.accelerometerSensor = ASensorManager_getDefaultSensor(engine.sensorManager,
? ? ? ? ? ? ASENSOR_TYPE_ACCELEROMETER);
? ? if (state->savedState != NULL) {
? ? ? ? // We are starting with a previous saved state; restore from it.
? ? ? ? engine.state = *(struct saved_state*)state->savedState;
? ? }
? ? int ident, events;
? ? struct android_poll_source* source;
? ? while (true)
? ? {
? ? ? ? while ((ident = ALooper_pollAll(0, NULL, &events, (void**)&source)) >= 0)
? ? ? ? {
? ? ? ? ? ? if (source != NULL)
? ? ? ? ? ? ? ? source->process(state, source);
? ? ? ? ? ? if (state->destroyRequested != 0)
? ? ? ? ? ? ? ? return;
? ? ? ? }
? ? ? ? engine_draw_frame(&engine);
? ? }
}
效果如图所示。
