Examples: GLFW: rework examples main loop to handle minimization without burning CPU or GPU by running unthrottled code. (#7844)

Backends: GLFW: added ImGui_ImplGlfw_Sleep() helper.
This commit is contained in:
ocornut 2024-07-31 17:33:24 +02:00
parent 887478793b
commit 71ee2ce367
6 changed files with 36 additions and 2 deletions

View File

@ -20,6 +20,7 @@
// CHANGELOG
// (minor and older changes stripped away, please see git history for details)
// 2024-07-31: Added ImGui_ImplGlfw_Sleep() helper function for usage by our examples app, since GLFW doesn't provide one.
// 2024-07-08: *BREAKING* Renamed ImGui_ImplGlfw_InstallEmscriptenCanvasResizeCallback to ImGui_ImplGlfw_InstallEmscriptenCallbacks(), added GLFWWindow* parameter.
// 2024-07-08: Emscripten: Added support for GLFW3 contrib port (GLFW 3.4.0 features + bug fixes): to enable, replace -sUSE_GLFW=3 with --use-port=contrib.glfw3 (requires emscripten 3.1.59+) (https://github.com/pongasoft/emscripten-glfw)
// 2024-07-02: Emscripten: Added io.PlatformOpenInShellFn() handler for Emscripten versions.
@ -99,6 +100,9 @@
#endif
#include <GLFW/glfw3native.h> // for glfwGetCocoaWindow()
#endif
#ifndef _WIN32
#include <unistd.h> // for usleep()
#endif
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
@ -825,6 +829,16 @@ void ImGui_ImplGlfw_NewFrame()
ImGui_ImplGlfw_UpdateGamepads();
}
// GLFW doesn't provide a portable sleep function
void ImGui_ImplGlfw_Sleep(int milliseconds)
{
#ifdef _WIN32
::Sleep(milliseconds);
#else
usleep(milliseconds * 1000);
#endif
}
#ifdef EMSCRIPTEN_USE_EMBEDDED_GLFW3
static EM_BOOL ImGui_ImplGlfw_OnCanvasSizeChange(int event_type, const EmscriptenUiEvent* event, void* user_data)
{

View File

@ -57,4 +57,7 @@ IMGUI_IMPL_API void ImGui_ImplGlfw_KeyCallback(GLFWwindow* window, int key,
IMGUI_IMPL_API void ImGui_ImplGlfw_CharCallback(GLFWwindow* window, unsigned int c);
IMGUI_IMPL_API void ImGui_ImplGlfw_MonitorCallback(GLFWmonitor* monitor, int event);
// GLFW helpers
IMGUI_IMPL_API void ImGui_ImplGlfw_Sleep(int milliseconds);
#endif // #ifndef IMGUI_DISABLE

View File

@ -43,8 +43,10 @@ Breaking changes:
Other changes:
- Examples: SDL2 (all), SDL3 (all), Win32+OpenGL3: rework examples main loop to handle
minimization without burning CPU or GPU by running unthrottled code. (#7844)
- Backends: GLFW: added ImGui_ImplGlfw_Sleep() helper function because GLFW does not
provide a way to do a portable sleep. (#7844)
- Examples: GLFW (all), SDL2 (all), SDL3 (all), Win32+OpenGL3: rework examples main loop
to handle minimization without burning CPU or GPU by running unthrottled code. (#7844)

View File

@ -91,6 +91,11 @@ int main(int, char**)
// - 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.
glfwPollEvents();
if (glfwGetWindowAttrib(window, GLFW_ICONIFIED) != 0)
{
ImGui_ImplGlfw_Sleep(10);
continue;
}
// Start the Dear ImGui frame
ImGui_ImplOpenGL2_NewFrame();

View File

@ -127,6 +127,11 @@ int main(int, char**)
// - 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.
glfwPollEvents();
if (glfwGetWindowAttrib(window, GLFW_ICONIFIED) != 0)
{
ImGui_ImplGlfw_Sleep(10);
continue;
}
// Start the Dear ImGui frame
ImGui_ImplOpenGL3_NewFrame();

View File

@ -494,6 +494,11 @@ int main(int, char**)
g_MainWindowData.FrameIndex = 0;
g_SwapChainRebuild = false;
}
if (glfwGetWindowAttrib(window, GLFW_ICONIFIED) != 0)
{
ImGui_ImplGlfw_Sleep(10);
continue;
}
// Start the Dear ImGui frame
ImGui_ImplVulkan_NewFrame();