From fee7afc74a2ca5c2da0365185a10edd698b4f70e Mon Sep 17 00:00:00 2001 From: minggo Date: Sun, 11 Feb 2018 14:25:14 +0800 Subject: [PATCH] mac main loop work (#1112) * mac main loop work * fix some issue --- .../cocos2d_libs.xcodeproj/project.pbxproj | 12 + native/cocos/platform/CCApplication.h | 15 +- .../platform/desktop/CCGLView-desktop.cpp | 319 ++++++++++++++++++ .../cocos/platform/desktop/CCGLView-desktop.h | 124 +++++++ .../cocos/platform/ios/CCApplication-ios.mm | 20 +- .../cocos/platform/mac/CCApplication-mac.mm | 220 ++++++++++++ .../js-bindings/event/EventDispatch.cpp | 6 +- .../js-bindings/event/EventDispatch.h | 3 +- .../runtime-src/Classes/AppDelegate.cpp | 2 +- 9 files changed, 703 insertions(+), 18 deletions(-) create mode 100644 native/cocos/platform/desktop/CCGLView-desktop.cpp create mode 100644 native/cocos/platform/desktop/CCGLView-desktop.h create mode 100644 native/cocos/platform/mac/CCApplication-mac.mm diff --git a/native/build/cocos2d_libs.xcodeproj/project.pbxproj b/native/build/cocos2d_libs.xcodeproj/project.pbxproj index ff7e376190..9be4b436f2 100644 --- a/native/build/cocos2d_libs.xcodeproj/project.pbxproj +++ b/native/build/cocos2d_libs.xcodeproj/project.pbxproj @@ -104,6 +104,9 @@ 294D7D9B1D0E93A2002CE7B7 /* CCDevice-apple.h in Headers */ = {isa = PBXBuildFile; fileRef = 294D7D981D0E93A2002CE7B7 /* CCDevice-apple.h */; }; 294D7D9C1D0E93A2002CE7B7 /* CCDevice-apple.mm in Sources */ = {isa = PBXBuildFile; fileRef = 294D7D991D0E93A2002CE7B7 /* CCDevice-apple.mm */; }; 294D7D9D1D0E93A2002CE7B7 /* CCDevice-apple.mm in Sources */ = {isa = PBXBuildFile; fileRef = 294D7D991D0E93A2002CE7B7 /* CCDevice-apple.mm */; }; + 4626B1FF202D9BD800BE30B9 /* CCApplication-mac.mm in Sources */ = {isa = PBXBuildFile; fileRef = 4626B1FE202D9BD800BE30B9 /* CCApplication-mac.mm */; }; + 4626B202202D9BE700BE30B9 /* CCGLView-desktop.h in Headers */ = {isa = PBXBuildFile; fileRef = 4626B200202D9BE700BE30B9 /* CCGLView-desktop.h */; }; + 4626B203202D9BE700BE30B9 /* CCGLView-desktop.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4626B201202D9BE700BE30B9 /* CCGLView-desktop.cpp */; }; 46FDDA6D202ACC6A00931238 /* INode.h in Headers */ = {isa = PBXBuildFile; fileRef = 46FDDA2E202ACC6A00931238 /* INode.h */; }; 46FDDA6E202ACC6A00931238 /* INode.h in Headers */ = {isa = PBXBuildFile; fileRef = 46FDDA2E202ACC6A00931238 /* INode.h */; }; 46FDDA6F202ACC6A00931238 /* Pass.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 46FDDA2F202ACC6A00931238 /* Pass.cpp */; }; @@ -787,6 +790,9 @@ 1ACF6A3B1E4AFDC80033C137 /* libssl.a */ = {isa = PBXFileReference; lastKnownFileType = archive.ar; name = libssl.a; path = ../external/ios/libs/libssl.a; sourceTree = ""; }; 294D7D981D0E93A2002CE7B7 /* CCDevice-apple.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = "CCDevice-apple.h"; sourceTree = ""; }; 294D7D991D0E93A2002CE7B7 /* CCDevice-apple.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = "CCDevice-apple.mm"; sourceTree = ""; }; + 4626B1FE202D9BD800BE30B9 /* CCApplication-mac.mm */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.objcpp; path = "CCApplication-mac.mm"; sourceTree = ""; }; + 4626B200202D9BE700BE30B9 /* CCGLView-desktop.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; name = "CCGLView-desktop.h"; path = "../desktop/CCGLView-desktop.h"; sourceTree = ""; }; + 4626B201202D9BE700BE30B9 /* CCGLView-desktop.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = "CCGLView-desktop.cpp"; path = "../desktop/CCGLView-desktop.cpp"; sourceTree = ""; }; 46FDDA2E202ACC6A00931238 /* INode.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = INode.h; sourceTree = ""; }; 46FDDA2F202ACC6A00931238 /* Pass.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Pass.cpp; sourceTree = ""; }; 46FDDA30202ACC6A00931238 /* Scene.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; path = Scene.cpp; sourceTree = ""; }; @@ -1804,6 +1810,9 @@ 50ABBF4E1926664700A911A9 /* mac */ = { isa = PBXGroup; children = ( + 4626B201202D9BE700BE30B9 /* CCGLView-desktop.cpp */, + 4626B200202D9BE700BE30B9 /* CCGLView-desktop.h */, + 4626B1FE202D9BD800BE30B9 /* CCApplication-mac.mm */, 50ABBF521926664700A911A9 /* CCDevice-mac.mm */, 50ABBF531926664700A911A9 /* CCGL-mac.h */, 50ABBF541926664700A911A9 /* CCPlatformDefine-mac.h */, @@ -2058,6 +2067,7 @@ 46FDDC63202D502F00931238 /* CCApplication.h in Headers */, 4DED48081DFFA4AF0070C5C4 /* b2GrowableStack.h in Headers */, BA68D79A1D62F4B700B7A3F9 /* clipper.hpp in Headers */, + 4626B202202D9BE700BE30B9 /* CCGLView-desktop.h in Headers */, 50ABBD521925AB0000A911A9 /* Quaternion.h in Headers */, BA68D7931D62F4A500B7A3F9 /* sweep_context.h in Headers */, 4DED482A1DFFA4AF0070C5C4 /* b2TimeStep.h in Headers */, @@ -2440,6 +2450,7 @@ 46FDDBA9202ADDCE00931238 /* CCProfiling.cpp in Sources */, 4DED47F21DFFA4AF0070C5C4 /* b2CircleShape.cpp in Sources */, 4DED481E1DFFA4AF0070C5C4 /* b2ContactManager.cpp in Sources */, + 4626B1FF202D9BD800BE30B9 /* CCApplication-mac.mm in Sources */, 5027253C190BF1B900AAF4ED /* cocos2d.cpp in Sources */, 4DED483C1DFFA4AF0070C5C4 /* b2CircleContact.cpp in Sources */, 4DED47EA1DFFA4AF0070C5C4 /* b2TimeOfImpact.cpp in Sources */, @@ -2562,6 +2573,7 @@ 4DED48341DFFA4AF0070C5C4 /* b2ChainAndCircleContact.cpp in Sources */, 4DED47E21DFFA4AF0070C5C4 /* b2Distance.cpp in Sources */, 46FDDAD3202ACC6A00931238 /* Texture2D.cpp in Sources */, + 4626B203202D9BE700BE30B9 /* CCGLView-desktop.cpp in Sources */, 46FDDB6F202ADDCE00931238 /* ZipUtils.cpp in Sources */, 1A28FF871F20AFAB007A1D9D /* SRSIMDHelpers.m in Sources */, 50ABBD601925AB0000A911A9 /* Vec4.cpp in Sources */, diff --git a/native/cocos/platform/CCApplication.h b/native/cocos/platform/CCApplication.h index 5141e8f284..3ed49f9ecf 100644 --- a/native/cocos/platform/CCApplication.h +++ b/native/cocos/platform/CCApplication.h @@ -75,9 +75,13 @@ public: BULGARIAN }; - Application(); + Application(const std::string& name); virtual ~Application(); + virtual bool applicationDidFinishLaunching(); + virtual void applicationDidEnterBackground(); + virtual void applicationWillEnterForeground(); + inline void* getView() const { return _view; } void start(); @@ -125,17 +129,16 @@ public: bool openURL(const std::string &url); protected: - virtual bool applicationDidFinishLaunching(); - virtual void applicationDidEnterBackground(); - virtual void applicationWillEnterForeground(); - virtual void onCreateView(int& x, int& y, int& width, int& height, int& rBits, int& gBits, int& bBits, int& aBits, int& depthBits, int& stencilBits); + virtual void onCreateView(int& x, int& y, int& width, int& height, + int& rBits, int& gBits, int& bBits, int& aBits, int& depthBits, int& stencilBits, int& multisamplingCount); private: - void createView(); + void createView(const std::string& name); void* _view = nullptr; bool _multiTouch = false; void* _delegate = nullptr; + float _animationInterval = 1.0 / 60; }; // end of platform group diff --git a/native/cocos/platform/desktop/CCGLView-desktop.cpp b/native/cocos/platform/desktop/CCGLView-desktop.cpp new file mode 100644 index 0000000000..a2aaf0b953 --- /dev/null +++ b/native/cocos/platform/desktop/CCGLView-desktop.cpp @@ -0,0 +1,319 @@ +/**************************************************************************** +Copyright (c) 2010-2012 cocos2d-x.org +Copyright (c) 2013-2016 Chukong Technologies Inc. +Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. + +http://www.cocos2d-x.org + +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 "platform/desktop/CCGLView-desktop.h" + +#include +#include + +#include "scripting/js-bindings/event/EventDispatch.h" +#include "ccMacros.h" +#include "platform/CCApplication.h" + + +NS_CC_BEGIN + +GLView* GLFWEventHandler::_view = nullptr; + + +////////////////////////////////////////////////////////////////////////// +// implement GLView +////////////////////////////////////////////////////////////////////////// + +GLView::GLView(Application* application, const std::string& name, int x, int y, int width, int height, int r, int g, int b, int a, int depth, int stencil, int multisamplingCount) +: _application(application) +, _captured(false) +, _mainWindow(nullptr) +, _monitor(nullptr) +, _mouseX(0.0f) +, _mouseY(0.0f) +{ + GLFWEventHandler::setGLView(this); + glfwSetErrorCallback(GLFWEventHandler::onGLFWError); + glfwInit(); + + glfwWindowHint(GLFW_RED_BITS, r); + glfwWindowHint(GLFW_GREEN_BITS, g); + glfwWindowHint(GLFW_BLUE_BITS, b); + glfwWindowHint(GLFW_ALPHA_BITS, a); + glfwWindowHint(GLFW_DEPTH_BITS, depth); + glfwWindowHint(GLFW_STENCIL_BITS, stencil); + glfwWindowHint(GLFW_SAMPLES, multisamplingCount); + _mainWindow = glfwCreateWindow(width, height, name.c_str(), _monitor, nullptr); + + if (_mainWindow == nullptr) + { + std::string message = "Can't create window"; + if (!_glfwError.empty()) + { + message.append("\nMore info: \n"); + message.append(_glfwError); + } + printf("%s\n", message.c_str()); + return; + } + + glfwMakeContextCurrent(_mainWindow); + + glfwSetMouseButtonCallback(_mainWindow, GLFWEventHandler::onGLFWMouseCallBack); + glfwSetCursorPosCallback(_mainWindow, GLFWEventHandler::onGLFWMouseMoveCallBack); + glfwSetCharCallback(_mainWindow, GLFWEventHandler::onGLFWCharCallback); + glfwSetKeyCallback(_mainWindow, GLFWEventHandler::onGLFWKeyCallback); + glfwSetWindowIconifyCallback(_mainWindow, GLFWEventHandler::onGLFWWindowIconifyCallback); + + // check OpenGL version at first + const GLubyte* glVersion = glGetString(GL_VERSION); + + if (utils::atof((const char*)glVersion) < 1.5 ) + { + char strComplain[256] = {0}; + sprintf(strComplain, + "OpenGL 1.5 or higher is required (your version is %s). Please upgrade the driver of your video card.", + glVersion); + printf("%s\n", strComplain); + return; + } + + initGlew(); + + // Enable point size by default. + glEnable(GL_VERTEX_PROGRAM_POINT_SIZE); + + if(multisamplingCount > 0) + glEnable(GL_MULTISAMPLE); +} + +GLView::~GLView() +{ + GLFWEventHandler::setGLView(nullptr); + glfwTerminate(); +} + +bool GLView::windowShouldClose() const +{ + if(_mainWindow) + return glfwWindowShouldClose(_mainWindow) ? true : false; + else + return true; +} + +void GLView::pollEvents() +{ + glfwPollEvents(); +} + +void GLView::onGLFWError(int errorID, const char* errorDesc) +{ + if (_mainWindow) + printf("GLFWError #%d Happen, %s\n", errorID, errorDesc); + else + printf("GLFWError #%d Happen, %s\n", errorID, errorDesc); +} + +namespace +{ + void dispatchTouchEvent(float x, float y, cocos2d::TouchEvent::Type type) + { + cocos2d::TouchInfo touchInfo; + touchInfo.x = x; + touchInfo.y = y; + touchInfo.index = 0; + + cocos2d::TouchEvent touchEvent; + touchEvent.type = type; + touchEvent.touches.push_back(touchInfo); + + cocos2d::EventDispatch::dispatchTouchEvent(touchEvent); + } +} + +void GLView::onGLFWMouseCallBack(GLFWwindow* /*window*/, int button, int action, int /*modify*/) +{ + if(GLFW_MOUSE_BUTTON_LEFT == button) + { + if(GLFW_PRESS == action) + { + _captured = true; + dispatchTouchEvent(_mouseX, _mouseY, TouchEvent::Type::BEGAN); + } + else if(GLFW_RELEASE == action) + { + if (_captured) + dispatchTouchEvent(_mouseX, _mouseY, TouchEvent::Type::ENDED); + } + } +} + +void GLView::onGLFWMouseMoveCallBack(GLFWwindow* window, double x, double y) +{ + _mouseX = (float)x; + _mouseY = (float)y; + + if (_captured) + dispatchTouchEvent(_mouseX, _mouseY, TouchEvent::Type::MOVED); +} + +void GLView::onGLFWKeyCallback(GLFWwindow* /*window*/, int key, int /*scancode*/, int action, int /*mods*/) +{ + EventDispatch::dispatchKeyEvent(key, action); +} + +void GLView::onGLFWCharCallback(GLFWwindow* /*window*/, unsigned int character) +{ + // TODO + // char16_t wcharString[2] = { (char16_t) character, 0 }; + // std::string utf8String; + + // StringUtils::UTF16ToUTF8( wcharString, utf8String ); + // static std::set controlUnicode = { + // "\xEF\x9C\x80", // up + // "\xEF\x9C\x81", // down + // "\xEF\x9C\x82", // left + // "\xEF\x9C\x83", // right + // "\xEF\x9C\xA8", // delete + // "\xEF\x9C\xA9", // home + // "\xEF\x9C\xAB", // end + // "\xEF\x9C\xAC", // pageup + // "\xEF\x9C\xAD", // pagedown + // "\xEF\x9C\xB9" // clear + // }; +} + +void GLView::onGLFWWindowIconifyCallback(GLFWwindow* /*window*/, int iconified) +{ + if (iconified == GL_TRUE) + _application->applicationDidEnterBackground(); + else + _application->applicationWillEnterForeground(); +} + +#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) +static bool glew_dynamic_binding() +{ + const char *gl_extensions = (const char*)glGetString(GL_EXTENSIONS); + + // If the current opengl driver doesn't have framebuffers methods, check if an extension exists + if (glGenFramebuffers == nullptr) + { + log("OpenGL: glGenFramebuffers is nullptr, try to detect an extension"); + if (strstr(gl_extensions, "ARB_framebuffer_object")) + { + log("OpenGL: ARB_framebuffer_object is supported"); + + glIsRenderbuffer = (PFNGLISRENDERBUFFERPROC) wglGetProcAddress("glIsRenderbuffer"); + glBindRenderbuffer = (PFNGLBINDRENDERBUFFERPROC) wglGetProcAddress("glBindRenderbuffer"); + glDeleteRenderbuffers = (PFNGLDELETERENDERBUFFERSPROC) wglGetProcAddress("glDeleteRenderbuffers"); + glGenRenderbuffers = (PFNGLGENRENDERBUFFERSPROC) wglGetProcAddress("glGenRenderbuffers"); + glRenderbufferStorage = (PFNGLRENDERBUFFERSTORAGEPROC) wglGetProcAddress("glRenderbufferStorage"); + glGetRenderbufferParameteriv = (PFNGLGETRENDERBUFFERPARAMETERIVPROC) wglGetProcAddress("glGetRenderbufferParameteriv"); + glIsFramebuffer = (PFNGLISFRAMEBUFFERPROC) wglGetProcAddress("glIsFramebuffer"); + glBindFramebuffer = (PFNGLBINDFRAMEBUFFERPROC) wglGetProcAddress("glBindFramebuffer"); + glDeleteFramebuffers = (PFNGLDELETEFRAMEBUFFERSPROC) wglGetProcAddress("glDeleteFramebuffers"); + glGenFramebuffers = (PFNGLGENFRAMEBUFFERSPROC) wglGetProcAddress("glGenFramebuffers"); + glCheckFramebufferStatus = (PFNGLCHECKFRAMEBUFFERSTATUSPROC) wglGetProcAddress("glCheckFramebufferStatus"); + glFramebufferTexture1D = (PFNGLFRAMEBUFFERTEXTURE1DPROC) wglGetProcAddress("glFramebufferTexture1D"); + glFramebufferTexture2D = (PFNGLFRAMEBUFFERTEXTURE2DPROC) wglGetProcAddress("glFramebufferTexture2D"); + glFramebufferTexture3D = (PFNGLFRAMEBUFFERTEXTURE3DPROC) wglGetProcAddress("glFramebufferTexture3D"); + glFramebufferRenderbuffer = (PFNGLFRAMEBUFFERRENDERBUFFERPROC) wglGetProcAddress("glFramebufferRenderbuffer"); + glGetFramebufferAttachmentParameteriv = (PFNGLGETFRAMEBUFFERATTACHMENTPARAMETERIVPROC) wglGetProcAddress("glGetFramebufferAttachmentParameteriv"); + glGenerateMipmap = (PFNGLGENERATEMIPMAPPROC) wglGetProcAddress("glGenerateMipmap"); + } + else + if (strstr(gl_extensions, "EXT_framebuffer_object")) + { + log("OpenGL: EXT_framebuffer_object is supported"); + glIsRenderbuffer = (PFNGLISRENDERBUFFERPROC) wglGetProcAddress("glIsRenderbufferEXT"); + glBindRenderbuffer = (PFNGLBINDRENDERBUFFERPROC) wglGetProcAddress("glBindRenderbufferEXT"); + glDeleteRenderbuffers = (PFNGLDELETERENDERBUFFERSPROC) wglGetProcAddress("glDeleteRenderbuffersEXT"); + glGenRenderbuffers = (PFNGLGENRENDERBUFFERSPROC) wglGetProcAddress("glGenRenderbuffersEXT"); + glRenderbufferStorage = (PFNGLRENDERBUFFERSTORAGEPROC) wglGetProcAddress("glRenderbufferStorageEXT"); + glGetRenderbufferParameteriv = (PFNGLGETRENDERBUFFERPARAMETERIVPROC) wglGetProcAddress("glGetRenderbufferParameterivEXT"); + glIsFramebuffer = (PFNGLISFRAMEBUFFERPROC) wglGetProcAddress("glIsFramebufferEXT"); + glBindFramebuffer = (PFNGLBINDFRAMEBUFFERPROC) wglGetProcAddress("glBindFramebufferEXT"); + glDeleteFramebuffers = (PFNGLDELETEFRAMEBUFFERSPROC) wglGetProcAddress("glDeleteFramebuffersEXT"); + glGenFramebuffers = (PFNGLGENFRAMEBUFFERSPROC) wglGetProcAddress("glGenFramebuffersEXT"); + glCheckFramebufferStatus = (PFNGLCHECKFRAMEBUFFERSTATUSPROC) wglGetProcAddress("glCheckFramebufferStatusEXT"); + glFramebufferTexture1D = (PFNGLFRAMEBUFFERTEXTURE1DPROC) wglGetProcAddress("glFramebufferTexture1DEXT"); + glFramebufferTexture2D = (PFNGLFRAMEBUFFERTEXTURE2DPROC) wglGetProcAddress("glFramebufferTexture2DEXT"); + glFramebufferTexture3D = (PFNGLFRAMEBUFFERTEXTURE3DPROC) wglGetProcAddress("glFramebufferTexture3DEXT"); + glFramebufferRenderbuffer = (PFNGLFRAMEBUFFERRENDERBUFFERPROC) wglGetProcAddress("glFramebufferRenderbufferEXT"); + glGetFramebufferAttachmentParameteriv = (PFNGLGETFRAMEBUFFERATTACHMENTPARAMETERIVPROC) wglGetProcAddress("glGetFramebufferAttachmentParameterivEXT"); + glGenerateMipmap = (PFNGLGENERATEMIPMAPPROC) wglGetProcAddress("glGenerateMipmapEXT"); + } + else + { + log("OpenGL: No framebuffers extension is supported"); + log("OpenGL: Any call to Fbo will crash!"); + return false; + } + } + return true; +} +#endif + +// helper +bool GLView::initGlew() +{ +#if (CC_TARGET_PLATFORM != CC_PLATFORM_MAC) + GLenum GlewInitResult = glewInit(); + if (GLEW_OK != GlewInitResult) + { + log((char *)glewGetErrorString(GlewInitResult), "OpenGL error"); + return false; + } + + if (GLEW_ARB_vertex_shader && GLEW_ARB_fragment_shader) + { + log("Ready for GLSL"); + } + else + { + log("Not totally ready :("); + } + + if (glewIsSupported("GL_VERSION_2_0")) + { + log("Ready for OpenGL 2.0"); + } + else + { + log("OpenGL 2.0 not supported"); + } + +#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) + if(glew_dynamic_binding() == false) + { + log("No OpenGL framebuffer support. Please upgrade the driver of your video card.", "OpenGL error"); + return false; + } +#endif + +#endif // (CC_TARGET_PLATFORM != CC_PLATFORM_MAC) + + return true; +} + +NS_CC_END // end of namespace cocos2d; diff --git a/native/cocos/platform/desktop/CCGLView-desktop.h b/native/cocos/platform/desktop/CCGLView-desktop.h new file mode 100644 index 0000000000..e2d7eca463 --- /dev/null +++ b/native/cocos/platform/desktop/CCGLView-desktop.h @@ -0,0 +1,124 @@ +/**************************************************************************** +Copyright (c) 2010-2012 cocos2d-x.org +Copyright (c) 2013-2016 Chukong Technologies Inc. +Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. + +http://www.cocos2d-x.org + +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 "glfw3/glfw3.h" +#include "glfw3/glfw3native.h" + +#include "platform/CCPlatformMacros.h" + +#ifndef GLFW_EXPOSE_NATIVE_WIN32 +#define GLFW_EXPOSE_NATIVE_WIN32 +#endif + +#ifndef GLFW_EXPOSE_NATIVE_WGL +#define GLFW_EXPOSE_NATIVE_WGL +#endif + +NS_CC_BEGIN + +class Application; + +class CC_DLL GLView final +{ +public: + GLView(Application *application, const std::string& name, int x, int y, int width, int height, int r, int g, int b, int a, int depth, int stencil, int multiSampleCount); + ~GLView(); + + bool windowShouldClose() const; + void pollEvents(); + +private: + bool initGlew(); + + // GLFW callbacks + void onGLFWError(int errorID, const char* errorDesc); + void onGLFWMouseCallBack(GLFWwindow* window, int button, int action, int modify); + void onGLFWMouseMoveCallBack(GLFWwindow* window, double x, double y); + void onGLFWKeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods); + void onGLFWCharCallback(GLFWwindow* window, unsigned int character); + void onGLFWWindowIconifyCallback(GLFWwindow* window, int iconified); + + bool _captured = false; + + GLFWwindow* _mainWindow = nullptr; + GLFWmonitor* _monitor = nullptr; + + std::string _glfwError; + + Application *_application = nullptr; + + float _mouseX = 0; + float _mouseY = 0; + + friend class GLFWEventHandler; +}; + + +class CC_DLL GLFWEventHandler final +{ +public: + static void onGLFWError(int errorID, const char* errorDesc) + { + _view->onGLFWError(errorID, errorDesc); + } + + static void onGLFWMouseCallBack(GLFWwindow* window, int button, int action, int modify) + { + _view->onGLFWMouseCallBack(window, button, action, modify); + } + + static void onGLFWMouseMoveCallBack(GLFWwindow* window, double x, double y) + { + _view->onGLFWMouseMoveCallBack(window, x, y); + } + + static void onGLFWKeyCallback(GLFWwindow* window, int key, int scancode, int action, int mods) + { + _view->onGLFWKeyCallback(window, key, scancode, action, mods); + } + + static void onGLFWCharCallback(GLFWwindow* window, unsigned int character) + { + _view->onGLFWCharCallback(window, character); + } + + static void setGLView(GLView* view) + { + _view = view; + } + + static void onGLFWWindowIconifyCallback(GLFWwindow* window, int iconified) + { + _view->onGLFWWindowIconifyCallback(window, iconified); + } + +private: + static GLView* _view; +}; + +NS_CC_END // end of namespace cocos2d diff --git a/native/cocos/platform/ios/CCApplication-ios.mm b/native/cocos/platform/ios/CCApplication-ios.mm index 991312a310..2dec7372a3 100644 --- a/native/cocos/platform/ios/CCApplication-ios.mm +++ b/native/cocos/platform/ios/CCApplication-ios.mm @@ -125,9 +125,9 @@ NS_CC_BEGIN -Application::Application() +Application::Application(const std::string& name) { - createView(); + createView(name); renderer::DeviceGraphics::getInstance(); se::ScriptEngine::getInstance(); @@ -247,7 +247,7 @@ void Application::setMultitouch(bool value) } } -void Application::onCreateView(int&x, int& y, int& width, int& height, int& rBits, int& gBits, int& bBits, int& aBits, int& depthBits, int& stencilBits) +void Application::onCreateView(int&x, int& y, int& width, int& height, int& rBits, int& gBits, int& bBits, int& aBits, int& depthBits, int& stencilBits, int& multisamplingCount) { CGRect bounds = [UIScreen mainScreen].bounds; x = bounds.origin.x; @@ -261,9 +261,10 @@ void Application::onCreateView(int&x, int& y, int& width, int& height, int& rBit aBits = 0; depthBits = 24; stencilBits = 8; + multisamplingCount = 0; } -void Application::createView() +void Application::createView(const std::string& /*name*/) { int x = 0; int y = 0; @@ -275,12 +276,13 @@ void Application::createView() int a = 0; int depth = 0; int stencil = 0; + int multisamplingCount = 0; onCreateView(x, y, width, height, - r, g, b, a, depth, stencil); + r, g, b, a, depth, stencil, multisamplingCount); CGRect bounds; bounds.origin.x = x; @@ -288,17 +290,17 @@ void Application::createView() bounds.size.width = width; bounds.size.height = height; - //TODO: pixel format, depth format and multi sample + //TODO: pixel format, depth format // create view CCEAGLView *eaglView = [CCEAGLView viewWithFrame: bounds pixelFormat: kEAGLColorFormatRGB565 depthFormat: GL_DEPTH_COMPONENT24_OES preserveBackbuffer: NO sharegroup: nil - multiSampling: NO - numberOfSamples: 0]; + multiSampling: multisamplingCount != 0 + numberOfSamples: multisamplingCount]; - eaglView.backgroundColor = [UIColor redColor]; + eaglView.backgroundColor = [UIColor redColor]; // TODO: remove it [eaglView setMultipleTouchEnabled:_multiTouch]; [eaglView retain]; diff --git a/native/cocos/platform/mac/CCApplication-mac.mm b/native/cocos/platform/mac/CCApplication-mac.mm new file mode 100644 index 0000000000..fe8c8a980b --- /dev/null +++ b/native/cocos/platform/mac/CCApplication-mac.mm @@ -0,0 +1,220 @@ +/**************************************************************************** +Copyright (c) 2010-2012 cocos2d-x.org +Copyright (c) 2013-2016 Chukong Technologies Inc. +Copyright (c) 2017-2018 Xiamen Yaji Software Co., Ltd. + +http://www.cocos2d-x.org + +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. +****************************************************************************/ + +#import +#include + +#import "platform/CCApplication.h" +#import "platform/desktop/CCGLView-desktop.h" +#import "scripting/js-bindings/event/EventDispatch.h" +#import "renderer/gfx/DeviceGraphics.h" +#import "scripting/js-bindings/jswrapper/jsc/ScriptEngine.hpp" + +NS_CC_BEGIN + +namespace +{ + long getCurrentMillSecond() + { + long lLastTime = 0; + struct timeval stCurrentTime; + + gettimeofday(&stCurrentTime, nullptr); + lLastTime = stCurrentTime.tv_sec * 1000+stCurrentTime.tv_usec * 0.001; // milliseconds + return lLastTime; + } +} + +#define CAST_VIEW(view) ((GLView*)view) + +Application::Application(const std::string& name) +{ + createView(name); + + renderer::DeviceGraphics::getInstance(); + se::ScriptEngine::getInstance(); + + applicationDidFinishLaunching(); +} + +Application::~Application() +{ + // TODO: destroy DeviceGraphics + + se::ScriptEngine::destroyInstance(); + + delete CAST_VIEW(_view); + _view = nullptr; +} + +void Application::start() +{ + if(!applicationDidFinishLaunching()) + return; + + long lastTime = 0L; + long curTime = 0L; + + if (!_view) + return; + + while (!CAST_VIEW(_view)->windowShouldClose()) + { + lastTime = getCurrentMillSecond(); + + CAST_VIEW(_view)->pollEvents(); + + curTime = getCurrentMillSecond(); + if (curTime - lastTime < _animationInterval) + usleep(static_cast((_animationInterval - curTime + lastTime) * 1000)); + + EventDispatch::dispatchTicket(); + } +} + +void Application::setAnimationInterval(float interval) +{ + _animationInterval = interval * 1000.0f; +} + +Application::Platform Application::getPlatform() const +{ + return Platform::MAC; +} + +std::string Application::getCurrentLanguageCode() const +{ + static char code[3]={0}; + NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; + NSArray *languages = [defaults objectForKey:@"AppleLanguages"]; + NSString *currentLanguage = [languages objectAtIndex:0]; + + // get the current language code.(such as English is "en", Chinese is "zh" and so on) + NSDictionary* temp = [NSLocale componentsFromLocaleIdentifier:currentLanguage]; + NSString * languageCode = [temp objectForKey:NSLocaleLanguageCode]; + [languageCode getCString:code maxLength:3 encoding:NSASCIIStringEncoding]; + code[2]='\0'; + return code; +} + +Application::LanguageType Application::getCurrentLanguage() const +{ + // get the current language and country config + NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; + NSArray *languages = [defaults objectForKey:@"AppleLanguages"]; + NSString *currentLanguage = [languages objectAtIndex:0]; + + // get the current language code.(such as English is "en", Chinese is "zh" and so on) + NSDictionary* temp = [NSLocale componentsFromLocaleIdentifier:currentLanguage]; + NSString * languageCode = [temp objectForKey:NSLocaleLanguageCode]; + + if ([languageCode isEqualToString:@"zh"]) return LanguageType::CHINESE; + if ([languageCode isEqualToString:@"en"]) return LanguageType::ENGLISH; + if ([languageCode isEqualToString:@"fr"]) return LanguageType::FRENCH; + if ([languageCode isEqualToString:@"it"]) return LanguageType::ITALIAN; + if ([languageCode isEqualToString:@"de"]) return LanguageType::GERMAN; + if ([languageCode isEqualToString:@"es"]) return LanguageType::SPANISH; + if ([languageCode isEqualToString:@"nl"]) return LanguageType::DUTCH; + if ([languageCode isEqualToString:@"ru"]) return LanguageType::RUSSIAN; + if ([languageCode isEqualToString:@"ko"]) return LanguageType::KOREAN; + if ([languageCode isEqualToString:@"ja"]) return LanguageType::JAPANESE; + if ([languageCode isEqualToString:@"hu"]) return LanguageType::HUNGARIAN; + if ([languageCode isEqualToString:@"pt"]) return LanguageType::PORTUGUESE; + if ([languageCode isEqualToString:@"ar"]) return LanguageType::ARABIC; + if ([languageCode isEqualToString:@"nb"]) return LanguageType::NORWEGIAN; + if ([languageCode isEqualToString:@"pl"]) return LanguageType::POLISH; + if ([languageCode isEqualToString:@"tr"]) return LanguageType::TURKISH; + if ([languageCode isEqualToString:@"uk"]) return LanguageType::UKRAINIAN; + if ([languageCode isEqualToString:@"ro"]) return LanguageType::ROMANIAN; + if ([languageCode isEqualToString:@"bg"]) return LanguageType::BULGARIAN; + return LanguageType::ENGLISH; + +} + +bool Application::openURL(const std::string &url) +{ + NSString* msg = [NSString stringWithCString:url.c_str() encoding:NSUTF8StringEncoding]; + NSURL* nsUrl = [NSURL URLWithString:msg]; + return [[NSWorkspace sharedWorkspace] openURL:nsUrl]; +} + +bool Application::applicationDidFinishLaunching() +{ + return true; +} + +void Application::applicationDidEnterBackground() +{ +} + +void Application::applicationWillEnterForeground() +{ +} + +void Application::setMultitouch(bool) +{ +} + +void Application::onCreateView(int&x, int& y, int& width, int& height, int& rBits, int& gBits, int& bBits, int& aBits, int& depthBits, int& stencilBits, int& multisamplingCount) +{ + x = 0; + y = 0; + width = 960; + height = 640; + + rBits = 8; + gBits = 8; + bBits = 8; + aBits = 8; + depthBits = 16; + stencilBits = 8; + multisamplingCount = 0; +} + +void Application::createView(const std::string& name) +{ + int x = 0; + int y = 0; + int width = 0; + int height = 0; + int r = 0; + int g = 0; + int b = 0; + int a = 0; + int depth = 0; + int stencil = 0; + int multisamplingCount = 0; + + onCreateView(x, + y, + width, + height, + r, g, b, a, depth, stencil, multisamplingCount); + + _view = new GLView(this, name, x, y, width, height, r, g, b, a, depth, stencil, multisamplingCount); +} + +NS_CC_END diff --git a/native/cocos/scripting/js-bindings/event/EventDispatch.cpp b/native/cocos/scripting/js-bindings/event/EventDispatch.cpp index 4a9412216c..da7fde2f74 100644 --- a/native/cocos/scripting/js-bindings/event/EventDispatch.cpp +++ b/native/cocos/scripting/js-bindings/event/EventDispatch.cpp @@ -27,7 +27,11 @@ namespace cocos2d { -void EventDispatch::dispatchTouchEvent(const struct TouchEvent& touch) +void EventDispatch::dispatchTouchEvent(const struct TouchEvent& touchEvent) +{ +} + +void EventDispatch::dispatchKeyEvent(int key, int action) { } diff --git a/native/cocos/scripting/js-bindings/event/EventDispatch.h b/native/cocos/scripting/js-bindings/event/EventDispatch.h index b65cc489ea..2a19dbe7d2 100644 --- a/native/cocos/scripting/js-bindings/event/EventDispatch.h +++ b/native/cocos/scripting/js-bindings/event/EventDispatch.h @@ -55,7 +55,8 @@ struct TouchEvent class EventDispatch { public: - static void dispatchTouchEvent(const struct TouchEvent& touch); + static void dispatchTouchEvent(const struct TouchEvent& touchEvent); + static void dispatchKeyEvent(int key, int action); static void dispatchTicket(); }; diff --git a/native/templates/js-template-link/frameworks/runtime-src/Classes/AppDelegate.cpp b/native/templates/js-template-link/frameworks/runtime-src/Classes/AppDelegate.cpp index cf5415ec2c..21913f4e6b 100755 --- a/native/templates/js-template-link/frameworks/runtime-src/Classes/AppDelegate.cpp +++ b/native/templates/js-template-link/frameworks/runtime-src/Classes/AppDelegate.cpp @@ -41,7 +41,7 @@ using namespace anysdk::framework; USING_NS_CC; -AppDelegate::AppDelegate() +AppDelegate::AppDelegate() : Application("Creator game") { }