diff --git a/src/Magnum/Platform/Sdl2Application.cpp b/src/Magnum/Platform/Sdl2Application.cpp index 2efc506fa..8a3e60dcc 100644 --- a/src/Magnum/Platform/Sdl2Application.cpp +++ b/src/Magnum/Platform/Sdl2Application.cpp @@ -322,6 +322,10 @@ void Sdl2Application::mainLoop() { } } + /* Tick event */ + if(!(_flags & Flag::NoTickEvent)) tickEvent(); + + /* Draw event */ if(_flags & Flag::Redraw) { _flags &= ~Flag::Redraw; drawEvent(); @@ -346,8 +350,9 @@ void Sdl2Application::mainLoop() { SDL_Delay(_minimalLoopPeriod - loopTime); } - /* Then wait indefinitely for next input event */ - SDL_WaitEvent(nullptr); + /* Then, if the tick event doesn't need to be called periodically, wait + indefinitely for next input event */ + if(_flags & Flag::NoTickEvent) SDL_WaitEvent(nullptr); #endif } @@ -362,6 +367,12 @@ void Sdl2Application::setMouseLocked(bool enabled) { #endif } +void Sdl2Application::tickEvent() { + /* If this got called, the tick event is not implemented by user and thus + we don't need to call it ever again */ + _flags |= Flag::NoTickEvent; +} + void Sdl2Application::viewportEvent(const Vector2i&) {} void Sdl2Application::keyPressEvent(KeyEvent&) {} void Sdl2Application::keyReleaseEvent(KeyEvent&) {} diff --git a/src/Magnum/Platform/Sdl2Application.h b/src/Magnum/Platform/Sdl2Application.h index 0e03c121e..ba19e9f3b 100644 --- a/src/Magnum/Platform/Sdl2Application.h +++ b/src/Magnum/Platform/Sdl2Application.h @@ -325,6 +325,17 @@ class Sdl2Application { #else private: #endif + /** + * @brief Tick event + * + * If implemented, this function is called periodically after + * processing all input events and before draw event even though there + * might be no input events and redraw is not requested. Useful e.g. + * for asynchronous task polling. Use @ref setMinimalLoopPeriod()/ + * @ref setSwapInterval() to control main loop frequency. + */ + virtual void tickEvent(); + /** * @brief Viewport event * @@ -421,8 +432,9 @@ class Sdl2Application { enum class Flag: UnsignedByte { Redraw = 1 << 0, VSyncEnabled = 1 << 1, + NoTickEvent = 1 << 2, #ifndef CORRADE_TARGET_EMSCRIPTEN - Exit = 1 << 2 + Exit = 1 << 3 #endif };