游戏 Main Activity 继承自 GameActivity,并完成 native library 加载。
1 2 3 4 5 6 7 8 9
import com.google.androidgamesdk.GameActivity;
publicclassYourGameActivityextendsGameActivity { static { // Load the native library. // The name "game" depends on your CMake configuration. System.loadLibrary("game"); } }
while (1) { int events; structandroid_poll_source* source;
// If not animating, block until we get an event; if animating, don't block. while ((ALooper_pollAll(IsAnimating() ? 0 : -1, NULL, &events, (void **) &source)) >= 0) {
// process 间接调用 app->onAppCmd if (source != NULL) { source->process(mApp, source); }
staticvoid _handle_cmd(struct android_app* app, int32_t cmd) { switch (cmd) { case APP_CMD_SAVE_STATE: // The system has asked us to save our current state. app->savedState = malloc(sizeof(mState)); *(app->savedState) = mState; app->savedStateSize = sizeof(mState); break; case APP_CMD_STOP: mIsVisible = false; break; case APP_CMD_START: mIsVisible = true; break; case APP_CMD_PAUSE: case APP_CMD_RESUME: case APP_CMD_CONFIG_CHANGED: break; } }