From 3437fa5edf2016e8f1e74c268c60d5741de0551e Mon Sep 17 00:00:00 2001 From: Benau Date: Fri, 18 Oct 2019 19:08:21 +0800 Subject: [PATCH] Fix iOS crashing when going to background --- src/main_loop.cpp | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/src/main_loop.cpp b/src/main_loop.cpp index b993b2eef..1fd206a2b 100644 --- a/src/main_loop.cpp +++ b/src/main_loop.cpp @@ -19,6 +19,7 @@ #include "main_loop.hpp" +#include "audio/music_manager.hpp" #include "audio/sfx_manager.hpp" #include "config/user_config.hpp" #include "graphics/central_settings.hpp" @@ -110,6 +111,35 @@ MainLoop::~MainLoop() float MainLoop::getLimitedDt() { m_prev_time = m_curr_time; + +#ifdef IOS_STK + IrrlichtDevice* dev = irr_driver->getDevice(); + if (dev) + { + // When ios apps entering background it should not run any + // opengl command from apple document, so we stop here + bool win_active = dev->isWindowActive(); + bool has_focus = dev->isWindowFocused(); + bool first_out_focus = !has_focus; + while (!has_focus || !win_active) + { + if (first_out_focus) + { + first_out_focus = false; + music_manager->pauseMusic(); + SFXManager::get()->pauseAll(); + } + dev->run(); + win_active = dev->isWindowActive(); + has_focus = dev->isWindowFocused(); + if (has_focus && win_active) + { + music_manager->resumeMusic(); + SFXManager::get()->resumeAll(); + } + } + } +#endif float dt = 0; // In profile mode without graphics, run with a fixed dt of 1/60