Android application state handling (#97)

This commit is contained in:
saschawillems 2016-03-26 12:58:35 +01:00
parent 1288672fd9
commit 2ed3e946b1
2 changed files with 23 additions and 11 deletions

View file

@ -355,29 +355,36 @@ void VulkanExampleBase::renderLoop()
} }
} }
#elif defined(__ANDROID__) #elif defined(__ANDROID__)
// todo : Application moved to background
while (1) while (1)
{ {
// Read all pending events.
int ident; int ident;
int events; int events;
struct android_poll_source* source; 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) if (source != NULL)
{ {
source->process(androidApp, source); source->process(androidApp, source);
} }
if (androidApp->destroyRequested != 0) if (androidApp->destroyRequested != 0)
{ {
// todo : free resources LOGD("Android app destroy requested");
//delete(vulkanExample); destroy = true;
return; break;
} }
} }
// App destruction requested
// Exit loop, example will be destroyed in application main
if (destroy)
{
break;
}
// Render frame // Render frame
if (prepared) if (prepared)
{ {
@ -976,6 +983,7 @@ void VulkanExampleBase::handleAppCommand(android_app * app, int32_t cmd)
switch (cmd) switch (cmd)
{ {
case APP_CMD_SAVE_STATE: case APP_CMD_SAVE_STATE:
LOGD("APP_CMD_SAVE_STATE");
/* /*
vulkanExample->app->savedState = malloc(sizeof(struct saved_state)); vulkanExample->app->savedState = malloc(sizeof(struct saved_state));
*((struct saved_state*)vulkanExample->app->savedState) = vulkanExample->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"); LOGD("APP_CMD_INIT_WINDOW");
if (vulkanExample->androidApp->window != NULL) if (vulkanExample->androidApp->window != NULL)
{ {
LOGI("Initializing Vulkan...");
vulkanExample->initVulkan(false); vulkanExample->initVulkan(false);
vulkanExample->initSwapchain(); vulkanExample->initSwapchain();
vulkanExample->prepare(); vulkanExample->prepare();
assert(vulkanExample->prepared); assert(vulkanExample->prepared);
LOGI("Vulkan initialized");
} }
else else
{ {
@ -999,7 +1005,12 @@ void VulkanExampleBase::handleAppCommand(android_app * app, int32_t cmd)
} }
break; break;
case APP_CMD_LOST_FOCUS: 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; break;
} }
} }

View file

@ -155,7 +155,8 @@ public:
HINSTANCE windowInstance; HINSTANCE windowInstance;
#elif defined(__ANDROID__) #elif defined(__ANDROID__)
android_app* androidApp; android_app* androidApp;
bool animating = true; // true if application has focused, false if moved to background
bool focused = false;
// Gamepad state (only one) // Gamepad state (only one)
struct struct
{ {