From 155a484e6765953bc4e1d55dd1e4ee4b4fe9f023 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Wed, 10 Jun 2015 22:26:03 +0200 Subject: [PATCH] Platform: added Sdl2Application::tickEvent(). If implemented by the user, it is called periodically after processing input events and before draw event and does not depend on whether there are any input events or the application needs to redraw. --- src/Magnum/Platform/Sdl2Application.cpp | 15 +++++++++++++-- src/Magnum/Platform/Sdl2Application.h | 14 +++++++++++++- 2 files changed, 26 insertions(+), 3 deletions(-) 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 };