mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-20 23:52:40 +08:00
102 lines
3.2 KiB
C++
102 lines
3.2 KiB
C++
/****************************************************************************
|
|
Copyright (c) 2020 Xiamen Yaji Software Co., Ltd.
|
|
|
|
http://www.cocos.com
|
|
|
|
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
of this software and associated documentation files (the "Software"), to deal
|
|
in the Software without restriction, including without limitation the rights
|
|
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
copies of the Software, and to permit persons to whom the Software is
|
|
furnished to do so, subject to the following conditions:
|
|
|
|
The above copyright notice and this permission notice shall be included in
|
|
all copies or substantial portions of the Software.
|
|
|
|
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
|
THE SOFTWARE.
|
|
****************************************************************************/
|
|
|
|
#include "cocos/platform/Application.h"
|
|
#include "cocos/bindings/jswrapper/SeApi.h"
|
|
|
|
#if USE_AUDIO
|
|
#include "cocos/audio/include/AudioEngine.h"
|
|
#endif
|
|
|
|
#if USE_SOCKET
|
|
#include "cocos/network/WebSocket.h"
|
|
#endif
|
|
|
|
#include "cocos/network/HttpClient.h"
|
|
|
|
namespace cc {
|
|
void Application::restartVM() {
|
|
|
|
cc::EventDispatcher::dispatchRestartVM();
|
|
|
|
auto scriptEngine = se::ScriptEngine::getInstance();
|
|
|
|
cc::PoolManager::getInstance()->getCurrentPool()->clear();
|
|
#if USE_AUDIO
|
|
cc::AudioEngine::stopAll();
|
|
#endif
|
|
#if USE_SOCKET
|
|
cc::network::WebSocket::closeAllConnections();
|
|
#endif
|
|
cc::network::HttpClient::destroyInstance();
|
|
|
|
_scheduler->removeAllFunctionsToBePerformedInCocosThread();
|
|
_scheduler->unscheduleAll();
|
|
|
|
scriptEngine->cleanup();
|
|
cc::EventDispatcher::destroy();
|
|
|
|
// start
|
|
|
|
cc::EventDispatcher::init();
|
|
init();
|
|
}
|
|
|
|
void Application::tick() {
|
|
|
|
if (_needRestart) {
|
|
restartVM();
|
|
_needRestart = false;
|
|
}
|
|
|
|
static std::chrono::steady_clock::time_point prevTime;
|
|
static std::chrono::steady_clock::time_point now;
|
|
static float dt = 0.f;
|
|
static double dtNS = NANOSECONDS_60FPS;
|
|
|
|
++_totalFrames;
|
|
|
|
// iOS/macOS use its own fps limitation algorithm.
|
|
#if (CC_PLATFORM == CC_PLATFORM_ANDROID || CC_PLATFORM == CC_PLATFORM_WINDOWS)
|
|
if (dtNS < _prefererredNanosecondsPerFrame) {
|
|
std::this_thread::sleep_for(
|
|
std::chrono::nanoseconds(_prefererredNanosecondsPerFrame - static_cast<long>(dtNS)));
|
|
dtNS = _prefererredNanosecondsPerFrame;
|
|
}
|
|
#endif
|
|
|
|
prevTime = std::chrono::steady_clock::now();
|
|
|
|
_scheduler->update(dt);
|
|
cc::EventDispatcher::dispatchTickEvent(dt);
|
|
|
|
PoolManager::getInstance()->getCurrentPool()->clear();
|
|
|
|
now = std::chrono::steady_clock::now();
|
|
dtNS = dtNS * 0.1 + 0.9 * std::chrono::duration_cast<std::chrono::nanoseconds>(now - prevTime).count();
|
|
dt = (float)dtNS / NANOSECONDS_PER_SECOND;
|
|
}
|
|
|
|
} // namespace cc
|