Skip to content

Commit

Permalink
Examples: GLFW: rework examples main loop to handle minimization with…
Browse files Browse the repository at this point in the history
…out burning CPU or GPU by running unthrottled code. (#7844)

Backends: GLFW: added ImGui_ImplGlfw_Sleep() helper.
  • Loading branch information
ocornut committed Jul 31, 2024
1 parent 8874787 commit 71ee2ce
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 2 deletions.
14 changes: 14 additions & 0 deletions backends/imgui_impl_glfw.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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>
Expand Down Expand Up @@ -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)
{
Expand Down
3 changes: 3 additions & 0 deletions backends/imgui_impl_glfw.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
6 changes: 4 additions & 2 deletions docs/CHANGELOG.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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)



Expand Down
5 changes: 5 additions & 0 deletions examples/example_glfw_opengl2/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 5 additions & 0 deletions examples/example_glfw_opengl3/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
5 changes: 5 additions & 0 deletions examples/example_glfw_vulkan/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down

0 comments on commit 71ee2ce

Please sign in to comment.