diff --git a/src/Magnum/Platform/AbstractXApplication.cpp b/src/Magnum/Platform/AbstractXApplication.cpp index ca224d2d2..0d4547760 100644 --- a/src/Magnum/Platform/AbstractXApplication.cpp +++ b/src/Magnum/Platform/AbstractXApplication.cpp @@ -139,11 +139,7 @@ int AbstractXApplication::exec() { return _exitCode; } -bool AbstractXApplication::mainLoopIteration() { - /* If exit was requested directly in the constructor, exit immediately - without calling anything else */ - if(_flags & Flag::Exit) return false; - +bool AbstractXApplication::mainLoopEventIteration() { XEvent event; /* Closed window */ @@ -184,11 +180,29 @@ bool AbstractXApplication::mainLoopIteration() { } break; } } +} +bool AbstractXApplication::mainLoopDrawEventIteration() { if(_flags & Flag::Redraw) { _flags &= ~Flag::Redraw; drawEvent(); - } else Utility::System::sleep(5); + return true; + } + return false; +} + +bool AbstractXApplication::mainLoopIteration() { + /* If exit was requested directly in the constructor, exit immediately + without calling anything else */ + if(_flags & Flag::Exit) return false; + + /* exit was requested in the event loop */ + if (!mainLoopEventIteration()) + return false; + + /* If not drawing anything, delay to prevent CPU hogging */ + if (!mainLoopDraweventIteration()) + Utility::System::sleep(5); return !(_flags & Flag::Exit); } diff --git a/src/Magnum/Platform/AbstractXApplication.h b/src/Magnum/Platform/AbstractXApplication.h index cfa22db03..07bdb2982 100644 --- a/src/Magnum/Platform/AbstractXApplication.h +++ b/src/Magnum/Platform/AbstractXApplication.h @@ -134,14 +134,34 @@ class AbstractXApplication { * should exit, @cpp true @ce otherwise * @m_since{2020,06} * + * Calls @ref mainLoopEventIteration() and @ref mainLoopDrawEventIteration() + * managing the delays between them. * Called internally from @ref exec(). If you want to have better * control over how the main loop behaves, you can call this function + * (or the sub mainLoopIteration functions) * yourself from your own `main()` function instead of it being called * automatically from @ref exec() / @ref MAGNUM_GLXAPPLICATION_MAIN() * / @ref MAGNUM_XEGLAPPLICATION_MAIN(). */ bool mainLoopIteration(); + /** + * @brief Process pending application events + * @return @cpp false @ce if @ref exit() was called and the application + * should exit, @cpp true @ce otherwise + * + * Called internally from @ref mainLoopIteration(). + */ + bool mainLoopEventIteration(); + + /** + * @brief Calls @ref drawEvent() if @ref Flag::Redraw is set and unset it. + * @return @cpp true @ce if @ref drawEvent() was called, @cpp false @ce otherwise + * + * Called internally from @ref mainLoopIteration(). + */ + bool mainLoopDrawEventIteration(); + /** * @brief Exit application * @param exitCode The exit code the application should return