From 979b531e9fff8ef044cf036d8ebd7fab3c17bec3 Mon Sep 17 00:00:00 2001 From: Jeremy Kniager Date: Fri, 22 Nov 2019 14:55:57 -0700 Subject: vkcube: Fix Cube Squishing on Resize Modified viewport creation in vkcube to keep the aspect ratio square and centered. This change prevents cube from squishing and warping when the window is resized. It now shrinks and grows. Change-Id: Ie2b3c5747293dfed968af5d83b581ac74d81bb10 --- cube/cube.c | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) (limited to 'cube/cube.c') diff --git a/cube/cube.c b/cube/cube.c index 5382cd0e..9cbc45e5 100644 --- a/cube/cube.c +++ b/cube/cube.c @@ -99,10 +99,10 @@ void DbgMsg(char *fmt, ...) { #elif defined __ANDROID__ #include -#define ERR_EXIT(err_msg, err_class) \ - do { \ +#define ERR_EXIT(err_msg, err_class) \ + do { \ ((void)__android_log_print(ANDROID_LOG_INFO, "Vulkan Cube", err_msg)); \ - exit(1); \ + exit(1); \ } while (0) #ifdef VARARGS_WORKS_ON_ANDROID void DbgMsg(const char *fmt, ...) { @@ -112,8 +112,8 @@ void DbgMsg(const char *fmt, ...) { va_end(va); } #else // VARARGS_WORKS_ON_ANDROID -#define DbgMsg(fmt, ...) \ - do { \ +#define DbgMsg(fmt, ...) \ + do { \ ((void)__android_log_print(ANDROID_LOG_INFO, "Vulkan Cube", fmt, ##__VA_ARGS__)); \ } while (0) #endif // VARARGS_WORKS_ON_ANDROID @@ -529,22 +529,21 @@ VKAPI_ATTR VkBool32 VKAPI_CALL debug_messenger_callback(VkDebugUtilsMessageSever #ifdef _WIN32 in_callback = true; - if (!demo->suppress_popups) - MessageBox(NULL, message, "Alert", MB_OK); + if (!demo->suppress_popups) MessageBox(NULL, message, "Alert", MB_OK); in_callback = false; #elif defined(ANDROID) if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_INFO_BIT_EXT) { - __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message); + __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message); } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_WARNING_BIT_EXT) { - __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message); + __android_log_print(ANDROID_LOG_WARN, APP_SHORT_NAME, "%s", message); } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_ERROR_BIT_EXT) { __android_log_print(ANDROID_LOG_ERROR, APP_SHORT_NAME, "%s", message); } else if (messageSeverity & VK_DEBUG_UTILS_MESSAGE_SEVERITY_VERBOSE_BIT_EXT) { __android_log_print(ANDROID_LOG_VERBOSE, APP_SHORT_NAME, "%s", message); } else { - __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message); + __android_log_print(ANDROID_LOG_INFO, APP_SHORT_NAME, "%s", message); } #else @@ -772,8 +771,16 @@ static void demo_draw_build_cmd(struct demo *demo, VkCommandBuffer cmd_buf) { &demo->swapchain_image_resources[demo->current_buffer].descriptor_set, 0, NULL); VkViewport viewport; memset(&viewport, 0, sizeof(viewport)); - viewport.height = (float)demo->height; - viewport.width = (float)demo->width; + float viewport_dimension; + if (demo->width < demo->height) { + viewport_dimension = (float)demo->width; + viewport.y = (demo->height - demo->width) / 2.0f; + } else { + viewport_dimension = (float)demo->height; + viewport.x = (demo->width - demo->height) / 2.0f; + } + viewport.height = viewport_dimension; + viewport.width = viewport_dimension; viewport.minDepth = (float)0.0f; viewport.maxDepth = (float)1.0f; vkCmdSetViewport(cmd_buf, 0, 1, &viewport); @@ -3763,7 +3770,7 @@ static void demo_init(struct demo *demo, int argc, char **argv) { memset(demo, 0, sizeof(*demo)); demo->presentMode = VK_PRESENT_MODE_FIFO_KHR; demo->frameCount = INT32_MAX; - + for (int i = 1; i < argc; i++) { if (strcmp(argv[i], "--use_staging") == 0) { demo->use_staging_buffer = true; @@ -3953,7 +3960,6 @@ int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, #elif defined(VK_USE_PLATFORM_IOS_MVK) || defined(VK_USE_PLATFORM_MACOS_MVK) static void demo_main(struct demo *demo, void *view, int argc, const char *argv[]) { - demo_init(demo, argc, (char **)argv); demo->window = view; demo_init_vk_swapchain(demo); -- cgit v1.2.3