diff --git a/base/vulkanexamplebase.cpp b/base/vulkanexamplebase.cpp index 035e1a66..85cfe7d0 100644 --- a/base/vulkanexamplebase.cpp +++ b/base/vulkanexamplebase.cpp @@ -355,29 +355,36 @@ void VulkanExampleBase::renderLoop() } } #elif defined(__ANDROID__) - // todo : Application moved to background while (1) { - // Read all pending events. int ident; int events; struct android_poll_source* source; + bool destroy = false; - while ((ident = ALooper_pollAll(animating ? 0 : -1, NULL, &events, (void**)&source)) >= 0) + focused = true; + + while ((ident = ALooper_pollAll(focused ? 0 : -1, NULL, &events, (void**)&source)) >= 0) { if (source != NULL) { source->process(androidApp, source); } - if (androidApp->destroyRequested != 0) { - // todo : free resources - //delete(vulkanExample); - return; + LOGD("Android app destroy requested"); + destroy = true; + break; } } + // App destruction requested + // Exit loop, example will be destroyed in application main + if (destroy) + { + break; + } + // Render frame if (prepared) { @@ -976,6 +983,7 @@ void VulkanExampleBase::handleAppCommand(android_app * app, int32_t cmd) switch (cmd) { case APP_CMD_SAVE_STATE: + LOGD("APP_CMD_SAVE_STATE"); /* vulkanExample->app->savedState = malloc(sizeof(struct saved_state)); *((struct saved_state*)vulkanExample->app->savedState) = vulkanExample->state; @@ -986,12 +994,10 @@ void VulkanExampleBase::handleAppCommand(android_app * app, int32_t cmd) LOGD("APP_CMD_INIT_WINDOW"); if (vulkanExample->androidApp->window != NULL) { - LOGI("Initializing Vulkan..."); vulkanExample->initVulkan(false); vulkanExample->initSwapchain(); vulkanExample->prepare(); assert(vulkanExample->prepared); - LOGI("Vulkan initialized"); } else { @@ -999,7 +1005,12 @@ void VulkanExampleBase::handleAppCommand(android_app * app, int32_t cmd) } break; case APP_CMD_LOST_FOCUS: - //vulkanExample->animating = 0; + LOGD("APP_CMD_LOST_FOCUS"); + vulkanExample->focused = false; + break; + case APP_CMD_GAINED_FOCUS: + LOGD("APP_CMD_GAINED_FOCUS"); + vulkanExample->focused = true; break; } } diff --git a/base/vulkanexamplebase.h b/base/vulkanexamplebase.h index 940bfb8d..f071778b 100644 --- a/base/vulkanexamplebase.h +++ b/base/vulkanexamplebase.h @@ -155,7 +155,8 @@ public: HINSTANCE windowInstance; #elif defined(__ANDROID__) android_app* androidApp; - bool animating = true; + // true if application has focused, false if moved to background + bool focused = false; // Gamepad state (only one) struct {