Skip to content

Commit

Permalink
Update sdl example for best swapchain recreation
Browse files Browse the repository at this point in the history
  • Loading branch information
RoryO committed Aug 6, 2020
1 parent 797b765 commit a487ee4
Showing 1 changed file with 15 additions and 17 deletions.
32 changes: 15 additions & 17 deletions examples/example_sdl_vulkan/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ static uint32_t g_MinImageCount = 2;
static bool g_SwapChainRebuild = false;
static int g_SwapChainResizeWidth = 0;
static int g_SwapChainResizeHeight = 0;
static SDL_Window* g_Window;

static void check_vk_result(VkResult err)
{
Expand Down Expand Up @@ -313,6 +314,12 @@ static void FramePresent(ImGui_ImplVulkanH_Window* wd)
info.pSwapchains = &wd->Swapchain;
info.pImageIndices = &wd->FrameIndex;
VkResult err = vkQueuePresentKHR(g_Queue, &info);
if(err == VK_ERROR_OUT_OF_DATE_KHR)
{
SDL_GetWindowSize(g_Window, &g_SwapChainResizeWidth, &g_SwapChainResizeHeight);
g_SwapChainRebuild = true;
return;
}
check_vk_result(err);
wd->SemaphoreIndex = (wd->SemaphoreIndex + 1) % wd->ImageCount; // Now we can use the next set of semaphores
}
Expand All @@ -328,28 +335,28 @@ int main(int, char**)

// Setup window
SDL_WindowFlags window_flags = (SDL_WindowFlags)(SDL_WINDOW_VULKAN | SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI);
SDL_Window* window = SDL_CreateWindow("Dear ImGui SDL2+Vulkan example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags);
g_Window = SDL_CreateWindow("Dear ImGui SDL2+Vulkan example", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1280, 720, window_flags);

// Setup Vulkan
uint32_t extensions_count = 0;
SDL_Vulkan_GetInstanceExtensions(window, &extensions_count, NULL);
SDL_Vulkan_GetInstanceExtensions(g_Window, &extensions_count, NULL);
const char** extensions = new const char*[extensions_count];
SDL_Vulkan_GetInstanceExtensions(window, &extensions_count, extensions);
SDL_Vulkan_GetInstanceExtensions(g_Window, &extensions_count, extensions);
SetupVulkan(extensions, extensions_count);
delete[] extensions;

// Create Window Surface
VkSurfaceKHR surface;
VkResult err;
if (SDL_Vulkan_CreateSurface(window, g_Instance, &surface) == 0)
if (SDL_Vulkan_CreateSurface(g_Window, g_Instance, &surface) == 0)
{
printf("Failed to create Vulkan surface.\n");
return 1;
}

// Create Framebuffers
int w, h;
SDL_GetWindowSize(window, &w, &h);
SDL_GetWindowSize(g_Window, &w, &h);
ImGui_ImplVulkanH_Window* wd = &g_MainWindowData;
SetupVulkanWindow(wd, surface, w, h);

Expand All @@ -365,7 +372,7 @@ int main(int, char**)
//ImGui::StyleColorsClassic();

// Setup Platform/Renderer bindings
ImGui_ImplSDL2_InitForVulkan(window);
ImGui_ImplSDL2_InitForVulkan(g_Window);
ImGui_ImplVulkan_InitInfo init_info = {};
init_info.Instance = g_Instance;
init_info.PhysicalDevice = g_PhysicalDevice;
Expand Down Expand Up @@ -445,15 +452,6 @@ int main(int, char**)
ImGui_ImplSDL2_ProcessEvent(&event);
if (event.type == SDL_QUIT)
done = true;
if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_RESIZED && event.window.windowID == SDL_GetWindowID(window))
{
// Note: your own application may rely on SDL_WINDOWEVENT_MINIMIZED/SDL_WINDOWEVENT_RESTORED to skip updating all-together.
// Here ImGui_ImplSDL2_NewFrame() will set io.DisplaySize to zero which will disable rendering but let application run.
// Please note that you can't Present into a minimized window.
g_SwapChainResizeWidth = (int)event.window.data1;
g_SwapChainResizeHeight = (int)event.window.data2;
g_SwapChainRebuild = true;
}
}

// Resize swap chain?
Expand All @@ -467,7 +465,7 @@ int main(int, char**)

// Start the Dear ImGui frame
ImGui_ImplVulkan_NewFrame();
ImGui_ImplSDL2_NewFrame(window);
ImGui_ImplSDL2_NewFrame(g_Window);
ImGui::NewFrame();

// 1. Show the big demo window (Most of the sample code is in ImGui::ShowDemoWindow()! You can browse its code to learn more about Dear ImGui!).
Expand Down Expand Up @@ -528,7 +526,7 @@ int main(int, char**)
CleanupVulkanWindow();
CleanupVulkan();

SDL_DestroyWindow(window);
SDL_DestroyWindow(g_Window);
SDL_Quit();

return 0;
Expand Down

0 comments on commit a487ee4

Please sign in to comment.