Code cleanup, return and exit conditions for Android

This commit is contained in:
saschawillems 2018-05-01 11:23:36 +02:00
parent 5056563f5d
commit 5fe9f91529
4 changed files with 29 additions and 13 deletions

View file

@ -15,8 +15,6 @@ import java.util.concurrent.Semaphore;
public class VulkanActivity extends NativeActivity {
VulkanActivity _activity;
static {
// Load native library
System.loadLibrary("native-lib");
@ -24,7 +22,6 @@ public class VulkanActivity extends NativeActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
_activity = this;
}
// Use a semaphore to create a modal dialog
@ -33,12 +30,14 @@ public class VulkanActivity extends NativeActivity {
public void showAlert(final String message)
{
ApplicationInfo applicationInfo = _activity.getApplicationInfo();
final VulkanActivity activity = this;
ApplicationInfo applicationInfo = activity.getApplicationInfo();
final String applicationName = applicationInfo.nonLocalizedLabel.toString();
this.runOnUiThread(new Runnable() {
public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(_activity, AlertDialog.THEME_HOLO_DARK);
AlertDialog.Builder builder = new AlertDialog.Builder(activity, AlertDialog.THEME_HOLO_DARK);
builder.setTitle(applicationName);
builder.setMessage(message);
builder.setPositiveButton("Close", new DialogInterface.OnClickListener() {

View file

@ -273,7 +273,9 @@ namespace vks
vks::android::showAlert(message.c_str());
#endif
std::cerr << message << "\n";
#if !defined(__ANDROID__)
exit(exitCode);
#endif
}
void exitFatal(std::string message, VkResult resultCode)

View file

@ -333,6 +333,7 @@ void VulkanExampleBase::renderLoop()
// Exit loop, example will be destroyed in application main
if (destroy)
{
ANativeActivity_finish(androidApp->activity);
break;
}
@ -556,7 +557,9 @@ void VulkanExampleBase::renderLoop()
}
#endif
// Flush device to make sure all resources can be freed
if (device != VK_NULL_HANDLE) {
vkDeviceWaitIdle(device);
}
}
void VulkanExampleBase::updateOverlay()
@ -830,7 +833,7 @@ VulkanExampleBase::~VulkanExampleBase()
#endif
}
void VulkanExampleBase::initVulkan()
bool VulkanExampleBase::initVulkan()
{
VkResult err;
@ -838,6 +841,7 @@ void VulkanExampleBase::initVulkan()
err = createInstance(settings.validation);
if (err) {
vks::tools::exitFatal("Could not create Vulkan instance : \n" + vks::tools::errorString(err), err);
return false;
}
#if defined(VK_USE_PLATFORM_ANDROID_KHR)
@ -864,6 +868,7 @@ void VulkanExampleBase::initVulkan()
err = vkEnumeratePhysicalDevices(instance, &gpuCount, physicalDevices.data());
if (err) {
vks::tools::exitFatal("Could not enumerate physical devices : \n" + vks::tools::errorString(err), err);
return false;
}
// GPU selection
@ -939,6 +944,7 @@ void VulkanExampleBase::initVulkan()
VkResult res = vulkanDevice->createLogicalDevice(enabledFeatures, enabledDeviceExtensions);
if (res != VK_SUCCESS) {
vks::tools::exitFatal("Could not create Vulkan device: \n" + vks::tools::errorString(res), res);
return false;
}
device = vulkanDevice->logicalDevice;
@ -988,6 +994,8 @@ void VulkanExampleBase::initVulkan()
};
LOGD("androidProduct = %s", androidProduct.c_str());
#endif
return true;
}
#if defined(_WIN32)
@ -1399,10 +1407,15 @@ void VulkanExampleBase::handleAppCommand(android_app * app, int32_t cmd)
LOGD("APP_CMD_INIT_WINDOW");
if (androidApp->window != NULL)
{
vulkanExample->initVulkan();
if (vulkanExample->initVulkan()) {
vulkanExample->prepare();
assert(vulkanExample->prepared);
}
else {
LOGE("Could not initialize Vulkan, exiting!");
androidApp->destroyRequested = 1;
}
}
else
{
LOGE("No window assigned!");
@ -1419,7 +1432,9 @@ void VulkanExampleBase::handleAppCommand(android_app * app, int32_t cmd)
case APP_CMD_TERM_WINDOW:
// Window is hidden or closed, clean up resources
LOGD("APP_CMD_TERM_WINDOW");
if (vulkanExample->prepared) {
vulkanExample->swapChain.cleanup();
}
break;
}
}

View file

@ -250,7 +250,7 @@ public:
virtual ~VulkanExampleBase();
// Setup the vulkan instance, enable required extensions and connect to the physical device (GPU)
void initVulkan();
bool initVulkan();
#if defined(_WIN32)
void setupConsole(std::string title);