no message
This commit is contained in:
57
cocos/application/ApplicationManager.cpp
Normal file
57
cocos/application/ApplicationManager.cpp
Normal file
@@ -0,0 +1,57 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2021-2023 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 "application/ApplicationManager.h"
|
||||
#include "base/Macros.h"
|
||||
|
||||
namespace cc {
|
||||
// static
|
||||
ApplicationManager *ApplicationManager::getInstance() {
|
||||
static ApplicationManager mgr;
|
||||
return &mgr;
|
||||
}
|
||||
|
||||
void ApplicationManager::releseAllApplications() {
|
||||
_apps.clear();
|
||||
}
|
||||
|
||||
ApplicationManager::ApplicationPtr ApplicationManager::getCurrentApp() const {
|
||||
if (_currentApp.expired()) {
|
||||
return nullptr;
|
||||
}
|
||||
return _currentApp.lock();
|
||||
}
|
||||
|
||||
ApplicationManager::ApplicationPtr ApplicationManager::getCurrentAppSafe() const {
|
||||
CC_ASSERT(!_currentApp.expired());
|
||||
return _currentApp.lock();
|
||||
}
|
||||
} // namespace cc
|
||||
|
||||
//
|
||||
void cocos_destory() { // NOLINT(readability-identifier-naming)
|
||||
// Called in the platform layer, because the platform layer is isolated from the application layer
|
||||
// It is the platform layer to drive applications and reclaim resources.
|
||||
cc::ApplicationManager::getInstance()->releseAllApplications();
|
||||
}
|
||||
96
cocos/application/ApplicationManager.h
Normal file
96
cocos/application/ApplicationManager.h
Normal file
@@ -0,0 +1,96 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2017-2023 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.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include "application/BaseApplication.h"
|
||||
#include "base/std/container/vector.h"
|
||||
|
||||
namespace cc {
|
||||
class CC_DLL ApplicationManager {
|
||||
public:
|
||||
static ApplicationManager* getInstance();
|
||||
|
||||
using ApplicationPtr = std::shared_ptr<BaseApplication>;
|
||||
|
||||
/**
|
||||
* @brief Generate application entry.
|
||||
*/
|
||||
template <class T>
|
||||
std::enable_if_t<std::is_base_of<BaseApplication, T>::value, ApplicationPtr>
|
||||
createApplication(int argc, const char* argv[]) {
|
||||
ApplicationPtr app = std::make_shared<T>();
|
||||
app->setArgumentsInternal(argc, argv);
|
||||
_apps.push_back(app);
|
||||
_currentApp = app;
|
||||
return app;
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Release all generated applications.
|
||||
*/
|
||||
void releseAllApplications();
|
||||
/**
|
||||
* @brief Get the current application, may get empty.
|
||||
*/
|
||||
ApplicationPtr getCurrentApp() const;
|
||||
/**
|
||||
* @brief Get the current application, make sure it is not empty.
|
||||
* Used to get the engine.
|
||||
*/
|
||||
ApplicationPtr getCurrentAppSafe() const;
|
||||
|
||||
private:
|
||||
std::weak_ptr<BaseApplication> _currentApp;
|
||||
ccstd::vector<ApplicationPtr> _apps;
|
||||
};
|
||||
} // namespace cc
|
||||
|
||||
#define CC_APPLICATION_MANAGER() cc::ApplicationManager::getInstance()
|
||||
#define CC_CURRENT_APPLICATION() CC_APPLICATION_MANAGER()->getCurrentApp()
|
||||
#define CC_CURRENT_APPLICATION_SAFE() CC_APPLICATION_MANAGER()->getCurrentAppSafe()
|
||||
#define CC_CURRENT_ENGINE() CC_CURRENT_APPLICATION_SAFE()->getEngine()
|
||||
#define CC_GET_PLATFORM_INTERFACE(intf) CC_CURRENT_ENGINE()->getInterface<intf>()
|
||||
#define CC_GET_SYSTEM_WINDOW(id) CC_GET_PLATFORM_INTERFACE(cc::ISystemWindowManager)->getWindow(id)
|
||||
#define CC_GET_MAIN_SYSTEM_WINDOW() CC_GET_SYSTEM_WINDOW(cc::ISystemWindow::mainWindowId) // Assuming the 1st created window is the main system window for now!
|
||||
|
||||
#define CC_GET_XR_INTERFACE() BasePlatform::getPlatform()->getInterface<IXRInterface>()
|
||||
|
||||
/**
|
||||
* @brief Called at the user-defined main entry
|
||||
*/
|
||||
#define CC_START_APPLICATION(className) \
|
||||
do { \
|
||||
auto app = CC_APPLICATION_MANAGER()->createApplication<className>(argc, argv); \
|
||||
if (app->init()) { \
|
||||
return -1; \
|
||||
} \
|
||||
return app->run(argc, argv); \
|
||||
} while (0)
|
||||
|
||||
#define CC_REGISTER_APPLICATION(className) \
|
||||
int cocos_main(int argc, const char** argv) { \
|
||||
CC_START_APPLICATION(className); \
|
||||
}
|
||||
80
cocos/application/BaseApplication.h
Normal file
80
cocos/application/BaseApplication.h
Normal file
@@ -0,0 +1,80 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2017-2023 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.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <memory>
|
||||
#include "engine/BaseEngine.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
class CC_DLL BaseApplication {
|
||||
public:
|
||||
virtual ~BaseApplication() = default;
|
||||
/**
|
||||
* @brief Application initialization
|
||||
*/
|
||||
virtual int32_t init() = 0;
|
||||
/**
|
||||
* @brief Application main business logic.
|
||||
*/
|
||||
virtual int32_t run(int argc,
|
||||
const char **argv) = 0;
|
||||
/**
|
||||
* @brief Pause the application.
|
||||
*/
|
||||
virtual void pause() = 0;
|
||||
/**
|
||||
* @brief Resume the application.
|
||||
*/
|
||||
virtual void resume() = 0;
|
||||
/**
|
||||
* @brief Restart the application.
|
||||
*/
|
||||
virtual void restart() = 0;
|
||||
/**
|
||||
* @brief Close the application.
|
||||
*/
|
||||
virtual void close() = 0;
|
||||
/**
|
||||
* @brief Get engine.
|
||||
*/
|
||||
virtual BaseEngine::Ptr getEngine() const = 0;
|
||||
|
||||
/**
|
||||
* @brief Get arguments passed to execution file
|
||||
*/
|
||||
virtual const std::vector<std::string> &getArguments() const = 0;
|
||||
|
||||
protected:
|
||||
/**
|
||||
* @brief Set arguments passed to execution file
|
||||
* @note setArgumentsInternal needs to be protected since it should only be used internally.
|
||||
*/
|
||||
virtual void setArgumentsInternal(int argc, const char *argv[]) = 0;
|
||||
|
||||
friend class ApplicationManager;
|
||||
};
|
||||
|
||||
} // namespace cc
|
||||
90
cocos/application/BaseGame.cpp
Normal file
90
cocos/application/BaseGame.cpp
Normal file
@@ -0,0 +1,90 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2017-2023 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 "BaseGame.h"
|
||||
#include <string>
|
||||
#include "ApplicationManager.h"
|
||||
#include "platform/interfaces/modules/ISystemWindowManager.h"
|
||||
#include "renderer/pipeline/GlobalDescriptorSetManager.h"
|
||||
|
||||
#if CC_PLATFORM == CC_PLATFORM_ANDROID
|
||||
#include "platform/android/adpf_manager.h"
|
||||
#endif
|
||||
extern "C" void cc_load_all_plugins(); // NOLINT
|
||||
|
||||
namespace cc {
|
||||
int BaseGame::init() {
|
||||
cc::pipeline::GlobalDSManager::setDescriptorSetLayout();
|
||||
|
||||
cc_load_all_plugins();
|
||||
|
||||
#if (CC_PLATFORM == CC_PLATFORM_ANDROID) && CC_SUPPORT_ADPF
|
||||
ADPFManager::getInstance().Initialize();
|
||||
#endif
|
||||
|
||||
#if CC_PLATFORM == CC_PLATFORM_WINDOWS || CC_PLATFORM == CC_PLATFORM_LINUX || CC_PLATFORM == CC_PLATFORM_QNX || CC_PLATFORM == CC_PLATFORM_MACOS
|
||||
// override default value
|
||||
//_windowInfo.x = _windowInfo.x == -1 ? 0 : _windowInfo.x;
|
||||
//_windowInfo.y = _windowInfo.y == -1 ? 0 : _windowInfo.y;
|
||||
_windowInfo.width = _windowInfo.width == -1 ? 800 : _windowInfo.width;
|
||||
_windowInfo.height = _windowInfo.height == -1 ? 600 : _windowInfo.height;
|
||||
_windowInfo.flags = _windowInfo.flags == -1 ? cc::ISystemWindow::CC_WINDOW_SHOWN |
|
||||
cc::ISystemWindow::CC_WINDOW_RESIZABLE |
|
||||
cc::ISystemWindow::CC_WINDOW_INPUT_FOCUS
|
||||
: _windowInfo.flags;
|
||||
std::call_once(_windowCreateFlag, [&]() {
|
||||
ISystemWindowInfo info;
|
||||
info.title = _windowInfo.title;
|
||||
#if CC_PLATFORM == CC_PLATFORM_WINDOWS
|
||||
info.x = _windowInfo.x == -1 ? 50 : _windowInfo.x; // 50 meams move window a little for now
|
||||
info.y = _windowInfo.y == -1 ? 50 : _windowInfo.y; // same above
|
||||
#else
|
||||
info.x = _windowInfo.x == -1 ? 0 : _windowInfo.x;
|
||||
info.y = _windowInfo.y == -1 ? 0 : _windowInfo.y;
|
||||
#endif
|
||||
info.width = _windowInfo.width;
|
||||
info.height = _windowInfo.height;
|
||||
info.flags = _windowInfo.flags;
|
||||
|
||||
ISystemWindowManager* windowMgr = CC_GET_PLATFORM_INTERFACE(ISystemWindowManager);
|
||||
windowMgr->createWindow(info);
|
||||
});
|
||||
|
||||
#endif
|
||||
|
||||
if (_debuggerInfo.enabled) {
|
||||
setDebugIpAndPort(_debuggerInfo.address, _debuggerInfo.port, _debuggerInfo.pauseOnStart);
|
||||
}
|
||||
|
||||
int ret = cc::CocosApplication::init();
|
||||
if (ret != 0) {
|
||||
return ret;
|
||||
}
|
||||
|
||||
setXXTeaKey(_xxteaKey);
|
||||
runScript("jsb-adapter/web-adapter.js");
|
||||
runScript("main.js");
|
||||
return 0;
|
||||
}
|
||||
} // namespace cc
|
||||
55
cocos/application/BaseGame.h
Normal file
55
cocos/application/BaseGame.h
Normal file
@@ -0,0 +1,55 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2017-2023 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.
|
||||
****************************************************************************/
|
||||
#pragma once
|
||||
#include <string>
|
||||
#include "CocosApplication.h"
|
||||
|
||||
namespace cc {
|
||||
class BaseGame : public CocosApplication {
|
||||
public:
|
||||
struct DebuggerInfo {
|
||||
bool enabled{true};
|
||||
int32_t port{6086};
|
||||
std::string address{"0.0.0.0"};
|
||||
bool pauseOnStart{false};
|
||||
};
|
||||
struct WindowInfo {
|
||||
std::string title;
|
||||
int32_t x{-1};
|
||||
int32_t y{-1};
|
||||
int32_t width{-1};
|
||||
int32_t height{-1};
|
||||
int32_t flags{-1};
|
||||
};
|
||||
|
||||
BaseGame() = default;
|
||||
int init() override;
|
||||
|
||||
protected:
|
||||
std::string _xxteaKey;
|
||||
DebuggerInfo _debuggerInfo;
|
||||
WindowInfo _windowInfo;
|
||||
std::once_flag _windowCreateFlag;
|
||||
};
|
||||
} // namespace cc
|
||||
192
cocos/application/CocosApplication.cpp
Normal file
192
cocos/application/CocosApplication.cpp
Normal file
@@ -0,0 +1,192 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2017-2023 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 "application/CocosApplication.h"
|
||||
|
||||
#include "base/Macros.h"
|
||||
|
||||
#include "ApplicationManager.h"
|
||||
#include "cocos/bindings/event/EventDispatcher.h"
|
||||
#include "cocos/bindings/jswrapper/SeApi.h"
|
||||
#include "cocos/bindings/manual/jsb_classtype.h"
|
||||
#include "cocos/bindings/manual/jsb_global.h"
|
||||
#include "cocos/bindings/manual/jsb_module_register.h"
|
||||
#include "cocos/engine/BaseEngine.h"
|
||||
#include "cocos/platform/interfaces/modules/IScreen.h"
|
||||
#include "cocos/platform/interfaces/modules/ISystemWindowManager.h"
|
||||
|
||||
namespace cc {
|
||||
|
||||
CocosApplication::CocosApplication() {
|
||||
_engine = BaseEngine::createEngine();
|
||||
}
|
||||
|
||||
CocosApplication::~CocosApplication() {
|
||||
unregisterAllEngineEvents();
|
||||
}
|
||||
|
||||
void CocosApplication::unregisterAllEngineEvents() {
|
||||
if (_engine != nullptr) {
|
||||
_engine->off(_engineEvents);
|
||||
}
|
||||
}
|
||||
|
||||
int CocosApplication::init() {
|
||||
if (_engine->init()) {
|
||||
return -1;
|
||||
}
|
||||
unregisterAllEngineEvents();
|
||||
|
||||
_systemWindow = CC_GET_MAIN_SYSTEM_WINDOW();
|
||||
|
||||
_engineEvents = _engine->on<BaseEngine::EngineStatusChange>([this](BaseEngine * /*emitter*/, BaseEngine::EngineStatus status) {
|
||||
switch (status) {
|
||||
case BaseEngine::ON_START:
|
||||
this->onStart();
|
||||
break;
|
||||
case BaseEngine::ON_RESUME:
|
||||
this->onResume();
|
||||
break;
|
||||
case BaseEngine::ON_PAUSE:
|
||||
this->onPause();
|
||||
break;
|
||||
case BaseEngine::ON_CLOSE:
|
||||
this->onClose();
|
||||
break;
|
||||
default:
|
||||
CC_ABORT();
|
||||
}
|
||||
});
|
||||
|
||||
se::ScriptEngine *se = se::ScriptEngine::getInstance();
|
||||
|
||||
jsb_init_file_operation_delegate();
|
||||
|
||||
se->setExceptionCallback(
|
||||
std::bind(&CocosApplication::handleException, this, // NOLINT(modernize-avoid-bind)
|
||||
std::placeholders::_1, std::placeholders::_2, std::placeholders::_3));
|
||||
|
||||
jsb_register_all_modules();
|
||||
#if CC_EDITOR
|
||||
auto isolate = v8::Isolate::GetCurrent();
|
||||
se->start(isolate);
|
||||
#else
|
||||
se->start();
|
||||
#endif
|
||||
|
||||
#if (CC_PLATFORM == CC_PLATFORM_IOS)
|
||||
auto logicSize = _systemWindow->getViewSize();
|
||||
IScreen *screen = _engine->getInterface<IScreen>();
|
||||
float pixelRatio = screen->getDevicePixelRatio();
|
||||
uint32_t windowId = _systemWindow->getWindowId();
|
||||
events::Resize::broadcast(logicSize.width * pixelRatio, logicSize.height * pixelRatio, windowId);
|
||||
#endif
|
||||
return 0;
|
||||
}
|
||||
|
||||
int32_t CocosApplication::run(int argc, const char **argv) {
|
||||
CC_UNUSED_PARAM(argc);
|
||||
CC_UNUSED_PARAM(argv);
|
||||
return _engine->run();
|
||||
}
|
||||
|
||||
void CocosApplication::pause() {
|
||||
_engine->pause();
|
||||
}
|
||||
|
||||
void CocosApplication::resume() {
|
||||
_engine->resume();
|
||||
}
|
||||
|
||||
void CocosApplication::restart() {
|
||||
_engine->restart();
|
||||
}
|
||||
// IMPORTANT!!The method `onClose` is a function to be listen close event, while `close` is a jsb binding method mean to close the whole application.
|
||||
void CocosApplication::close() {
|
||||
_systemWindow->closeWindow();
|
||||
}
|
||||
|
||||
BaseEngine::Ptr CocosApplication::getEngine() const {
|
||||
return _engine;
|
||||
}
|
||||
|
||||
const std::vector<std::string> &CocosApplication::getArguments() const {
|
||||
return _argv;
|
||||
}
|
||||
|
||||
void CocosApplication::setArgumentsInternal(int argc, const char *argv[]) {
|
||||
_argv.clear();
|
||||
_argv.reserve(argc);
|
||||
for (int i = 0; i < argc; ++i) {
|
||||
_argv.emplace_back(argv[i]);
|
||||
}
|
||||
}
|
||||
|
||||
void CocosApplication::onStart() {
|
||||
// TODO(cc): Handling engine start events
|
||||
}
|
||||
|
||||
void CocosApplication::onPause() {
|
||||
// TODO(cc): Handling pause events
|
||||
}
|
||||
|
||||
void CocosApplication::onResume() {
|
||||
// TODO(cc): Handling resume events
|
||||
}
|
||||
|
||||
void CocosApplication::onClose() {
|
||||
_engine->close();
|
||||
}
|
||||
|
||||
void CocosApplication::setDebugIpAndPort(const ccstd::string &serverAddr, uint32_t port, bool isWaitForConnect) {
|
||||
// Enable debugger here
|
||||
jsb_enable_debugger(serverAddr, port, isWaitForConnect);
|
||||
}
|
||||
|
||||
void CocosApplication::runScript(const ccstd::string &filePath) {
|
||||
jsb_run_script(filePath);
|
||||
}
|
||||
|
||||
void CocosApplication::handleException(const char *location, const char *message, const char *stack) {
|
||||
// Send exception information to server like Tencent Bugly.
|
||||
CC_LOG_ERROR("\nUncaught Exception:\n - location : %s\n - msg : %s\n - detail : \n %s\n", location, message, stack);
|
||||
}
|
||||
|
||||
void CocosApplication::setXXTeaKey(const ccstd::string &key) {
|
||||
jsb_set_xxtea_key(key);
|
||||
}
|
||||
#if CC_PLATFORM == CC_PLATFORM_WINDOWS || CC_PLATFORM == CC_PLATFORM_LINUX || CC_PLATFORM == CC_PLATFORM_QNX || CC_PLATFORM == CC_PLATFORM_MACOS
|
||||
void CocosApplication::createWindow(const char *title, int32_t w,
|
||||
int32_t h, int32_t flags) {
|
||||
_systemWindow->createWindow(title, w, h, flags);
|
||||
}
|
||||
|
||||
void CocosApplication::createWindow(const char *title,
|
||||
int32_t x, int32_t y, int32_t w,
|
||||
int32_t h, int32_t flags) {
|
||||
_systemWindow->createWindow(title, x, y, w, h, flags);
|
||||
}
|
||||
#endif
|
||||
|
||||
} // namespace cc
|
||||
151
cocos/application/CocosApplication.h
Normal file
151
cocos/application/CocosApplication.h
Normal file
@@ -0,0 +1,151 @@
|
||||
/****************************************************************************
|
||||
Copyright (c) 2017-2023 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.
|
||||
****************************************************************************/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include "application/BaseApplication.h"
|
||||
#include "cocos/platform/interfaces/modules/ISystemWindow.h"
|
||||
|
||||
namespace cc {
|
||||
class BaseEngine;
|
||||
|
||||
class CC_DLL CocosApplication : public BaseApplication {
|
||||
public:
|
||||
CocosApplication();
|
||||
~CocosApplication() override;
|
||||
|
||||
/**
|
||||
* @brief Application initialization.
|
||||
*/
|
||||
int32_t init() override;
|
||||
/**
|
||||
* @brief Application main business logic.
|
||||
*/
|
||||
int32_t run(int argc, const char **argv) override;
|
||||
/**
|
||||
* @brief Pause the application.
|
||||
*/
|
||||
void pause() override;
|
||||
/**
|
||||
* @brief Resume the application.
|
||||
*/
|
||||
void resume() override;
|
||||
/**
|
||||
* @brief Restart the application.
|
||||
*/
|
||||
void restart() override;
|
||||
/**
|
||||
* @brief Close the application.
|
||||
*/
|
||||
void close() override;
|
||||
/**
|
||||
* @brief Get engine.
|
||||
*/
|
||||
BaseEngine::Ptr getEngine() const override;
|
||||
|
||||
/**
|
||||
* @brief Get arguments passed to execution file
|
||||
*/
|
||||
const std::vector<std::string> &getArguments() const override;
|
||||
|
||||
protected:
|
||||
/**
|
||||
* @brief Set arguments passed to execution file
|
||||
* @note setArgumentsInternal needs to be protected since it should only be used internally.
|
||||
*/
|
||||
void setArgumentsInternal(int argc, const char *argv[]) override;
|
||||
|
||||
public:
|
||||
/**
|
||||
* @brief Processing engine start events.
|
||||
*/
|
||||
virtual void onStart();
|
||||
/**
|
||||
* @brief Processing pause events..
|
||||
*/
|
||||
virtual void onPause();
|
||||
/**
|
||||
* @brief Processing recovery events.
|
||||
*/
|
||||
virtual void onResume();
|
||||
/**
|
||||
* @brief Processing close events.
|
||||
*/
|
||||
virtual void onClose();
|
||||
#if CC_PLATFORM == CC_PLATFORM_WINDOWS || CC_PLATFORM == CC_PLATFORM_LINUX || CC_PLATFORM == CC_PLATFORM_QNX || CC_PLATFORM == CC_PLATFORM_MACOS
|
||||
/**
|
||||
* @brief Create window.
|
||||
* @param title: Window title
|
||||
* @param x: x-axis coordinate
|
||||
* @param y: y-axis coordinate
|
||||
* @param w: Window width
|
||||
* @param h: Window height
|
||||
* @param flags: Window flag
|
||||
*/
|
||||
virtual void createWindow(const char *title,
|
||||
int32_t x, int32_t y, int32_t w,
|
||||
int32_t h, int32_t flags);
|
||||
/**
|
||||
* @brief Create a centered window.
|
||||
* @param title: Window title
|
||||
* @param w: Window width
|
||||
* @param h: Window height
|
||||
* @param flags: Window flag
|
||||
*/
|
||||
virtual void createWindow(const char *title, int32_t w,
|
||||
int32_t h, int32_t flags);
|
||||
#endif
|
||||
/**
|
||||
* @brief Set the debugging server Addr and port
|
||||
* @param serverAddr:Server address.
|
||||
* @param port:Server port.
|
||||
* @param isWaitForConnect:Is Wait for connect.
|
||||
*/
|
||||
virtual void setDebugIpAndPort(const ccstd::string &serverAddr, uint32_t port, bool isWaitForConnect);
|
||||
/**
|
||||
* @brief Run the script file
|
||||
* @param filePath:script path.
|
||||
*/
|
||||
virtual void runScript(const ccstd::string &filePath);
|
||||
/**
|
||||
* @brief Script exception handling
|
||||
* @param location,Exception location
|
||||
* @param message,Exception message
|
||||
* @param stack,Exception stack
|
||||
*/
|
||||
virtual void handleException(const char *location, const char *message, const char *stack);
|
||||
virtual void setXXTeaKey(const ccstd::string &key);
|
||||
|
||||
private:
|
||||
void unregisterAllEngineEvents();
|
||||
|
||||
ISystemWindow *_systemWindow{nullptr};
|
||||
BaseEngine::Ptr _engine{nullptr};
|
||||
|
||||
BaseEngine::EngineStatusChange::EventID _engineEvents;
|
||||
|
||||
std::vector<std::string> _argv;
|
||||
};
|
||||
} // namespace cc
|
||||
Reference in New Issue
Block a user