From 82f53862e12a7318bb1e3c62e8307422290b36ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Vondru=C5=A1?= Date: Sat, 19 Oct 2019 00:37:45 +0200 Subject: [PATCH] Platform: Sdl2Application::mainLoopIteration() now returns a bool. To indicate when the app desires to exit. --- doc/changelog.dox | 2 ++ src/Magnum/Platform/Sdl2Application.cpp | 21 ++++++++++++--------- src/Magnum/Platform/Sdl2Application.h | 8 ++++---- 3 files changed, 18 insertions(+), 13 deletions(-) diff --git a/doc/changelog.dox b/doc/changelog.dox index fde1b6bd6..42c9e1d4e 100644 --- a/doc/changelog.dox +++ b/doc/changelog.dox @@ -500,6 +500,8 @@ See also: @ref Platform::GlfwApplication::exit() now have an optional parameter to specify the actual application return code (see [mosra/magnum#332](https://github.com/mosra/magnum/pull/332)) +- @ref Platform::Sdl2Application::mainLoopIteration() now returns a + @cpp bool @ce to indicate if the application should exit - @ref Platform::GlfwApplication and @ref Platform::EmscriptenApplication now implement @ref Platform::GlfwApplication::MouseMoveEvent::relativePosition() as well for better compatibility with @ref Platform::Sdl2Application. The diff --git a/src/Magnum/Platform/Sdl2Application.cpp b/src/Magnum/Platform/Sdl2Application.cpp index 1a0dd4118..fe028777c 100644 --- a/src/Magnum/Platform/Sdl2Application.cpp +++ b/src/Magnum/Platform/Sdl2Application.cpp @@ -692,7 +692,7 @@ Sdl2Application::~Sdl2Application() { int Sdl2Application::exec() { #ifndef CORRADE_TARGET_EMSCRIPTEN - while(!(_flags & Flag::Exit)) mainLoopIteration(); + while(mainLoopIteration()) {} #else emscripten_set_main_loop_arg([](void* arg) { static_cast(arg)->mainLoopIteration(); @@ -701,16 +701,17 @@ int Sdl2Application::exec() { return _exitCode; } -void Sdl2Application::exit(int exitCode) { - #ifndef CORRADE_TARGET_EMSCRIPTEN +void Sdl2Application::exit(const int exitCode) { + /* On Emscripten this flag is used only to indicate a desire to exit from + mainLoopIteration() */ _flags |= Flag::Exit; - #else + #ifdef CORRADE_TARGET_EMSCRIPTEN emscripten_cancel_main_loop(); #endif _exitCode = exitCode; } -void Sdl2Application::mainLoopIteration() { +bool Sdl2Application::mainLoopIteration() { #ifndef CORRADE_TARGET_EMSCRIPTEN const UnsignedInt timeBefore = _minimalLoopPeriod ? SDL_GetTicks() : 0; #endif @@ -828,12 +829,13 @@ void Sdl2Application::mainLoopIteration() { ExitEvent e{event}; exitEvent(e); if(e.isAccepted()) { - #ifndef CORRADE_TARGET_EMSCRIPTEN + /* On Emscripten this flag is used only to indicate a + desire to exit from mainLoopIteration() */ _flags |= Flag::Exit; - #else + #ifdef CORRADE_TARGET_EMSCRIPTEN emscripten_cancel_main_loop(); #endif - return; + return !(_flags & Flag::Exit); } } break; @@ -860,7 +862,7 @@ void Sdl2Application::mainLoopIteration() { } #endif - return; + return !(_flags & Flag::Exit); } #ifndef CORRADE_TARGET_EMSCRIPTEN @@ -875,6 +877,7 @@ void Sdl2Application::mainLoopIteration() { indefinitely for next input event */ if(_flags & Flag::NoTickEvent) SDL_WaitEvent(nullptr); #endif + return !(_flags & Flag::Exit); } void Sdl2Application::setMouseLocked(bool enabled) { diff --git a/src/Magnum/Platform/Sdl2Application.h b/src/Magnum/Platform/Sdl2Application.h index f630508ce..819ee41f6 100644 --- a/src/Magnum/Platform/Sdl2Application.h +++ b/src/Magnum/Platform/Sdl2Application.h @@ -504,13 +504,15 @@ class Sdl2Application { /** * @brief Run one iteration of application main loop + * @return @cpp false @ce if @ref exit() was called and the application + * should exit, @cpp true @ce otherwise * * Called internally from @ref exec(). If you want to have better * control over how the main loop behaves, you can call this function * yourself from your own `main()` function instead of it being called * automatically from @ref exec() / @ref MAGNUM_SDL2APPLICATION_MAIN(). */ - void mainLoopIteration(); + bool mainLoopIteration(); #ifndef CORRADE_TARGET_EMSCRIPTEN /** @@ -1003,9 +1005,7 @@ class Sdl2Application { VSyncEnabled = 1 << 1, NoTickEvent = 1 << 2, NoAnyEvent = 1 << 3, - #ifndef CORRADE_TARGET_EMSCRIPTEN - Exit = 1 << 4 - #endif + Exit = 1 << 4, #ifdef CORRADE_TARGET_EMSCRIPTEN TextInputActive = 1 << 5, Resizable = 1 << 6