diff --git a/native/CMakeLists.txt b/native/CMakeLists.txt index 7ecdbdfc4a..3542afacae 100644 --- a/native/CMakeLists.txt +++ b/native/CMakeLists.txt @@ -382,7 +382,7 @@ elseif(IOS) ) endif() -if(WINDOWS OR LINUX) +if(NOT USE_SERVER_MODE AND (WINDOWS OR LINUX)) cocos_source_files( cocos/platform/SDLHelper.h cocos/platform/SDLHelper.cpp @@ -461,19 +461,13 @@ cocos_source_files( cocos_source_files( cocos/platform/interfaces/modules/Device.cpp cocos/platform/interfaces/modules/Device.h - cocos/platform/interfaces/modules/IAccelerometer.cpp cocos/platform/interfaces/modules/IAccelerometer.h - cocos/platform/interfaces/modules/IBattery.cpp cocos/platform/interfaces/modules/IBattery.h - cocos/platform/interfaces/modules/IScreen.cpp cocos/platform/interfaces/modules/IScreen.h - cocos/platform/interfaces/modules/INetwork.cpp cocos/platform/interfaces/modules/INetwork.h cocos/platform/interfaces/modules/ISystem.cpp cocos/platform/interfaces/modules/ISystem.h - cocos/platform/interfaces/modules/ISystemWindow.cpp cocos/platform/interfaces/modules/ISystemWindow.h - cocos/platform/interfaces/modules/IVibrator.cpp cocos/platform/interfaces/modules/IVibrator.h ) @@ -527,8 +521,8 @@ if(USE_SERVER_MODE) cocos/platform/empty/modules/Screen.h cocos/platform/empty/modules/Network.cpp cocos/platform/empty/modules/Network.h - cocos/platform/empty/modules/Vibrate.cpp - cocos/platform/empty/modules/Vibrate.h + cocos/platform/empty/modules/Vibrator.cpp + cocos/platform/empty/modules/Vibrator.h cocos/platform/empty/modules/System.cpp cocos/platform/empty/modules/System.h cocos/platform/empty/modules/SystemWindow.cpp @@ -629,8 +623,8 @@ elseif(LINUX) cocos/platform/linux/modules/Screen.h cocos/platform/linux/modules/Network.cpp cocos/platform/linux/modules/Network.h - cocos/platform/linux/modules/Vibrate.cpp - cocos/platform/linux/modules/Vibrate.h + cocos/platform/linux/modules/Vibrator.cpp + cocos/platform/linux/modules/Vibrator.h cocos/platform/linux/modules/System.cpp cocos/platform/linux/modules/System.h cocos/platform/linux/modules/SystemWindow.cpp @@ -646,8 +640,8 @@ elseif(QNX) cocos/platform/qnx/modules/Screen.h cocos/platform/qnx/modules/Network.cpp cocos/platform/qnx/modules/Network.h - cocos/platform/qnx/modules/Vibrate.cpp - cocos/platform/qnx/modules/Vibrate.h + cocos/platform/qnx/modules/Vibrator.cpp + cocos/platform/qnx/modules/Vibrator.h cocos/platform/qnx/modules/System.cpp cocos/platform/qnx/modules/System.h cocos/platform/qnx/modules/SystemWindow.cpp diff --git a/native/cocos/application/BaseGame.h b/native/cocos/application/BaseGame.h index 1d089f13d6..1a70e8cad3 100644 --- a/native/cocos/application/BaseGame.h +++ b/native/cocos/application/BaseGame.h @@ -36,7 +36,6 @@ public: std::string address{"0.0.0.0"}; bool pauseOnStart{false}; }; - struct WindowInfo { std::string title; int32_t x{-1}; @@ -45,20 +44,26 @@ public: int32_t height{-1}; int32_t flags{-1}; }; + BaseGame() = default; int init() override { +#if CC_PLATFORM == CC_PLATFORM_WINDOWS || CC_PLATFORM == CC_PLATFORM_LINUX || CC_PLATFORM == CC_PLATFORM_QNX || CC_PLATFORM == CC_PLATFORM_MAC_OSX // override default value - _windowInfo.x = _windowInfo.x == -1 ? 0 : _windowInfo.x; - _windowInfo.y = _windowInfo.y == -1 ? 0 : _windowInfo.y; + //_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; - - createWindow(_windowInfo.title.c_str(), - _windowInfo.x, _windowInfo.y, _windowInfo.width, _windowInfo.height, _windowInfo.flags); + : _windowInfo.flags; + if (_windowInfo.x == -1 || _windowInfo.y == -1) { + createWindow(_windowInfo.title.c_str(), _windowInfo.width, _windowInfo.height, _windowInfo.flags); + } else { + createWindow(_windowInfo.title.c_str(), + _windowInfo.x, _windowInfo.y, _windowInfo.width, _windowInfo.height, _windowInfo.flags); + } +#endif if (_debuggerInfo.enabled) { setJsDebugIpAndPort(_debuggerInfo.address, _debuggerInfo.port, _debuggerInfo.pauseOnStart); @@ -81,4 +86,4 @@ protected: DebuggerInfo _debuggerInfo; WindowInfo _windowInfo; }; -} // namespace cc \ No newline at end of file +} // namespace cc diff --git a/native/cocos/application/CocosApplication.cpp b/native/cocos/application/CocosApplication.cpp index e2d8ec0e22..beddda60cb 100644 --- a/native/cocos/application/CocosApplication.cpp +++ b/native/cocos/application/CocosApplication.cpp @@ -133,12 +133,18 @@ void CocosApplication::handleException(const char *location, const char *message void CocosApplication::setXXTeaKey(const std::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_MAC_OSX +void CocosApplication::createWindow(const char* title, int32_t w, + int32_t h, int32_t flags) { + _systemWidow->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) { _systemWidow->createWindow(title, x, y, w, h, flags); } +#endif void CocosApplication::handleAppEvent(const OSEvent &ev) { const AppEvent &appEv = OSEvent::castEvent(ev); diff --git a/native/cocos/application/CocosApplication.h b/native/cocos/application/CocosApplication.h index 2a6ccf8f7b..df0951bfb5 100644 --- a/native/cocos/application/CocosApplication.h +++ b/native/cocos/application/CocosApplication.h @@ -78,6 +78,7 @@ public: * @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_MAC_OSX /** * @brief Create window. * @param title: Window title @@ -90,6 +91,16 @@ public: 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 js debugging server Addr and port * @param serverAddr:Server address. diff --git a/native/cocos/main/main-linux.cpp b/native/cocos/main/main-linux.cpp index f183ca7baf..5a220154f9 100644 --- a/native/cocos/main/main-linux.cpp +++ b/native/cocos/main/main-linux.cpp @@ -25,7 +25,6 @@ #include -#include "SDL2/SDL_main.h" #include "platform/BasePlatform.h" int main(int argc, char** argv) { diff --git a/native/cocos/main/main-qnx.cpp b/native/cocos/main/main-qnx.cpp index f183ca7baf..5a220154f9 100644 --- a/native/cocos/main/main-qnx.cpp +++ b/native/cocos/main/main-qnx.cpp @@ -25,7 +25,6 @@ #include -#include "SDL2/SDL_main.h" #include "platform/BasePlatform.h" int main(int argc, char** argv) { diff --git a/native/cocos/platform/SDLHelper.cpp b/native/cocos/platform/SDLHelper.cpp index 5a9c761ab6..9103df2cb2 100644 --- a/native/cocos/platform/SDLHelper.cpp +++ b/native/cocos/platform/SDLHelper.cpp @@ -158,13 +158,13 @@ SDLHelper::~SDLHelper() { } } -bool SDLHelper::init() { +int SDLHelper::init() { if (SDL_Init(SDL_INIT_VIDEO) < 0) { // Display error message CC_LOG_ERROR("SDL could not initialize! SDL_Error: %s\n", SDL_GetError()); - return false; + return -1; } - return true; + return 0; } void SDLHelper::dispatchWindowEvent(const SDL_WindowEvent &wevent) { WindowEvent ev; @@ -332,12 +332,17 @@ void SDLHelper::pollEvent(bool *quit) { } } +bool SDLHelper::createWindow(const char *title, + int w, int h, int flags) { + return createWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, flags); +} + bool SDLHelper::createWindow(const char *title, int x, int y, int w, int h, int flags) { // Create window int sdlFlags = windowFlagsToSDLWindowFlag(flags); - _handle = SDL_CreateWindow(title, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, sdlFlags); + _handle = SDL_CreateWindow(title, x, y, w, h, sdlFlags); if (_handle == nullptr) { // Display error message CC_LOG_ERROR("Window could not be created! SDL_Error: %s\n", SDL_GetError()); @@ -347,6 +352,10 @@ bool SDLHelper::createWindow(const char *title, return true; } +void SDLHelper::setCursorEnabled(bool value) { + SDL_SetRelativeMouseMode(value ? SDL_FALSE : SDL_TRUE); +} + void SDLHelper::swapWindow() { SDL_GL_SwapWindow(_handle); } diff --git a/native/cocos/platform/SDLHelper.h b/native/cocos/platform/SDLHelper.h index edd260d9ab..926e1eebe5 100644 --- a/native/cocos/platform/SDLHelper.h +++ b/native/cocos/platform/SDLHelper.h @@ -39,20 +39,20 @@ public: SDLHelper(IEventDispatch* delegate); ~SDLHelper(); - bool init(); + int init(); void swapWindow(); - void createWindow(); - int getEvent(OSEvent* ev); + bool createWindow(const char* title, + int w, int h, int flags); bool createWindow(const char* title, int x, int y, int w, int h, int flags); void pollEvent(bool* quit); - uintptr_t getWindowHandler() const; #if (CC_PLATFORM == CC_PLATFORM_LINUX) uintptr_t getDisplay() const; #endif + void setCursorEnabled(bool value); private: void dispatchSDLEvent(const SDL_Event& sdlEvent, bool* quit); diff --git a/native/cocos/platform/UniversalPlatform.cpp b/native/cocos/platform/UniversalPlatform.cpp index 396ed7bfe3..fb7e805320 100644 --- a/native/cocos/platform/UniversalPlatform.cpp +++ b/native/cocos/platform/UniversalPlatform.cpp @@ -27,14 +27,6 @@ #include "platform/interfaces/OSInterface.h" -#include "platform/interfaces/modules/IAccelerometer.h" -#include "platform/interfaces/modules/IBattery.h" -#include "platform/interfaces/modules/INetwork.h" -#include "platform/interfaces/modules/IScreen.h" -#include "platform/interfaces/modules/ISystem.h" -#include "platform/interfaces/modules/ISystemWindow.h" -#include "platform/interfaces/modules/IVibrator.h" - extern int cocos_main(int argc, const char** argv); // NOLINT(readability-identifier-naming) extern void cocos_destory(); // NOLINT(readability-identifier-naming) @@ -74,17 +66,6 @@ void UniversalPlatform::setHandleDefaultEventCallback(HandleEventCallback cb) { _handleDefaultEventCallback = cb; } -int32_t UniversalPlatform::init() { - registerInterface(ISystemWindow::createSystemWindowInterface()); - registerInterface(ISystem::createSystemInterface()); - registerInterface(INetwork::createNetworkInterface()); - registerInterface(IScreen::createScreenInterface()); - registerInterface(IBattery::createBatteryInterface()); - registerInterface(IVibrator::createVibratorInterface()); - registerInterface(IAccelerometer::createAccelerometerInterface()); - return 0; -} - int32_t UniversalPlatform::run(int argc, const char** argv) { if (cocos_main(argc, argv) != 0) { return -1; diff --git a/native/cocos/platform/UniversalPlatform.h b/native/cocos/platform/UniversalPlatform.h index a044baadfe..5bd63e8ba9 100644 --- a/native/cocos/platform/UniversalPlatform.h +++ b/native/cocos/platform/UniversalPlatform.h @@ -31,10 +31,6 @@ namespace cc { class CC_DLL UniversalPlatform : public BasePlatform, public IEventDispatch { public: - /** - * @brief Implement basic platform initialization. - */ - int32_t init() override; /** * @brief Start base platform initialization. */ diff --git a/native/cocos/platform/android/AndroidPlatform.cpp b/native/cocos/platform/android/AndroidPlatform.cpp index 89c7e9130a..3434e2faf5 100644 --- a/native/cocos/platform/android/AndroidPlatform.cpp +++ b/native/cocos/platform/android/AndroidPlatform.cpp @@ -27,12 +27,30 @@ #include "platform/android/AndroidPlatform.h" #include "platform/java/jni/glue/JniNativeGlue.h" +#include "platform/java/modules/Accelerometer.h" +#include "platform/java/modules/Battery.h" +#include "platform/java/modules/Network.h" +#include "platform/java/modules/SystemWindow.h" +#include "platform/java/modules/Vibrator.h" +#include "modules/Screen.h" +#include "modules/System.h" namespace cc { AndroidPlatform::AndroidPlatform() { _jniNativeGlue = JNI_NATIVE_GLUE(); } +int AndroidPlatform::init() { + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + return 0; +} + int AndroidPlatform::getSdkVersion() const { return _jniNativeGlue->getSdkVersion(); } diff --git a/native/cocos/platform/android/AndroidPlatform.h b/native/cocos/platform/android/AndroidPlatform.h index 741d5ccc85..23bae1b1a7 100644 --- a/native/cocos/platform/android/AndroidPlatform.h +++ b/native/cocos/platform/android/AndroidPlatform.h @@ -32,6 +32,7 @@ class JniNativeGlue; class CC_DLL AndroidPlatform : public UniversalPlatform { public: AndroidPlatform(); + int init() override; void pollEvent() override; int32_t run(int argc, const char** argv) override; int getSdkVersion() const override; diff --git a/native/cocos/platform/empty/EmptyPlatform.cpp b/native/cocos/platform/empty/EmptyPlatform.cpp index f963109a93..b391284afe 100644 --- a/native/cocos/platform/empty/EmptyPlatform.cpp +++ b/native/cocos/platform/empty/EmptyPlatform.cpp @@ -24,12 +24,17 @@ ****************************************************************************/ #include "platform/empty/EmptyPlatform.h" - -#include -#include - #include "platform/interfaces/OSInterface.h" -#include "platform/interfaces/modules/ISystemWindow.h" +#include "modules/Accelerometer.h" +#include "modules/Battery.h" +#include "modules/Network.h" +#include "modules/Screen.h" +#include "modules/System.h" +#include "modules/SystemWindow.h" +#include "modules/Vibrator.h" + +#include +#include namespace cc { EmptyPlatform::EmptyPlatform() = default; @@ -37,30 +42,21 @@ EmptyPlatform::~EmptyPlatform() { } int32_t EmptyPlatform::init() { - UniversalPlatform::init(); - - //init + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); return 0; } int32_t EmptyPlatform::loop() { while (!_quit) { - usleep(1000); + std::this_thread::sleep_for(std::chrono::seconds(1)); } return 0; } -void EmptyPlatform::pollEvent() { -} - -bool EmptyPlatform::createWindow(const char *title, - int x, int y, int w, - int h, int flags) { - return true; -} - -uintptr_t EmptyPlatform::getWindowHandler() const { - return -1; -} - } // namespace cc diff --git a/native/cocos/platform/empty/EmptyPlatform.h b/native/cocos/platform/empty/EmptyPlatform.h index cf947a8f0f..ed05654dae 100644 --- a/native/cocos/platform/empty/EmptyPlatform.h +++ b/native/cocos/platform/empty/EmptyPlatform.h @@ -30,8 +30,7 @@ namespace cc { -class CC_DLL EmptyPlatform : public UniversalPlatform, - public SystemWindow::Delegate { +class CC_DLL EmptyPlatform : public UniversalPlatform { public: EmptyPlatform(); /** @@ -45,15 +44,6 @@ public: int32_t loop() override; - // override from SystemWindow::Delegate - bool createWindow(const char* title, - int x, int y, int w, - int h, int flags) override; - uintptr_t getWindowHandler() const override; - -private: - void pollEvent() override; - private: bool _quit{false}; }; diff --git a/native/cocos/platform/empty/modules/CanvasRenderingContext2DDelegate.cpp b/native/cocos/platform/empty/modules/CanvasRenderingContext2DDelegate.cpp index da1953a76d..5d1bb20e89 100644 --- a/native/cocos/platform/empty/modules/CanvasRenderingContext2DDelegate.cpp +++ b/native/cocos/platform/empty/modules/CanvasRenderingContext2DDelegate.cpp @@ -81,6 +81,7 @@ void CanvasRenderingContext2DDelegate::strokeText(const std::string &text, float } CanvasRenderingContext2DDelegate::Size CanvasRenderingContext2DDelegate::measureText(const std::string &text) { + return Size{0, 0}; } void CanvasRenderingContext2DDelegate::updateFont(const std::string &fontName, @@ -144,4 +145,4 @@ void CanvasRenderingContext2DDelegate::rect(float /* x */, float /* h */) { } -} // namespace cc \ No newline at end of file +} // namespace cc diff --git a/native/cocos/platform/empty/modules/SystemWindow.cpp b/native/cocos/platform/empty/modules/SystemWindow.cpp index 4e7a975128..5c1c1dc8ea 100644 --- a/native/cocos/platform/empty/modules/SystemWindow.cpp +++ b/native/cocos/platform/empty/modules/SystemWindow.cpp @@ -45,24 +45,8 @@ SystemWindow::SystemWindow() { SystemWindow::~SystemWindow() { } -bool SystemWindow::createWindow(const char *title, - int x, int y, int w, - int h, int flags) { - // Create window - - EmptyPlatform *platform = dynamic_cast(BasePlatform::getPlatform()); - CCASSERT(platform != nullptr, "Platform pointer can't be null"); - platform->createWindow(title, x, y, w, h, flags); - _width = w; - _height = h; - return true; -} - uintptr_t SystemWindow::getWindowHandler() const { - //return _handle; - EmptyPlatform *platform = dynamic_cast(BasePlatform::getPlatform()); - CCASSERT(platform != nullptr, "Platform pointer can't be null"); - return platform->getWindowHandler(); + return 0; } void SystemWindow::setCursorEnabled(bool value) { diff --git a/native/cocos/platform/empty/modules/SystemWindow.h b/native/cocos/platform/empty/modules/SystemWindow.h index cdf82ac744..36b50e71a5 100644 --- a/native/cocos/platform/empty/modules/SystemWindow.h +++ b/native/cocos/platform/empty/modules/SystemWindow.h @@ -29,9 +29,6 @@ #include "platform/interfaces/modules/ISystemWindow.h" -struct SDL_Window; -struct SDL_WindowEvent; - namespace cc { class CC_DLL SystemWindow : public ISystemWindow { @@ -39,18 +36,6 @@ public: explicit SystemWindow(); ~SystemWindow() override; - class Delegate { - public: - virtual ~Delegate() = default; - virtual bool createWindow(const char* title, - int x, int y, int w, - int h, int flags) = 0; - virtual uintptr_t getWindowHandler() const = 0; - }; - - bool createWindow(const char* title, - int x, int y, int w, - int h, int flags) override; uintptr_t getWindowHandler() const override; Size getViewSize() const override; @@ -65,4 +50,4 @@ private: int _height{0}; }; -} // namespace cc \ No newline at end of file +} // namespace cc diff --git a/native/cocos/platform/empty/modules/Vibrate.cpp b/native/cocos/platform/empty/modules/Vibrator.cpp similarity index 96% rename from native/cocos/platform/empty/modules/Vibrate.cpp rename to native/cocos/platform/empty/modules/Vibrator.cpp index 6372db2a31..9bbbd86938 100644 --- a/native/cocos/platform/empty/modules/Vibrate.cpp +++ b/native/cocos/platform/empty/modules/Vibrator.cpp @@ -23,7 +23,7 @@ THE SOFTWARE. ****************************************************************************/ -#include "platform/empty/modules/Vibrate.h" +#include "platform/empty/modules/Vibrator.h" #include "base/Macros.h" diff --git a/native/cocos/platform/empty/modules/Vibrate.h b/native/cocos/platform/empty/modules/Vibrator.h similarity index 100% rename from native/cocos/platform/empty/modules/Vibrate.h rename to native/cocos/platform/empty/modules/Vibrator.h diff --git a/native/cocos/platform/interfaces/modules/IAccelerometer.cpp b/native/cocos/platform/interfaces/modules/IAccelerometer.cpp deleted file mode 100644 index 96fc1df3b4..0000000000 --- a/native/cocos/platform/interfaces/modules/IAccelerometer.cpp +++ /dev/null @@ -1,49 +0,0 @@ -/**************************************************************************** - Copyright (c) 2021-2022 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 "platform/interfaces/modules/IAccelerometer.h" - -#if (CC_PLATFORM == CC_PLATFORM_WINDOWS) - #include "platform/win32/modules/Accelerometer.h" -#elif (CC_PLATFORM == CC_PLATFORM_ANDROID || CC_PLATFORM == CC_PLATFORM_OHOS) - #include "platform/java/modules/Accelerometer.h" -#elif (CC_PLATFORM == CC_PLATFORM_MAC_OSX) - #include "platform/mac/modules/Accelerometer.h" -#elif (CC_PLATFORM == CC_PLATFORM_MAC_IOS) - #include "platform/ios/modules/Accelerometer.h" -#elif (CC_PLATFORM == CC_PLATFORM_LINUX) - #include "platform/linux/modules/Accelerometer.h" -#elif (CC_PLATFORM == CC_PLATFORM_QNX) - #include "platform/qnx/modules/Accelerometer.h" -#endif - -namespace cc { - -// static -OSInterface::Ptr IAccelerometer::createAccelerometerInterface() { - return std::make_shared(); -} - -} // namespace cc \ No newline at end of file diff --git a/native/cocos/platform/interfaces/modules/IAccelerometer.h b/native/cocos/platform/interfaces/modules/IAccelerometer.h index ef75a6b7e5..1806be0591 100644 --- a/native/cocos/platform/interfaces/modules/IAccelerometer.h +++ b/native/cocos/platform/interfaces/modules/IAccelerometer.h @@ -59,13 +59,7 @@ public: * Gets the motion value of current device. */ virtual const MotionValue &getDeviceMotionValue() = 0; - /** - @brief Create default accelerometer interface. - @return accelerometer interface. - */ - static OSInterface::Ptr createAccelerometerInterface(); -private: }; } // namespace cc diff --git a/native/cocos/platform/interfaces/modules/IBattery.cpp b/native/cocos/platform/interfaces/modules/IBattery.cpp deleted file mode 100644 index 5d36b4f14f..0000000000 --- a/native/cocos/platform/interfaces/modules/IBattery.cpp +++ /dev/null @@ -1,49 +0,0 @@ -/**************************************************************************** - Copyright (c) 2021-2022 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 "platform/interfaces/modules/IBattery.h" - -#if (CC_PLATFORM == CC_PLATFORM_WINDOWS) - #include "platform/win32/modules/Battery.h" -#elif (CC_PLATFORM == CC_PLATFORM_ANDROID || CC_PLATFORM == CC_PLATFORM_OHOS) - #include "platform/java/modules/Battery.h" -#elif (CC_PLATFORM == CC_PLATFORM_MAC_OSX) - #include "platform/mac/modules/Battery.h" -#elif (CC_PLATFORM == CC_PLATFORM_MAC_IOS) - #include "platform/ios/modules/Battery.h" -#elif (CC_PLATFORM == CC_PLATFORM_LINUX) - #include "platform/linux/modules/Battery.h" -#elif (CC_PLATFORM == CC_PLATFORM_QNX) - #include "platform/qnx/modules/Battery.h" -#endif - -namespace cc { - -// static -OSInterface::Ptr IBattery::createBatteryInterface() { - return std::make_shared(); -} - -} // namespace cc \ No newline at end of file diff --git a/native/cocos/platform/interfaces/modules/IBattery.h b/native/cocos/platform/interfaces/modules/IBattery.h index fb737c978a..6c6b685879 100644 --- a/native/cocos/platform/interfaces/modules/IBattery.h +++ b/native/cocos/platform/interfaces/modules/IBattery.h @@ -36,13 +36,7 @@ public: * @return 0.0 ~ 1.0 */ virtual float getBatteryLevel() const = 0; - /** - @brief Create default battery interface. - @return battery interface. - */ - static OSInterface::Ptr createBatteryInterface(); -private: }; } // namespace cc diff --git a/native/cocos/platform/interfaces/modules/INetwork.cpp b/native/cocos/platform/interfaces/modules/INetwork.cpp deleted file mode 100644 index 0d923df096..0000000000 --- a/native/cocos/platform/interfaces/modules/INetwork.cpp +++ /dev/null @@ -1,49 +0,0 @@ -/**************************************************************************** - Copyright (c) 2021-2022 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 "platform/interfaces/modules/INetwork.h" - -#if (CC_PLATFORM == CC_PLATFORM_WINDOWS) - #include "platform/win32/modules/Network.h" -#elif (CC_PLATFORM == CC_PLATFORM_ANDROID || CC_PLATFORM == CC_PLATFORM_OHOS) - #include "platform/java/modules/Network.h" -#elif (CC_PLATFORM == CC_PLATFORM_MAC_OSX) - #include "platform/mac/modules/Network.h" -#elif (CC_PLATFORM == CC_PLATFORM_MAC_IOS) - #include "platform/ios/modules/Network.h" -#elif (CC_PLATFORM == CC_PLATFORM_LINUX) - #include "platform/linux/modules/Network.h" -#elif (CC_PLATFORM == CC_PLATFORM_QNX) - #include "platform/qnx/modules/Network.h" -#endif - -namespace cc { - -// static -OSInterface::Ptr INetwork::createNetworkInterface() { - return std::make_shared(); -} - -} // namespace cc \ No newline at end of file diff --git a/native/cocos/platform/interfaces/modules/INetwork.h b/native/cocos/platform/interfaces/modules/INetwork.h index 7bc578807f..efb026efa9 100644 --- a/native/cocos/platform/interfaces/modules/INetwork.h +++ b/native/cocos/platform/interfaces/modules/INetwork.h @@ -37,13 +37,6 @@ public: WWAN }; virtual NetworkType getNetworkType() const = 0; - /** - @brief Create default network interface. - @return network interface. - */ - static OSInterface::Ptr createNetworkInterface(); - -private: }; } // namespace cc diff --git a/native/cocos/platform/interfaces/modules/IScreen.cpp b/native/cocos/platform/interfaces/modules/IScreen.cpp deleted file mode 100644 index a16b7b0f4b..0000000000 --- a/native/cocos/platform/interfaces/modules/IScreen.cpp +++ /dev/null @@ -1,51 +0,0 @@ -/**************************************************************************** - Copyright (c) 2021-2022 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 "platform/interfaces/modules/IScreen.h" - -#if (CC_PLATFORM == CC_PLATFORM_WINDOWS) - #include "platform/win32/modules/Screen.h" -#elif (CC_PLATFORM == CC_PLATFORM_ANDROID) - #include "platform/android/modules/Screen.h" -#elif (CC_PLATFORM == CC_PLATFORM_OHOS) - #include "platform/ohos/modules/Screen.h" -#elif (CC_PLATFORM == CC_PLATFORM_MAC_OSX) - #include "platform/mac/modules/Screen.h" -#elif (CC_PLATFORM == CC_PLATFORM_MAC_IOS) - #include "platform/ios/modules/Screen.h" -#elif (CC_PLATFORM == CC_PLATFORM_LINUX) - #include "platform/linux/modules/Screen.h" -#elif (CC_PLATFORM == CC_PLATFORM_QNX) - #include "platform/qnx/modules/Screen.h" -#endif - -namespace cc { - -// static -OSInterface::Ptr IScreen::createScreenInterface() { - return std::make_shared(); -} - -} // namespace cc \ No newline at end of file diff --git a/native/cocos/platform/interfaces/modules/IScreen.h b/native/cocos/platform/interfaces/modules/IScreen.h index 10d1a10ccf..70e4489aa5 100644 --- a/native/cocos/platform/interfaces/modules/IScreen.h +++ b/native/cocos/platform/interfaces/modules/IScreen.h @@ -62,12 +62,6 @@ public: virtual void setKeepScreenOn(bool keepScreenOn) = 0; virtual Vec4 getSafeAreaEdge() const = 0; - /** - @brief Create default screen interface. - @return screen interface. - */ - static OSInterface::Ptr createScreenInterface(); -private: }; } // namespace cc diff --git a/native/cocos/platform/interfaces/modules/ISystem.cpp b/native/cocos/platform/interfaces/modules/ISystem.cpp index f9cd42e727..5c59c64810 100644 --- a/native/cocos/platform/interfaces/modules/ISystem.cpp +++ b/native/cocos/platform/interfaces/modules/ISystem.cpp @@ -25,29 +25,9 @@ #include "platform/interfaces/modules/ISystem.h" -#if (CC_PLATFORM == CC_PLATFORM_WINDOWS) - #include "platform/win32/modules/System.h" -#elif (CC_PLATFORM == CC_PLATFORM_ANDROID) - #include "platform/android/modules/System.h" -#elif (CC_PLATFORM == CC_PLATFORM_OHOS) - #include "platform/ohos/modules/System.h" -#elif (CC_PLATFORM == CC_PLATFORM_MAC_OSX) - #include "platform/mac/modules/System.h" -#elif (CC_PLATFORM == CC_PLATFORM_MAC_IOS) - #include "platform/ios/modules/System.h" -#elif (CC_PLATFORM == CC_PLATFORM_LINUX) - #include "platform/linux/modules/System.h" -#elif (CC_PLATFORM == CC_PLATFORM_QNX) - #include "platform/qnx/modules/System.h" -#endif - namespace cc { ISystem::~ISystem() = default; -ISystem::Ptr ISystem::createSystemInterface() { - return std::make_shared(); -} - std::string ISystem::getCurrentLanguageToString() { LanguageType language = getCurrentLanguage(); std::string languageStr = ""; // NOLINT @@ -116,4 +96,4 @@ std::string ISystem::getCurrentLanguageToString() { return languageStr; } -} // namespace cc \ No newline at end of file +} // namespace cc diff --git a/native/cocos/platform/interfaces/modules/ISystem.h b/native/cocos/platform/interfaces/modules/ISystem.h index 7a2d5982e7..b64e510896 100644 --- a/native/cocos/platform/interfaces/modules/ISystem.h +++ b/native/cocos/platform/interfaces/modules/ISystem.h @@ -84,13 +84,7 @@ public: @return True if the resource located by the URL was successfully opened; otherwise false. */ virtual bool openURL(const std::string& url) = 0; - /** - @brief Create default sytem interface. - @return sytem interface. - */ - static OSInterface::Ptr createSystemInterface(); -private: }; } // namespace cc diff --git a/native/cocos/platform/interfaces/modules/ISystemWindow.cpp b/native/cocos/platform/interfaces/modules/ISystemWindow.cpp deleted file mode 100644 index 339bfe89fb..0000000000 --- a/native/cocos/platform/interfaces/modules/ISystemWindow.cpp +++ /dev/null @@ -1,48 +0,0 @@ -/**************************************************************************** - Copyright (c) 2021-2022 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 "platform/interfaces/modules/ISystemWindow.h" -#if (CC_PLATFORM == CC_PLATFORM_WINDOWS) - #include "platform/win32/modules/SystemWindow.h" -#elif (CC_PLATFORM == CC_PLATFORM_ANDROID || CC_PLATFORM == CC_PLATFORM_OHOS) - #include "platform/java/modules/SystemWindow.h" -#elif (CC_PLATFORM == CC_PLATFORM_MAC_OSX) - #include "platform/mac/modules/SystemWindow.h" -#elif (CC_PLATFORM == CC_PLATFORM_MAC_IOS) - #include "platform/ios/modules/SystemWindow.h" -#elif (CC_PLATFORM == CC_PLATFORM_LINUX) - #include "platform/linux/modules/SystemWindow.h" -#elif (CC_PLATFORM == CC_PLATFORM_QNX) - #include "platform/qnx/modules/SystemWindow.h" -#endif - -namespace cc { - -// static -OSInterface::Ptr ISystemWindow::createSystemWindowInterface() { - return std::make_shared(); -} - -} // namespace cc diff --git a/native/cocos/platform/interfaces/modules/ISystemWindow.h b/native/cocos/platform/interfaces/modules/ISystemWindow.h index e26d006ca4..40750ae9e2 100644 --- a/native/cocos/platform/interfaces/modules/ISystemWindow.h +++ b/native/cocos/platform/interfaces/modules/ISystemWindow.h @@ -71,9 +71,22 @@ public: *@param h: Window height *@param flags: Window flag */ + virtual bool createWindow(const char* title, + int x, int y, int w, + int h, int flags) { + return true; + } + /** + * @brief Create a centered window. + *@param title: Window title + *@param w: Window width + *@param h: Window height + *@param flags: Window flag + */ virtual bool createWindow(const char* title, - int x, int y, int w, - int h, int flags) = 0; + int w, int h, int flags) { + return true; + } virtual uintptr_t getWindowHandler() const = 0; virtual Size getViewSize() const = 0; /** @@ -82,13 +95,6 @@ public: virtual void setCursorEnabled(bool value) = 0; virtual void copyTextToClipboard(const std::string& text) = 0; - /** - @brief Create default sytem window interface. - @return sytem window interface. - */ - static OSInterface::Ptr createSystemWindowInterface(); - -private: }; } // namespace cc diff --git a/native/cocos/platform/interfaces/modules/IVibrator.cpp b/native/cocos/platform/interfaces/modules/IVibrator.cpp deleted file mode 100644 index 95be4f227d..0000000000 --- a/native/cocos/platform/interfaces/modules/IVibrator.cpp +++ /dev/null @@ -1,49 +0,0 @@ -/**************************************************************************** - Copyright (c) 2021-2022 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 "platform/interfaces/modules/IVibrator.h" - -#if (CC_PLATFORM == CC_PLATFORM_WINDOWS) - #include "platform/win32/modules/Vibrator.h" -#elif (CC_PLATFORM == CC_PLATFORM_ANDROID || CC_PLATFORM == CC_PLATFORM_OHOS) - #include "platform/java/modules/Vibrator.h" -#elif (CC_PLATFORM == CC_PLATFORM_MAC_OSX) - #include "platform/mac/modules/Vibrator.h" -#elif (CC_PLATFORM == CC_PLATFORM_MAC_IOS) - #include "platform/ios/modules/Vibrator.h" -#elif (CC_PLATFORM == CC_PLATFORM_LINUX) - #include "platform/linux/modules/Vibrate.h" -#elif (CC_PLATFORM == CC_PLATFORM_QNX) - #include "platform/qnx/modules/Vibrate.h" -#endif - -namespace cc { - -// static -OSInterface::Ptr IVibrator::createVibratorInterface() { - return std::make_shared(); -} - -} // namespace cc \ No newline at end of file diff --git a/native/cocos/platform/interfaces/modules/IVibrator.h b/native/cocos/platform/interfaces/modules/IVibrator.h index 66f91880ed..d249e600f7 100644 --- a/native/cocos/platform/interfaces/modules/IVibrator.h +++ b/native/cocos/platform/interfaces/modules/IVibrator.h @@ -40,12 +40,6 @@ public: * @param duration The duration in seconds. */ virtual void vibrate(float duration) = 0; - - /** - * @brief Create default Vibrator interface. - * @return Vibrator interface. - */ - static OSInterface::Ptr createVibratorInterface(); }; } // namespace cc diff --git a/native/cocos/platform/ios/IOSPlatform.mm b/native/cocos/platform/ios/IOSPlatform.mm index 03df14ecd4..0c62caf643 100644 --- a/native/cocos/platform/ios/IOSPlatform.mm +++ b/native/cocos/platform/ios/IOSPlatform.mm @@ -28,7 +28,15 @@ #include "platform/interfaces/modules/ISystemWindow.h" #import + #import "platform/ios/AppDelegate.h" +#include "modules/Accelerometer.h" +#include "modules/Battery.h" +#include "modules/Network.h" +#include "modules/Screen.h" +#include "modules/System.h" +#include "modules/SystemWindow.h" +#include "modules/Vibrator.h" extern int cocos_main(int argc, const char** argv); @@ -92,7 +100,14 @@ IOSPlatform::~IOSPlatform() = default; int32_t IOSPlatform::init() { _timer = [[MyTimer alloc] initWithApp:this fps:60]; - return UniversalPlatform::init(); + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + return 0; } int32_t IOSPlatform::loop() { diff --git a/native/cocos/platform/ios/modules/SystemWindow.h b/native/cocos/platform/ios/modules/SystemWindow.h index 0b5b349fb7..a523fe99f4 100644 --- a/native/cocos/platform/ios/modules/SystemWindow.h +++ b/native/cocos/platform/ios/modules/SystemWindow.h @@ -36,9 +36,6 @@ public: SystemWindow() = default; ~SystemWindow() override; - bool createWindow(const char* title, - int x, int y, int w, - int h, int flags) override; uintptr_t getWindowHandler() const override; Size getViewSize() const override; diff --git a/native/cocos/platform/ios/modules/SystemWindow.mm b/native/cocos/platform/ios/modules/SystemWindow.mm index 34f78a66cc..fced4eea12 100644 --- a/native/cocos/platform/ios/modules/SystemWindow.mm +++ b/native/cocos/platform/ios/modules/SystemWindow.mm @@ -35,12 +35,6 @@ namespace cc { SystemWindow::~SystemWindow() = default; -bool SystemWindow::createWindow(const char* title, - int x, int y, int w, - int h, int flags) { - _width = w; - _height = h; -} void SystemWindow::setCursorEnabled(bool value) { } diff --git a/native/cocos/platform/java/modules/SystemWindow.cpp b/native/cocos/platform/java/modules/SystemWindow.cpp index aad61b4c7a..a30c478e00 100644 --- a/native/cocos/platform/java/modules/SystemWindow.cpp +++ b/native/cocos/platform/java/modules/SystemWindow.cpp @@ -41,18 +41,6 @@ namespace { namespace cc { -bool SystemWindow::createWindow(const char *title, - int x, int y, int w, - int h, int flags) { - CC_UNUSED_PARAM(title); - CC_UNUSED_PARAM(x); - CC_UNUSED_PARAM(y); - CC_UNUSED_PARAM(w); - CC_UNUSED_PARAM(h); - CC_UNUSED_PARAM(flags); - return true; -} - void SystemWindow::setCursorEnabled(bool value) { } @@ -70,4 +58,4 @@ SystemWindow::Size SystemWindow::getViewSize() const { static_cast(JNI_NATIVE_GLUE()->getHeight())}; } -} // namespace cc \ No newline at end of file +} // namespace cc diff --git a/native/cocos/platform/java/modules/SystemWindow.h b/native/cocos/platform/java/modules/SystemWindow.h index 1c39bca8e3..bdc80c2e5a 100644 --- a/native/cocos/platform/java/modules/SystemWindow.h +++ b/native/cocos/platform/java/modules/SystemWindow.h @@ -33,9 +33,6 @@ namespace cc { class SystemWindow : public ISystemWindow { public: - bool createWindow(const char* title, - int x, int y, int w, - int h, int flags) override; /** @brief enable/disable(lock) the cursor, default is enabled */ @@ -45,4 +42,4 @@ public: Size getViewSize() const override; }; -} // namespace cc \ No newline at end of file +} // namespace cc diff --git a/native/cocos/platform/linux/LinuxPlatform.cpp b/native/cocos/platform/linux/LinuxPlatform.cpp index a5250f18a2..246b031cb9 100644 --- a/native/cocos/platform/linux/LinuxPlatform.cpp +++ b/native/cocos/platform/linux/LinuxPlatform.cpp @@ -28,24 +28,37 @@ #include #include -#include "platform/interfaces/OSInterface.h" -#include "platform/interfaces/modules/ISystemWindow.h" #include "platform/SDLHelper.h" +#include "modules/Accelerometer.h" +#include "modules/Battery.h" +#include "modules/Network.h" +#include "modules/Screen.h" +#include "modules/System.h" +#include "modules/SystemWindow.h" +#include "modules/Vibrator.h" + namespace { } // namespace namespace cc { LinuxPlatform::LinuxPlatform() { - _sdl = std::make_unique(this); } + LinuxPlatform::~LinuxPlatform() { } int32_t LinuxPlatform::init() { - UniversalPlatform::init(); - return _sdl->init() ? 0 : -1; + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + _window = std::make_shared(this); + registerInterface(_window); + registerInterface(std::make_shared()); + return _window->init(); } static long getCurrentMillSecond() { @@ -66,12 +79,12 @@ int32_t LinuxPlatform::loop() { while (!_quit) { curTime = getCurrentMillSecond(); desiredInterval = static_cast(1000.0 / getFps()); - pollEvent(); + _window->pollEvent(&_quit); actualInterval = curTime - lastTime; if (actualInterval >= desiredInterval) { lastTime = getCurrentMillSecond(); runTask(); - _sdl->swapWindow(); + _window->swapWindow(); } else { usleep((desiredInterval - curTime + lastTime) * 1000); } @@ -81,22 +94,4 @@ int32_t LinuxPlatform::loop() { return 0; } -void LinuxPlatform::pollEvent() { - _sdl->pollEvent(&_quit); -} - -bool LinuxPlatform::createWindow(const char *title, - int x, int y, int w, - int h, int flags) { - return _sdl->createWindow(title, x, y, w, h, flags); -} - -uintptr_t LinuxPlatform::getWindowHandler() const { - return _sdl->getWindowHandler(); -} - -uintptr_t LinuxPlatform::getDisplay() const { - return _sdl->getDisplay(); -} - } // namespace cc diff --git a/native/cocos/platform/linux/LinuxPlatform.h b/native/cocos/platform/linux/LinuxPlatform.h index a550f8154d..b418a7b78e 100644 --- a/native/cocos/platform/linux/LinuxPlatform.h +++ b/native/cocos/platform/linux/LinuxPlatform.h @@ -26,12 +26,10 @@ #pragma once #include "platform/UniversalPlatform.h" -#include "platform/linux/modules/SystemWindow.h" namespace cc { -class SDLHelper; -class CC_DLL LinuxPlatform : public UniversalPlatform, - public SystemWindow::Delegate { +class SystemWindow; +class CC_DLL LinuxPlatform : public UniversalPlatform { public: LinuxPlatform(); /** @@ -45,18 +43,9 @@ public: int32_t loop() override; - // override from SystemWindow::Delegate - bool createWindow(const char* title, - int x, int y, int w, - int h, int flags) override; - uintptr_t getWindowHandler() const override; - uintptr_t getDisplay() const; - private: - void pollEvent() override; - bool _inited{false}; - bool _quit{false}; - std::unique_ptr _sdl; + bool _quit{false}; + std::shared_ptr _window{nullptr}; }; } // namespace cc diff --git a/native/cocos/platform/linux/modules/CanvasRenderingContext2DDelegate.cpp b/native/cocos/platform/linux/modules/CanvasRenderingContext2DDelegate.cpp index 62b0e4807e..8b230552ef 100644 --- a/native/cocos/platform/linux/modules/CanvasRenderingContext2DDelegate.cpp +++ b/native/cocos/platform/linux/modules/CanvasRenderingContext2DDelegate.cpp @@ -25,6 +25,7 @@ #include "platform/linux/modules/CanvasRenderingContext2DDelegate.h" #include "platform/linux/LinuxPlatform.h" +#include "platform/linux/modules/SystemWindow.h" namespace { #define RGB(r, g, b) (int)((int)r | (((int)g) << 8) | (((int)b) << 16)) @@ -37,10 +38,10 @@ namespace cc { static const char gdefaultFontName[] = "lucidasans-24"; CanvasRenderingContext2DDelegate::CanvasRenderingContext2DDelegate() { - LinuxPlatform *platform = dynamic_cast(BasePlatform::getPlatform()); - CCASSERT(platform != nullptr, "Platform pointer can't be null"); - _dis = reinterpret_cast(platform->getDisplay()); - _win = reinterpret_cast(platform->getWindowHandler()); + SystemWindow* window = BasePlatform::getPlatform()->getInterface(); + CCASSERT(window != nullptr, "System window is not registered"); + _dis = reinterpret_cast(window->getDisplay()); + _win = reinterpret_cast(window->getWindowHandler()); } CanvasRenderingContext2DDelegate::~CanvasRenderingContext2DDelegate() { diff --git a/native/cocos/platform/linux/modules/SystemWindow.cpp b/native/cocos/platform/linux/modules/SystemWindow.cpp index 3f367d30e5..35fa280ec5 100644 --- a/native/cocos/platform/linux/modules/SystemWindow.cpp +++ b/native/cocos/platform/linux/modules/SystemWindow.cpp @@ -35,41 +35,57 @@ #include "SDL2/SDL_syswm.h" #include "bindings/event/EventDispatcher.h" #include "platform/IEventDispatch.h" -#include "platform/linux/LinuxPlatform.h" - -namespace { - -} // namespace +#include "platform/SDLHelper.h" namespace cc { -SystemWindow::SystemWindow() { +SystemWindow::SystemWindow(IEventDispatch *delegate) +: _sdl(std::make_unique(delegate)) { + } SystemWindow::~SystemWindow() { } +int SystemWindow::init() { + return _sdl->init(); +} + +void SystemWindow::pollEvent(bool *quit) { + return _sdl->pollEvent(quit); +} + +void SystemWindow::swapWindow() { + _sdl->swapWindow(); +} + +bool SystemWindow::createWindow(const char *title, + int w, int h, int flags) { + _sdl->createWindow(title, w, h, flags); + _width = w; + _height = h; + return true; +} + bool SystemWindow::createWindow(const char *title, int x, int y, int w, int h, int flags) { // Create window - - LinuxPlatform *platform = dynamic_cast(BasePlatform::getPlatform()); - CCASSERT(platform != nullptr, "Platform pointer can't be null"); - platform->createWindow(title, x, y, w, h, flags); + _sdl->createWindow(title, x, y, w, h, flags); _width = w; _height = h; return true; } uintptr_t SystemWindow::getWindowHandler() const { - //return _handle; - LinuxPlatform *platform = dynamic_cast(BasePlatform::getPlatform()); - CCASSERT(platform != nullptr, "Platform pointer can't be null"); - return platform->getWindowHandler(); + return _sdl->getWindowHandler(); +} + +uintptr_t SystemWindow::getDisplay() const { + return _sdl->getDisplay(); } void SystemWindow::setCursorEnabled(bool value) { - SDL_SetRelativeMouseMode(value ? SDL_FALSE : SDL_TRUE); + _sdl->setCursorEnabled(value); } void SystemWindow::copyTextToClipboard(const std::string &text) { diff --git a/native/cocos/platform/linux/modules/SystemWindow.h b/native/cocos/platform/linux/modules/SystemWindow.h index 1ff468a243..05cbcd592b 100644 --- a/native/cocos/platform/linux/modules/SystemWindow.h +++ b/native/cocos/platform/linux/modules/SystemWindow.h @@ -29,30 +29,24 @@ #include "platform/interfaces/modules/ISystemWindow.h" -struct SDL_Window; -struct SDL_WindowEvent; - namespace cc { - +class SDLHelper; class CC_DLL SystemWindow : public ISystemWindow { public: - explicit SystemWindow(); + explicit SystemWindow(IEventDispatch* delegate); ~SystemWindow() override; - class Delegate { - public: - virtual ~Delegate() = default; - virtual bool createWindow(const char* title, - int x, int y, int w, - int h, int flags) = 0; - virtual uintptr_t getWindowHandler() const = 0; - }; + int init(); + void swapWindow(); + void pollEvent(bool* quit); + bool createWindow(const char* title, + int w, int h, int flags) override; bool createWindow(const char* title, int x, int y, int w, int h, int flags) override; uintptr_t getWindowHandler() const override; - + uintptr_t getDisplay() const; Size getViewSize() const override; /* @brief enable/disable(lock) the cursor, default is enabled @@ -63,6 +57,7 @@ public: private: int _width{0}; int _height{0}; + std::unique_ptr _sdl; }; } // namespace cc \ No newline at end of file diff --git a/native/cocos/platform/linux/modules/Vibrate.cpp b/native/cocos/platform/linux/modules/Vibrator.cpp similarity index 96% rename from native/cocos/platform/linux/modules/Vibrate.cpp rename to native/cocos/platform/linux/modules/Vibrator.cpp index 06067d581b..cdae9043c6 100644 --- a/native/cocos/platform/linux/modules/Vibrate.cpp +++ b/native/cocos/platform/linux/modules/Vibrator.cpp @@ -23,7 +23,7 @@ THE SOFTWARE. ****************************************************************************/ -#include "platform/linux/modules/Vibrate.h" +#include "platform/linux/modules/Vibrator.h" #include "base/Macros.h" diff --git a/native/cocos/platform/linux/modules/Vibrate.h b/native/cocos/platform/linux/modules/Vibrator.h similarity index 100% rename from native/cocos/platform/linux/modules/Vibrate.h rename to native/cocos/platform/linux/modules/Vibrator.h diff --git a/native/cocos/platform/mac/AppDelegate.mm b/native/cocos/platform/mac/AppDelegate.mm index b4a973c198..450d84558c 100644 --- a/native/cocos/platform/mac/AppDelegate.mm +++ b/native/cocos/platform/mac/AppDelegate.mm @@ -40,6 +40,17 @@ @implementation AppDelegate +- (void)createWindowPosCentered:(NSString*)title width:(int)w height:(int)h { + CGRect screenFrame = CGDisplayBounds(kCGDirectMainDisplay); + int x = 0; + int y = 0; + if(w < screenFrame.size.width) + x = screenFrame.origin.x + (screenFrame.size.width - w)/2; + if(h < screenFrame.size.height) + y = screenFrame.origin.y + (screenFrame.size.height - h)/2; + [self createWindow :title xPos:x yPos:y width:w height:h]; +} + - (void)createWindow:(NSString*)title xPos:(int)x yPos:(int)y width:(int)w height:(int)h { _window.title = title; NSRect rect = NSMakeRect(x, y, w, h); diff --git a/native/cocos/platform/mac/MacPlatform.mm b/native/cocos/platform/mac/MacPlatform.mm index 68a2677b92..586208b7c1 100644 --- a/native/cocos/platform/mac/MacPlatform.mm +++ b/native/cocos/platform/mac/MacPlatform.mm @@ -25,10 +25,18 @@ #include "platform/mac/MacPlatform.h" #include "platform/interfaces/OSInterface.h" -#include "platform/interfaces/modules/ISystemWindow.h" #include "platform/mac/AppDelegate.h" +#include "modules/Accelerometer.h" +#include "modules/Battery.h" +#include "modules/Network.h" +#include "modules/Screen.h" +#include "modules/System.h" +#include "modules/SystemWindow.h" +#include "modules/Vibrator.h" + #import + extern int cocos_main(int argc, const char** argv); @interface MyTimer : NSObject { @@ -99,7 +107,14 @@ MacPlatform::~MacPlatform() { int32_t MacPlatform::init() { _timer = [[MyTimer alloc] initWithApp:this fps:60]; - return UniversalPlatform::init(); + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + return 0; } int32_t MacPlatform::loop(void) { diff --git a/native/cocos/platform/mac/modules/SystemWindow.h b/native/cocos/platform/mac/modules/SystemWindow.h index 883237ed9c..07d6516a3b 100644 --- a/native/cocos/platform/mac/modules/SystemWindow.h +++ b/native/cocos/platform/mac/modules/SystemWindow.h @@ -39,6 +39,9 @@ public: bool createWindow(const char* title, int x, int y, int w, int h, int flags) override; + bool createWindow(const char* title, + int w,int h, int flags) override; + uintptr_t getWindowHandler() const override; Size getViewSize() const override; diff --git a/native/cocos/platform/mac/modules/SystemWindow.mm b/native/cocos/platform/mac/modules/SystemWindow.mm index bfee8394f7..6d2751b781 100644 --- a/native/cocos/platform/mac/modules/SystemWindow.mm +++ b/native/cocos/platform/mac/modules/SystemWindow.mm @@ -38,6 +38,16 @@ SystemWindow::SystemWindow() = default; SystemWindow::~SystemWindow() = default; +bool SystemWindow::createWindow(const char* title, + int w, int h, int flags) { + _width = w; + _height = h; + AppDelegate *delegate = [[NSApplication sharedApplication] delegate]; + NSString * aString = [NSString stringWithUTF8String:title]; + [delegate createWindowPosCentered:aString width:w height:h]; + return true; +} + bool SystemWindow::createWindow(const char *title, int x, int y, int w, int h, int flags) { @@ -50,6 +60,7 @@ bool SystemWindow::createWindow(const char *title, AppDelegate *delegate = [[NSApplication sharedApplication] delegate]; NSString * aString = [NSString stringWithUTF8String:title]; [delegate createWindow:aString xPos:x yPos:y width:w height:h]; + return true; } void SystemWindow::setCursorEnabled(bool value) { diff --git a/native/cocos/platform/ohos/OhosPlatform.cpp b/native/cocos/platform/ohos/OhosPlatform.cpp index 711f670c54..7d5a633a49 100644 --- a/native/cocos/platform/ohos/OhosPlatform.cpp +++ b/native/cocos/platform/ohos/OhosPlatform.cpp @@ -25,16 +25,32 @@ #include -#include "platform/UniversalPlatform.h" -#include "platform/java/jni/glue/JniNativeGlue.h" -#include "platform/java/modules/SystemWindow.h" #include "platform/ohos/OhosPlatform.h" +#include "platform/java/jni/glue/JniNativeGlue.h" +#include "platform/java/modules/Accelerometer.h" +#include "platform/java/modules/Battery.h" +#include "platform/java/modules/Network.h" +#include "platform/java/modules/SystemWindow.h" +#include "platform/java/modules/Vibrator.h" +#include "modules/Screen.h" +#include "modules/System.h" namespace cc { OhosPlatform::OhosPlatform() { _jniNativeGlue = JNI_NATIVE_GLUE(); } +int OhosPlatform::init() { + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + return 0; +} + int OhosPlatform::getSdkVersion() const { return _jniNativeGlue->getSdkVersion(); } @@ -78,4 +94,4 @@ void OhosPlatform::pollEvent() { _jniNativeGlue->flushTasksOnGameThread(); } -}; // namespace cc \ No newline at end of file +}; // namespace cc diff --git a/native/cocos/platform/ohos/OhosPlatform.h b/native/cocos/platform/ohos/OhosPlatform.h index bd6d9b7b49..ec3e2e2738 100644 --- a/native/cocos/platform/ohos/OhosPlatform.h +++ b/native/cocos/platform/ohos/OhosPlatform.h @@ -29,9 +29,10 @@ namespace cc { class JniNativeGlue; -class OhosPlatform : public UniversalPlatform { +class CC_DLL OhosPlatform : public UniversalPlatform { public: OhosPlatform(); + int init() override; void pollEvent() override; int32_t run(int argc, const char** argv) override; int getSdkVersion() const override; diff --git a/native/cocos/platform/qnx/QNXPlatform.cpp b/native/cocos/platform/qnx/QNXPlatform.cpp index 0f55efbda0..8f30b3b2dc 100644 --- a/native/cocos/platform/qnx/QNXPlatform.cpp +++ b/native/cocos/platform/qnx/QNXPlatform.cpp @@ -29,7 +29,13 @@ #include #include "platform/interfaces/OSInterface.h" -#include "platform/interfaces/modules/ISystemWindow.h" +#include "modules/Accelerometer.h" +#include "modules/Battery.h" +#include "modules/Network.h" +#include "modules/Screen.h" +#include "modules/System.h" +#include "modules/SystemWindow.h" +#include "modules/Vibrator.h" namespace { @@ -39,18 +45,19 @@ namespace cc { QnxPlatform::QnxPlatform() { // _sdl = std::make_unique(this); } + QnxPlatform::~QnxPlatform() { - if (_screenWin) { - screen_destroy_window(_screenWin); - } - if (_screenCtx) { - screen_destroy_context(_screenCtx); - } } int32_t QnxPlatform::init() { - UniversalPlatform::init(); - // return _sdl->init() ? 0 : -1; + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + _window = std::make_shared(); + registerInterface(_window); + registerInterface(std::make_shared()); return 0; } @@ -71,7 +78,7 @@ int32_t QnxPlatform::loop() { onResume(); while (!_quit) { curTime = getCurrentMillSecond(); - desiredInterval = static_cast(1000.0 / getFps()); + desiredInterval = static_cast(1000.0 / getFps()); pollEvent(); actualInterval = curTime - lastTime; if (actualInterval >= desiredInterval) { @@ -92,50 +99,4 @@ void QnxPlatform::pollEvent() { //_sdl->pollEvent(&_quit); } -bool QnxPlatform::createWindow(const char *title, - int x, int y, int w, - int h, int flags) { - if (_inited) { - return true; - } - int rc; - - //Create the screen context - rc = screen_create_context(&_screenCtx, SCREEN_APPLICATION_CONTEXT); - if (rc) { - perror("screen_create_window"); - return EXIT_FAILURE; - } - - //Create the screen window that will be render onto - rc = screen_create_window(&_screenWin, _screenCtx); - if (rc) { - perror("screen_create_window"); - return EXIT_FAILURE; - } - - screen_set_window_property_iv(_screenWin, SCREEN_PROPERTY_FORMAT, (const int[]){SCREEN_FORMAT_RGBX8888}); - screen_set_window_property_iv(_screenWin, SCREEN_PROPERTY_USAGE, (const int[]){SCREEN_USAGE_OPENGL_ES2}); - - int size[2] = {w, h}; /* size of the window on screen */ - rc = screen_set_window_property_iv(_screenWin, SCREEN_PROPERTY_SIZE, size); - - int dpi = 0; - screen_get_window_property_iv(_screenWin, SCREEN_PROPERTY_DPI, &dpi); - fprintf(stdout, "[glError] %d\n", dpi); - - rc = screen_create_window_buffers(_screenWin, 2); - if (rc) { - perror("screen_create_window_buffers"); - return EXIT_FAILURE; - } - // return _sdl->createWindow(title, x, y, w, h, flags); - return true; -} - -uintptr_t QnxPlatform::getWindowHandler() const { - // return _sdl->getWindowHandler(); - return reinterpret_cast(_screenWin); -} - } // namespace cc diff --git a/native/cocos/platform/qnx/QNXPlatform.h b/native/cocos/platform/qnx/QNXPlatform.h index 9bb7699e38..d9f70c1079 100644 --- a/native/cocos/platform/qnx/QNXPlatform.h +++ b/native/cocos/platform/qnx/QNXPlatform.h @@ -25,14 +25,11 @@ #pragma once -#include #include "platform/UniversalPlatform.h" -#include "platform/qnx/modules/SystemWindow.h" namespace cc { - -class CC_DLL QnxPlatform : public UniversalPlatform, - public SystemWindow::Delegate { +class SystemWindow; +class CC_DLL QnxPlatform : public UniversalPlatform { public: QnxPlatform(); /** @@ -46,22 +43,10 @@ public: int32_t loop() override; - // override from SystemWindow::Delegate - bool createWindow(const char* title, - int x, int y, int w, - int h, int flags) override; - uintptr_t getWindowHandler() const override; - struct SDL_Window* getWindow() { - return nullptr; //_handle; - } - private: void pollEvent() override; - bool _inited{false}; bool _quit{false}; - // std::unique_ptr _sdl; - screen_context_t _screenCtx; - screen_window_t _screenWin; + std::shared_ptr _window; }; } // namespace cc diff --git a/native/cocos/platform/qnx/modules/SystemWindow.cpp b/native/cocos/platform/qnx/modules/SystemWindow.cpp index 199758ad1d..af5408ae6f 100644 --- a/native/cocos/platform/qnx/modules/SystemWindow.cpp +++ b/native/cocos/platform/qnx/modules/SystemWindow.cpp @@ -37,39 +37,90 @@ #include "platform/IEventDispatch.h" #include "platform/qnx/QnxPlatform.h" -namespace { - -} // namespace - namespace cc { SystemWindow::SystemWindow() { } SystemWindow::~SystemWindow() { + if (_screenWin) { + screen_destroy_window(_screenWin); + } + if (_screenCtx) { + screen_destroy_context(_screenCtx); + } +} + +bool SystemWindow::createWindow(const char *title, + int w, int h, int flags) { + _width = w; + _height = h; + createWindow(title, 0, 0, w, h, flags); + return true; } bool SystemWindow::createWindow(const char *title, int x, int y, int w, int h, int flags) { - // Create window - - QnxPlatform *platform = dynamic_cast(BasePlatform::getPlatform()); - CCASSERT(platform != nullptr, "Platform pointer can't be null"); - platform->createWindow(title, x, y, w, h, flags); + if (_screenWin) { + return true; + } _width = w; _height = h; + + //Create the screen context + int rc = screen_create_context(&_screenCtx, SCREEN_APPLICATION_CONTEXT); + if (rc) { + perror("screen_create_window"); + return EXIT_FAILURE; + } + + //Create the screen window that will be render onto + rc = screen_create_window(&_screenWin, _screenCtx); + if (rc) { + perror("screen_create_window"); + return EXIT_FAILURE; + } + + screen_set_window_property_iv(_screenWin, SCREEN_PROPERTY_FORMAT, (const int[]){SCREEN_FORMAT_RGBX8888}); +#ifdef CC_USE_GLES3 + screen_set_window_property_iv(_screenWin, SCREEN_PROPERTY_USAGE, (const int[]){SCREEN_USAGE_OPENGL_ES3}); +#elif CC_USE_GLES2 + screen_set_window_property_iv(_screenWin, SCREEN_PROPERTY_USAGE, (const int[]){SCREEN_USAGE_OPENGL_ES2}); +#endif + + int pos[2] = {x, y}; /* size of the window on screen */ + rc = screen_set_window_property_iv(_screenWin, SCREEN_PROPERTY_POSITION, pos); + if (rc) { + perror("screen_set_window_property_iv(SCREEN_PROPERTY_SIZE)"); + return EXIT_FAILURE; + } + + int size[2] = {w, h}; /* size of the window on screen */ + rc = screen_set_window_property_iv(_screenWin, SCREEN_PROPERTY_SIZE, size); + if (rc) { + perror("screen_set_window_property_iv(SCREEN_PROPERTY_POSITION)"); + return EXIT_FAILURE; + } + + int dpi = 0; + screen_get_window_property_iv(_screenWin, SCREEN_PROPERTY_DPI, &dpi); + fprintf(stdout, "[glError] %d\n", dpi); + + rc = screen_create_window_buffers(_screenWin, 2); + if (rc) { + perror("screen_create_window_buffers"); + return EXIT_FAILURE; + } + // return _sdl->createWindow(title, x, y, w, h, flags); return true; } uintptr_t SystemWindow::getWindowHandler() const { - //return _handle; - QnxPlatform *platform = dynamic_cast(BasePlatform::getPlatform()); - CCASSERT(platform != nullptr, "Platform pointer can't be null"); - return platform->getWindowHandler(); + return reinterpret_cast(_screenWin); } void SystemWindow::setCursorEnabled(bool value) { - SDL_SetRelativeMouseMode(value ? SDL_FALSE : SDL_TRUE); + //TODO } void SystemWindow::copyTextToClipboard(const std::string &text) { diff --git a/native/cocos/platform/qnx/modules/SystemWindow.h b/native/cocos/platform/qnx/modules/SystemWindow.h index 1ff468a243..ef72695278 100644 --- a/native/cocos/platform/qnx/modules/SystemWindow.h +++ b/native/cocos/platform/qnx/modules/SystemWindow.h @@ -28,9 +28,7 @@ #include #include "platform/interfaces/modules/ISystemWindow.h" - -struct SDL_Window; -struct SDL_WindowEvent; +#include namespace cc { @@ -39,15 +37,8 @@ public: explicit SystemWindow(); ~SystemWindow() override; - class Delegate { - public: - virtual ~Delegate() = default; - virtual bool createWindow(const char* title, - int x, int y, int w, - int h, int flags) = 0; - virtual uintptr_t getWindowHandler() const = 0; - }; - + bool createWindow(const char* title, + int w, int h, int flags) override; bool createWindow(const char* title, int x, int y, int w, int h, int flags) override; @@ -63,6 +54,9 @@ public: private: int _width{0}; int _height{0}; + // std::unique_ptr _sdl; + screen_context_t _screenCtx{nullptr}; + screen_window_t _screenWin{nullptr}; }; } // namespace cc \ No newline at end of file diff --git a/native/cocos/platform/qnx/modules/Vibrate.cpp b/native/cocos/platform/qnx/modules/Vibrator.cpp similarity index 96% rename from native/cocos/platform/qnx/modules/Vibrate.cpp rename to native/cocos/platform/qnx/modules/Vibrator.cpp index f93322ae4d..e7c2c3c659 100644 --- a/native/cocos/platform/qnx/modules/Vibrate.cpp +++ b/native/cocos/platform/qnx/modules/Vibrator.cpp @@ -23,7 +23,7 @@ THE SOFTWARE. ****************************************************************************/ -#include "platform/qnx/modules/Vibrate.h" +#include "platform/qnx/modules/Vibrator.h" #include "base/Macros.h" diff --git a/native/cocos/platform/qnx/modules/Vibrate.h b/native/cocos/platform/qnx/modules/Vibrator.h similarity index 100% rename from native/cocos/platform/qnx/modules/Vibrate.h rename to native/cocos/platform/qnx/modules/Vibrator.h diff --git a/native/cocos/platform/win32/WindowsPlatform.cpp b/native/cocos/platform/win32/WindowsPlatform.cpp index e944542886..4ffda82553 100644 --- a/native/cocos/platform/win32/WindowsPlatform.cpp +++ b/native/cocos/platform/win32/WindowsPlatform.cpp @@ -28,9 +28,14 @@ #include #include #include -#include "platform/interfaces/OSInterface.h" -#include "platform/interfaces/modules/ISystemWindow.h" -#include "platform/SDLHelper.h" + +#include "modules/Accelerometer.h" +#include "modules/Battery.h" +#include "modules/Network.h" +#include "modules/Screen.h" +#include "modules/System.h" +#include "modules/SystemWindow.h" +#include "modules/Vibrator.h" namespace { /** @@ -73,7 +78,6 @@ void PVRFrameEnableControlWindow(bool bEnable) { namespace cc { WindowsPlatform::WindowsPlatform() { - _sdl = std::make_unique(this); } WindowsPlatform::~WindowsPlatform() { @@ -83,7 +87,15 @@ WindowsPlatform::~WindowsPlatform() { } int32_t WindowsPlatform::init() { - UniversalPlatform::init(); + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + registerInterface(std::make_shared()); + _window = std::make_shared(this); + registerInterface(_window); + registerInterface(std::make_shared()); + #ifdef USE_WIN32_CONSOLE AllocConsole(); freopen("CONIN$", "r", stdin); @@ -93,7 +105,7 @@ int32_t WindowsPlatform::init() { PVRFrameEnableControlWindow(false); - return _sdl->init() ? 0 : -1; + return _window->init(); } int32_t WindowsPlatform::loop() { @@ -126,13 +138,13 @@ int32_t WindowsPlatform::loop() { onResume(); while (!_quit) { desiredInterval = (LONGLONG)(1.0 / getFps() * nFreq.QuadPart); - pollEvent(); + _window->pollEvent(&_quit); QueryPerformanceCounter(&nNow); actualInterval = nNow.QuadPart - nLast.QuadPart; if (actualInterval >= desiredInterval) { nLast.QuadPart = nNow.QuadPart; runTask(); - _sdl->swapWindow(); + _window->swapWindow(); } else { // The precision of timer on Windows is set to highest (1ms) by 'timeBeginPeriod' from above code, // but it's still not precise enough. For example, if the precision of timer is 1ms, @@ -152,19 +164,4 @@ int32_t WindowsPlatform::loop() { return 0; } - -void WindowsPlatform::pollEvent() { - _sdl->pollEvent(&_quit); -} - -bool WindowsPlatform::createWindow(const char *title, - int x, int y, int w, - int h, int flags) { - return _sdl->createWindow(title, x, y, w, h, flags); -} - -uintptr_t WindowsPlatform::getWindowHandler() const { - return _sdl->getWindowHandler(); -} - } // namespace cc diff --git a/native/cocos/platform/win32/WindowsPlatform.h b/native/cocos/platform/win32/WindowsPlatform.h index 90497e17fe..0c9bd01e69 100644 --- a/native/cocos/platform/win32/WindowsPlatform.h +++ b/native/cocos/platform/win32/WindowsPlatform.h @@ -25,13 +25,12 @@ #pragma once +#include #include "platform/UniversalPlatform.h" -#include "platform/win32/modules/SystemWindow.h" namespace cc { -class SDLHelper; -class CC_DLL WindowsPlatform : public UniversalPlatform, - public SystemWindow::Delegate { +class SystemWindow; +class CC_DLL WindowsPlatform : public UniversalPlatform { public: WindowsPlatform(); /** @@ -45,16 +44,9 @@ public: int32_t loop() override; - // override from SystemWindow::Delegate - bool createWindow(const char* title, - int x, int y, int w, - int h, int flags) override; - uintptr_t getWindowHandler() const override; - private: - void pollEvent() override; - bool _quit{false}; - std::unique_ptr _sdl; + std::shared_ptr _window{nullptr}; + bool _quit{false}; }; } // namespace cc diff --git a/native/cocos/platform/win32/modules/CanvasRenderingContext2DDelegate.cpp b/native/cocos/platform/win32/modules/CanvasRenderingContext2DDelegate.cpp index 3b17318667..d912fcdb8d 100644 --- a/native/cocos/platform/win32/modules/CanvasRenderingContext2DDelegate.cpp +++ b/native/cocos/platform/win32/modules/CanvasRenderingContext2DDelegate.cpp @@ -501,4 +501,4 @@ void CanvasRenderingContext2DDelegate::rect(float /* x */, float /* h */) { } -} // namespace cc \ No newline at end of file +} // namespace cc diff --git a/native/cocos/platform/win32/modules/SystemWindow.cpp b/native/cocos/platform/win32/modules/SystemWindow.cpp index c4ab92c1d0..451989c027 100644 --- a/native/cocos/platform/win32/modules/SystemWindow.cpp +++ b/native/cocos/platform/win32/modules/SystemWindow.cpp @@ -31,42 +31,52 @@ #include "bindings/event/EventDispatcher.h" #include "platform/IEventDispatch.h" #include "platform/win32/WindowsPlatform.h" -#include "sdl2/SDL.h" -#include "sdl2/SDL_main.h" -#include "sdl2/SDL_syswm.h" - -namespace { - -} // namespace +#include "platform/SDLHelper.h" namespace cc { -SystemWindow::SystemWindow() { +SystemWindow::SystemWindow(IEventDispatch *delegate) +: _sdl(std::make_unique(delegate)) { + } SystemWindow::~SystemWindow() { } +int SystemWindow::init() { + return _sdl->init(); +} + +void SystemWindow::pollEvent(bool *quit) { + return _sdl->pollEvent(quit); +} + +void SystemWindow::swapWindow() { + _sdl->swapWindow(); +} + +bool SystemWindow::createWindow(const char *title, + int w, int h, int flags) { + _sdl->createWindow(title, w, h, flags); + _width = w; + _height = h; + return true; +} + bool SystemWindow::createWindow(const char *title, int x, int y, int w, int h, int flags) { - // Create window - - WindowsPlatform *platform = dynamic_cast(BasePlatform::getPlatform()); - CCASSERT(platform != nullptr, "Platform pointer can't be null"); - platform->createWindow(title, x, y, w, h, flags); + _sdl->createWindow(title, x, y, w, h, flags); _width = w; _height = h; return true; } uintptr_t SystemWindow::getWindowHandler() const { - WindowsPlatform *platform = dynamic_cast(BasePlatform::getPlatform()); - CCASSERT(platform != nullptr, "Platform pointer can't be null"); - return platform->getWindowHandler(); + return _sdl->getWindowHandler(); } void SystemWindow::setCursorEnabled(bool value) { - SDL_SetRelativeMouseMode(value ? SDL_FALSE : SDL_TRUE); + _sdl->setCursorEnabled(value); } void SystemWindow::copyTextToClipboard(const std::string &text) { diff --git a/native/cocos/platform/win32/modules/SystemWindow.h b/native/cocos/platform/win32/modules/SystemWindow.h index caaa6bb39d..23087a1156 100644 --- a/native/cocos/platform/win32/modules/SystemWindow.h +++ b/native/cocos/platform/win32/modules/SystemWindow.h @@ -29,30 +29,23 @@ #include "platform/interfaces/modules/ISystemWindow.h" -struct SDL_Window; -struct SDL_WindowEvent; - namespace cc { - +class SDLHelper; class CC_DLL SystemWindow : public ISystemWindow { public: - explicit SystemWindow(); + explicit SystemWindow(IEventDispatch* delegate); ~SystemWindow() override; - class Delegate { - public: - virtual ~Delegate() = default; - virtual bool createWindow(const char* title, - int x, int y, int w, - int h, int flags) = 0; - virtual uintptr_t getWindowHandler() const = 0; - }; - + int init(); + void pollEvent(bool* quit); + void swapWindow(); + + bool createWindow(const char* title, + int w, int h, int flags) override; bool createWindow(const char* title, int x, int y, int w, int h, int flags) override; uintptr_t getWindowHandler() const override; - Size getViewSize() const override; /* @brief enable/disable(lock) the cursor, default is enabled @@ -63,6 +56,7 @@ public: private: int _width{0}; int _height{0}; + std::unique_ptr _sdl; }; } // namespace cc