|
@@ -23,6 +23,7 @@
|
|
|
#define IMGUI_VULKAN_DEBUG_REPORT
|
|
|
#endif
|
|
|
|
|
|
+// Data
|
|
|
static VkAllocationCallbacks* g_Allocator = NULL;
|
|
|
static VkInstance g_Instance = VK_NULL_HANDLE;
|
|
|
static VkPhysicalDevice g_PhysicalDevice = VK_NULL_HANDLE;
|
|
@@ -33,10 +34,14 @@ static VkDebugReportCallbackEXT g_DebugReport = VK_NULL_HANDLE;
|
|
|
static VkPipelineCache g_PipelineCache = VK_NULL_HANDLE;
|
|
|
static VkDescriptorPool g_DescriptorPool = VK_NULL_HANDLE;
|
|
|
|
|
|
+static SDL_Window* g_AppWindow = NULL;
|
|
|
static ImGui_ImplVulkanH_Window g_MainWindowData;
|
|
|
static uint32_t g_MinImageCount = 2;
|
|
|
static bool g_SwapChainRebuild = false;
|
|
|
|
|
|
+// Forward declarations of helper functions
|
|
|
+bool MainLoopStep();
|
|
|
+
|
|
|
static void check_vk_result(VkResult err)
|
|
|
{
|
|
|
if (err == 0)
|
|
@@ -339,6 +344,7 @@ static void FramePresent(ImGui_ImplVulkanH_Window* wd)
|
|
|
wd->SemaphoreIndex = (wd->SemaphoreIndex + 1) % wd->ImageCount; // Now we can use the next set of semaphores
|
|
|
}
|
|
|
|
|
|
+// Main code
|
|
|
int main(int, char**)
|
|
|
{
|
|
|
// Setup SDL
|
|
@@ -348,17 +354,16 @@ int main(int, char**)
|
|
|
return -1;
|
|
|
}
|
|
|
|
|
|
- // Setup window
|
|
|
+ // Create window with Vulkan graphics context
|
|
|
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);
|
|
|
-
|
|
|
- // Setup Vulkan
|
|
|
uint32_t extensions_count = 0;
|
|
|
SDL_Vulkan_GetInstanceExtensions(window, &extensions_count, NULL);
|
|
|
const char** extensions = new const char*[extensions_count];
|
|
|
SDL_Vulkan_GetInstanceExtensions(window, &extensions_count, extensions);
|
|
|
SetupVulkan(extensions, extensions_count);
|
|
|
delete[] extensions;
|
|
|
+ g_AppWindow = window;
|
|
|
|
|
|
// Create Window Surface
|
|
|
VkSurfaceKHR surface;
|
|
@@ -462,121 +467,131 @@ int main(int, char**)
|
|
|
ImGui_ImplVulkan_DestroyFontUploadObjects();
|
|
|
}
|
|
|
|
|
|
- // Our state
|
|
|
- bool show_demo_window = true;
|
|
|
- bool show_another_window = false;
|
|
|
- ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
|
|
|
-
|
|
|
// Main loop
|
|
|
- bool done = false;
|
|
|
- while (!done)
|
|
|
+ while (true)
|
|
|
{
|
|
|
- // Poll and handle events (inputs, window resize, etc.)
|
|
|
- // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
|
|
|
- // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
|
|
|
- // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
|
|
|
- // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
|
|
|
- SDL_Event event;
|
|
|
- while (SDL_PollEvent(&event))
|
|
|
- {
|
|
|
- ImGui_ImplSDL2_ProcessEvent(&event);
|
|
|
- if (event.type == SDL_QUIT)
|
|
|
- done = true;
|
|
|
- if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window))
|
|
|
- done = true;
|
|
|
- }
|
|
|
+ if (!MainLoopStep())
|
|
|
+ break;
|
|
|
+ }
|
|
|
|
|
|
- // Resize swap chain?
|
|
|
- if (g_SwapChainRebuild)
|
|
|
- {
|
|
|
- int width, height;
|
|
|
- SDL_GetWindowSize(window, &width, &height);
|
|
|
- if (width > 0 && height > 0)
|
|
|
- {
|
|
|
- ImGui_ImplVulkan_SetMinImageCount(g_MinImageCount);
|
|
|
- ImGui_ImplVulkanH_CreateOrResizeWindow(g_Instance, g_PhysicalDevice, g_Device, &g_MainWindowData, g_QueueFamily, g_Allocator, width, height, g_MinImageCount);
|
|
|
- g_MainWindowData.FrameIndex = 0;
|
|
|
- g_SwapChainRebuild = false;
|
|
|
- }
|
|
|
- }
|
|
|
+ // Cleanup
|
|
|
+ err = vkDeviceWaitIdle(g_Device);
|
|
|
+ check_vk_result(err);
|
|
|
+ ImGui_ImplVulkan_Shutdown();
|
|
|
+ ImGui_ImplSDL2_Shutdown();
|
|
|
+ ImGui::DestroyContext();
|
|
|
+
|
|
|
+ CleanupVulkanWindow();
|
|
|
+ CleanupVulkan();
|
|
|
+
|
|
|
+ SDL_DestroyWindow(window);
|
|
|
+ SDL_Quit();
|
|
|
|
|
|
- // Start the Dear ImGui frame
|
|
|
- ImGui_ImplVulkan_NewFrame();
|
|
|
- ImGui_ImplSDL2_NewFrame();
|
|
|
- ImGui::NewFrame();
|
|
|
+ return 0;
|
|
|
+}
|
|
|
|
|
|
- // 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!).
|
|
|
- if (show_demo_window)
|
|
|
- ImGui::ShowDemoWindow(&show_demo_window);
|
|
|
+bool MainLoopStep()
|
|
|
+{
|
|
|
+ // Poll and handle events (inputs, window resize, etc.)
|
|
|
+ // You can read the io.WantCaptureMouse, io.WantCaptureKeyboard flags to tell if dear imgui wants to use your inputs.
|
|
|
+ // - When io.WantCaptureMouse is true, do not dispatch mouse input data to your main application, or clear/overwrite your copy of the mouse data.
|
|
|
+ // - When io.WantCaptureKeyboard is true, do not dispatch keyboard input data to your main application, or clear/overwrite your copy of the keyboard data.
|
|
|
+ // Generally you may always pass all inputs to dear imgui, and hide them from your application based on those two flags.
|
|
|
+ SDL_Window* window = g_AppWindow;
|
|
|
+ SDL_Event event;
|
|
|
+ while (SDL_PollEvent(&event))
|
|
|
+ {
|
|
|
+ ImGui_ImplSDL2_ProcessEvent(&event);
|
|
|
+ if (event.type == SDL_QUIT)
|
|
|
+ return false;
|
|
|
+ if (event.type == SDL_WINDOWEVENT && event.window.event == SDL_WINDOWEVENT_CLOSE && event.window.windowID == SDL_GetWindowID(window))
|
|
|
+ return false;
|
|
|
+ }
|
|
|
|
|
|
- // 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window.
|
|
|
+ // Resize swap chain?
|
|
|
+ if (g_SwapChainRebuild)
|
|
|
+ {
|
|
|
+ int width, height;
|
|
|
+ SDL_GetWindowSize(window, &width, &height);
|
|
|
+ if (width > 0 && height > 0)
|
|
|
{
|
|
|
- static float f = 0.0f;
|
|
|
- static int counter = 0;
|
|
|
+ ImGui_ImplVulkan_SetMinImageCount(g_MinImageCount);
|
|
|
+ ImGui_ImplVulkanH_CreateOrResizeWindow(g_Instance, g_PhysicalDevice, g_Device, &g_MainWindowData, g_QueueFamily, g_Allocator, width, height, g_MinImageCount);
|
|
|
+ g_MainWindowData.FrameIndex = 0;
|
|
|
+ g_SwapChainRebuild = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
|
|
|
- ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
|
|
|
+ // Start the Dear ImGui frame
|
|
|
+ ImGui_ImplVulkan_NewFrame();
|
|
|
+ ImGui_ImplSDL2_NewFrame();
|
|
|
+ ImGui::NewFrame();
|
|
|
|
|
|
- ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
|
|
|
- ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
|
|
|
- ImGui::Checkbox("Another Window", &show_another_window);
|
|
|
+ // Our state
|
|
|
+ // (we use static, which essentially makes the variable globals, as a convenience to keep the example code easy to follow)
|
|
|
+ static bool show_demo_window = true;
|
|
|
+ static bool show_another_window = false;
|
|
|
+ static ImVec4 clear_color = ImVec4(0.45f, 0.55f, 0.60f, 1.00f);
|
|
|
|
|
|
- ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
|
|
|
- ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
|
|
|
+ // 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!).
|
|
|
+ if (show_demo_window)
|
|
|
+ ImGui::ShowDemoWindow(&show_demo_window);
|
|
|
|
|
|
- if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
|
|
|
- counter++;
|
|
|
- ImGui::SameLine();
|
|
|
- ImGui::Text("counter = %d", counter);
|
|
|
+ // 2. Show a simple window that we create ourselves. We use a Begin/End pair to create a named window.
|
|
|
+ {
|
|
|
+ static float f = 0.0f;
|
|
|
+ static int counter = 0;
|
|
|
|
|
|
- ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
|
|
|
- ImGui::End();
|
|
|
- }
|
|
|
+ ImGui::Begin("Hello, world!"); // Create a window called "Hello, world!" and append into it.
|
|
|
|
|
|
- // 3. Show another simple window.
|
|
|
- if (show_another_window)
|
|
|
- {
|
|
|
- ImGui::Begin("Another Window", &show_another_window); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked)
|
|
|
- ImGui::Text("Hello from another window!");
|
|
|
- if (ImGui::Button("Close Me"))
|
|
|
- show_another_window = false;
|
|
|
- ImGui::End();
|
|
|
- }
|
|
|
+ ImGui::Text("This is some useful text."); // Display some text (you can use a format strings too)
|
|
|
+ ImGui::Checkbox("Demo Window", &show_demo_window); // Edit bools storing our window open/close state
|
|
|
+ ImGui::Checkbox("Another Window", &show_another_window);
|
|
|
|
|
|
- // Rendering
|
|
|
- ImGui::Render();
|
|
|
- ImDrawData* main_draw_data = ImGui::GetDrawData();
|
|
|
- const bool main_is_minimized = (main_draw_data->DisplaySize.x <= 0.0f || main_draw_data->DisplaySize.y <= 0.0f);
|
|
|
- wd->ClearValue.color.float32[0] = clear_color.x * clear_color.w;
|
|
|
- wd->ClearValue.color.float32[1] = clear_color.y * clear_color.w;
|
|
|
- wd->ClearValue.color.float32[2] = clear_color.z * clear_color.w;
|
|
|
- wd->ClearValue.color.float32[3] = clear_color.w;
|
|
|
- if (!main_is_minimized)
|
|
|
- FrameRender(wd, main_draw_data);
|
|
|
-
|
|
|
- // Update and Render additional Platform Windows
|
|
|
- if (io.ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
|
|
|
- {
|
|
|
- ImGui::UpdatePlatformWindows();
|
|
|
- ImGui::RenderPlatformWindowsDefault();
|
|
|
- }
|
|
|
+ ImGui::SliderFloat("float", &f, 0.0f, 1.0f); // Edit 1 float using a slider from 0.0f to 1.0f
|
|
|
+ ImGui::ColorEdit3("clear color", (float*)&clear_color); // Edit 3 floats representing a color
|
|
|
|
|
|
- // Present Main Platform Window
|
|
|
- if (!main_is_minimized)
|
|
|
- FramePresent(wd);
|
|
|
+ if (ImGui::Button("Button")) // Buttons return true when clicked (most widgets return true when edited/activated)
|
|
|
+ counter++;
|
|
|
+ ImGui::SameLine();
|
|
|
+ ImGui::Text("counter = %d", counter);
|
|
|
+
|
|
|
+ ImGui::Text("Application average %.3f ms/frame (%.1f FPS)", 1000.0f / ImGui::GetIO().Framerate, ImGui::GetIO().Framerate);
|
|
|
+ ImGui::End();
|
|
|
}
|
|
|
|
|
|
- // Cleanup
|
|
|
- err = vkDeviceWaitIdle(g_Device);
|
|
|
- check_vk_result(err);
|
|
|
- ImGui_ImplVulkan_Shutdown();
|
|
|
- ImGui_ImplSDL2_Shutdown();
|
|
|
- ImGui::DestroyContext();
|
|
|
+ // 3. Show another simple window.
|
|
|
+ if (show_another_window)
|
|
|
+ {
|
|
|
+ ImGui::Begin("Another Window", &show_another_window); // Pass a pointer to our bool variable (the window will have a closing button that will clear the bool when clicked)
|
|
|
+ ImGui::Text("Hello from another window!");
|
|
|
+ if (ImGui::Button("Close Me"))
|
|
|
+ show_another_window = false;
|
|
|
+ ImGui::End();
|
|
|
+ }
|
|
|
|
|
|
- CleanupVulkanWindow();
|
|
|
- CleanupVulkan();
|
|
|
+ // Rendering
|
|
|
+ ImGui::Render();
|
|
|
+ ImDrawData* main_draw_data = ImGui::GetDrawData();
|
|
|
+ const bool main_is_minimized = (main_draw_data->DisplaySize.x <= 0.0f || main_draw_data->DisplaySize.y <= 0.0f);
|
|
|
+ ImGui_ImplVulkanH_Window* wd = &g_MainWindowData;
|
|
|
+ wd->ClearValue.color.float32[0] = clear_color.x * clear_color.w;
|
|
|
+ wd->ClearValue.color.float32[1] = clear_color.y * clear_color.w;
|
|
|
+ wd->ClearValue.color.float32[2] = clear_color.z * clear_color.w;
|
|
|
+ wd->ClearValue.color.float32[3] = clear_color.w;
|
|
|
+ if (!main_is_minimized)
|
|
|
+ FrameRender(wd, main_draw_data);
|
|
|
+
|
|
|
+ // Update and Render additional Platform Windows
|
|
|
+ if (ImGui::GetIO().ConfigFlags & ImGuiConfigFlags_ViewportsEnable)
|
|
|
+ {
|
|
|
+ ImGui::UpdatePlatformWindows();
|
|
|
+ ImGui::RenderPlatformWindowsDefault();
|
|
|
+ }
|
|
|
|
|
|
- SDL_DestroyWindow(window);
|
|
|
- SDL_Quit();
|
|
|
+ // Present Main Platform Window
|
|
|
+ if (!main_is_minimized)
|
|
|
+ FramePresent(wd);
|
|
|
|
|
|
- return 0;
|
|
|
+ return true;
|
|
|
}
|