Browse Source

Split Sdl2Application mainLoopIteration in event, tickEvent and drawEvent parts

pull/580/head
Andréas Leroux 4 years ago
parent
commit
f0d7c5d959
  1. 57
      src/Magnum/Platform/Sdl2Application.cpp
  2. 28
      src/Magnum/Platform/Sdl2Application.h

57
src/Magnum/Platform/Sdl2Application.cpp

@ -847,21 +847,7 @@ void Sdl2Application::exit(const int exitCode) {
_exitCode = exitCode; _exitCode = exitCode;
} }
bool Sdl2Application::mainLoopIteration() { bool Sdl2Application::mainLoopEventIteration() {
/* If exit was requested directly in the constructor, exit immediately
without calling anything else */
if(_flags & Flag::Exit) return false;
#ifndef CORRADE_TARGET_EMSCRIPTEN
CORRADE_ASSERT(_window, "Platform::Sdl2Application::mainLoopIteration(): no window opened", {});
#else
CORRADE_ASSERT(_surface, "Platform::Sdl2Application::mainLoopIteration(): no window opened", {});
#endif
#ifndef CORRADE_TARGET_EMSCRIPTEN
const UnsignedInt timeBefore = _minimalLoopPeriod ? SDL_GetTicks() : 0;
#endif
#ifdef CORRADE_TARGET_EMSCRIPTEN #ifdef CORRADE_TARGET_EMSCRIPTEN
/* The resize event is not fired on window resize, so poll for the canvas /* The resize event is not fired on window resize, so poll for the canvas
size here. But only if the window was requested to be resizable, to size here. But only if the window was requested to be resizable, to
@ -993,15 +979,50 @@ bool Sdl2Application::mainLoopIteration() {
default: if(!(_flags & Flag::NoAnyEvent)) anyEvent(event); default: if(!(_flags & Flag::NoAnyEvent)) anyEvent(event);
} }
} }
return !(_flags & Flag::Exit);
}
/* Tick event */ bool Sdl2Application::mainLoopTickEventIteration() {
if(!(_flags & Flag::NoTickEvent)) tickEvent(); if(!(_flags & Flag::NoTickEvent)) {
tickEvent();
return true;
}
return false;
}
/* Draw event */ bool Sdl2Application::mainLoopDrawEventIteration() {
if(_flags & Flag::Redraw) { if(_flags & Flag::Redraw) {
_flags &= ~Flag::Redraw; _flags &= ~Flag::Redraw;
drawEvent(); drawEvent();
return true;
}
return false;
}
bool Sdl2Application::mainLoopIteration() {
/* If exit was requested directly in the constructor, exit immediately
without calling anything else */
if(_flags & Flag::Exit) return false;
#ifndef CORRADE_TARGET_EMSCRIPTEN
CORRADE_ASSERT(_window, "Platform::Sdl2Application::mainLoopIteration(): no window opened", {});
#else
CORRADE_ASSERT(_surface, "Platform::Sdl2Application::mainLoopIteration(): no window opened", {});
#endif
#ifndef CORRADE_TARGET_EMSCRIPTEN
const UnsignedInt timeBefore = _minimalLoopPeriod ? SDL_GetTicks() : 0;
#endif
/* exit was requested in the event loop */
if (!mainLoopEventIteration())
return false;
/* call tickEvent() if implemented */
mainLoopTickEventIteration();
/* drawEvent() was called */
if (mainLoopDrawEventIteration()) {
#ifndef CORRADE_TARGET_EMSCRIPTEN #ifndef CORRADE_TARGET_EMSCRIPTEN
/* If VSync is not enabled, delay to prevent CPU hogging (if set) */ /* If VSync is not enabled, delay to prevent CPU hogging (if set) */
if(!(_flags & Flag::VSyncEnabled) && _minimalLoopPeriod) { if(!(_flags & Flag::VSyncEnabled) && _minimalLoopPeriod) {

28
src/Magnum/Platform/Sdl2Application.h

@ -571,13 +571,41 @@ class Sdl2Application {
* @return @cpp false @ce if @ref exit() was called and the application * @return @cpp false @ce if @ref exit() was called and the application
* should exit, @cpp true @ce otherwise * should exit, @cpp true @ce otherwise
* *
* Calls @ref mainLoopEventIteration(), @ref mainLoopTickEventIteration() and
* @ref mainLoopDrawEventIteration() managing the delays between them.
* Called internally from @ref exec(). If you want to have better * Called internally from @ref exec(). If you want to have better
* control over how the main loop behaves, you can call this function * 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 * yourself from your own `main()` function instead of it being called
* automatically from @ref exec() / @ref MAGNUM_SDL2APPLICATION_MAIN(). * automatically from @ref exec() / @ref MAGNUM_SDL2APPLICATION_MAIN().
*/ */
bool mainLoopIteration(); 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 tickEvent() if implemented
* @return @cpp true @ce if @ref tickEvent() was called, @cpp false @ce otherwise
*
* Called internally from @ref mainLoopIteration().
*/
bool mainLoopTickEventIteration();
/**
* @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 * @brief Exit application
* @param exitCode The exit code the application should return * @param exitCode The exit code the application should return

Loading…
Cancel
Save