mirror of
https://github.com/cocos/cocos4.git
synced 2026-11-20 23:52:40 +08:00
103 lines
3.3 KiB
C++
103 lines
3.3 KiB
C++
/****************************************************************************
|
|
Copyright (c) 2020-2021 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 engine source code (the "Software"), a limited,
|
|
worldwide, royalty-free, non-assignable, revocable and non-exclusive license
|
|
to use Cocos Creator solely to develop games on your target platforms. You shall
|
|
not use Cocos Creator software for developing other software or tools that's
|
|
used for developing games. You are not granted to publish, distribute,
|
|
sublicense, and/or sell copies of Cocos Creator.
|
|
|
|
The software or tools in this License Agreement are licensed, not sold.
|
|
Xiamen Yaji Software Co., Ltd. reserves all rights not expressly granted to you.
|
|
|
|
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
|