Skip to content

Commit

Permalink
Examples: Fix for Emscripten. GLFW+WGPU: rework examples main loop to…
Browse files Browse the repository at this point in the history
… handle minimization. (#7844)

Amend 8874787, 71ee2ce
Amend ea39841 (emscripten_mainloop_stub.h)
  • Loading branch information
ocornut committed Jul 31, 2024
1 parent 71ee2ce commit fd57b25
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 3 deletions.
5 changes: 5 additions & 0 deletions examples/example_glfw_wgpu/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,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;
}

// React to changes in screen size
int width, height;
Expand Down
3 changes: 3 additions & 0 deletions examples/example_sdl3_sdlrenderer3/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,9 @@ int main(int, char**)
ImGui_ImplSDLRenderer3_RenderDrawData(ImGui::GetDrawData(), renderer);
SDL_RenderPresent(renderer);
}
#ifdef __EMSCRIPTEN__
EMSCRIPTEN_MAINLOOP_END;
#endif

// Cleanup
ImGui_ImplSDLRenderer3_Shutdown();
Expand Down
7 changes: 4 additions & 3 deletions examples/libs/emscripten/emscripten_mainloop_stub.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,20 +17,21 @@
// - So the next logical step was to refactor all examples to follow that layout of using a "main loop" function.
// This worked, but it made us lose all the nice things we had...

// Since only about 3 examples really need to run with Emscripten, here's our solution:
// Since only about 4 examples really need to run with Emscripten, here's our solution:
// - Use some weird macros and capturing lambda to turn a loop in main() into a function.
// - Hide all that crap in this file so it doesn't make our examples unusually ugly.
// As a stance and principle of Dear ImGui development we don't use C++ headers and we don't
// want to suggest to the newcomer that we would ever use C++ headers as this would affect
// the initial judgment of many of our target audience.
// - Technique is based on this idea: https://github.com/ocornut/imgui/pull/2492/
// - The do { } while (0) is to allow our code calling continue in the main loop.
#ifdef __EMSCRIPTEN__
#include <emscripten.h>
#include <functional>
static std::function<void()> MainLoopForEmscriptenP;
static void MainLoopForEmscripten() { MainLoopForEmscriptenP(); }
#define EMSCRIPTEN_MAINLOOP_BEGIN MainLoopForEmscriptenP = [&]()
#define EMSCRIPTEN_MAINLOOP_END ; emscripten_set_main_loop(MainLoopForEmscripten, 0, true)
#define EMSCRIPTEN_MAINLOOP_BEGIN MainLoopForEmscriptenP = [&]() { do
#define EMSCRIPTEN_MAINLOOP_END while (0); }; emscripten_set_main_loop(MainLoopForEmscripten, 0, true)
#else
#define EMSCRIPTEN_MAINLOOP_BEGIN
#define EMSCRIPTEN_MAINLOOP_END
Expand Down

0 comments on commit fd57b25

Please sign in to comment.