diff --git a/cocos/platform/CCApplication.h b/cocos/platform/CCApplication.h index a517294edaa..6b74d075727 100644 --- a/cocos/platform/CCApplication.h +++ b/cocos/platform/CCApplication.h @@ -137,6 +137,9 @@ class CC_DLL Application @return Current language iso 639-1 code. */ std::string getCurrentLanguageCode() const; + + const cocos2d::Vec2& getViewSize() const; + void updateViewSize(int width, int height); /** @brief Get current display stats. @@ -204,6 +207,8 @@ class CC_DLL Application bool _multiTouch = false; bool _isStarted = false; bool _isDownsampleEnabled = false; + + cocos2d::Vec2 _viewSize; }; // end of platform group diff --git a/cocos/platform/android/CCApplication-android.cpp b/cocos/platform/android/CCApplication-android.cpp index 0e27dfa1267..0c2765782e6 100644 --- a/cocos/platform/android/CCApplication-android.cpp +++ b/cocos/platform/android/CCApplication-android.cpp @@ -26,6 +26,7 @@ THE SOFTWARE. #include "platform/CCApplication.h" #include #include +#include #include "platform/android/jni/JniImp.h" #include "platform/android/CCGL-android.h" #include "base/CCScheduler.h" @@ -52,6 +53,22 @@ PFNGLDELETEVERTEXARRAYSOESPROC glDeleteVertexArraysOESEXT = 0; NS_CC_BEGIN +void Application::updateViewSize(int width, int height) +{ + _viewSize.x = width; + _viewSize.y = height; +} + +extern "C" { + void Java_org_cocos2dx_lib_Cocos2dxGLSurfaceView_nativeOnSizeChanged(JNIEnv * env, jobject obj, jint width, jint height) { + auto inst = Application::getInstance(); + // nativeOnSizeChanged is firstly called before Application initiating. + if (inst != nullptr) { + inst->updateViewSize(width, height); + } + } +} + Application* Application::_instance = nullptr; std::shared_ptr Application::_scheduler = nullptr; @@ -67,6 +84,7 @@ Application::Application(const std::string& name, int width, int height) PFNGLDELETEVERTEXARRAYSOESPROC glDeleteVertexArraysOESEXT = (PFNGLDELETEVERTEXARRAYSOESPROC)eglGetProcAddress("glDeleteVertexArraysOES"); _renderTexture = new RenderTexture(width, height); + updateViewSize(width, height); } Application::~Application() @@ -266,4 +284,9 @@ std::string Application::getSystemVersion() return getSystemVersionJNI(); } +const cocos2d::Vec2& Application::getViewSize() const +{ + return _viewSize; +} + NS_CC_END diff --git a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java index 1a4bd06bebb..e4683a12a0f 100644 --- a/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java +++ b/cocos/platform/android/java/src/org/cocos2dx/lib/Cocos2dxGLSurfaceView.java @@ -54,6 +54,8 @@ public class Cocos2dxGLSurfaceView extends GLSurfaceView { private Cocos2dxRenderer mCocos2dxRenderer; private boolean mStopHandleTouchAndKeyEvents = false; + public static native void nativeOnSizeChanged(int width, int height); + // =========================================================== // Constructors // =========================================================== @@ -250,6 +252,7 @@ public void run() { protected void onSizeChanged(final int pNewSurfaceWidth, final int pNewSurfaceHeight, final int pOldSurfaceWidth, final int pOldSurfaceHeight) { if(!this.isInEditMode()) { this.mCocos2dxRenderer.setScreenWidthAndHeight(pNewSurfaceWidth, pNewSurfaceHeight); + nativeOnSizeChanged(pNewSurfaceWidth, pNewSurfaceHeight); } } diff --git a/cocos/platform/desktop/CCGLView-desktop.cpp b/cocos/platform/desktop/CCGLView-desktop.cpp index c0b1127f75c..ca68eb817f3 100644 --- a/cocos/platform/desktop/CCGLView-desktop.cpp +++ b/cocos/platform/desktop/CCGLView-desktop.cpp @@ -184,6 +184,7 @@ GLView::GLView(Application* application, const std::string& name, int x, int y, computeScale(); glGetIntegerv(GL_FRAMEBUFFER_BINDING, &_mainFBO); + Application::getInstance()->updateViewSize(width, height); } GLView::~GLView() @@ -443,6 +444,7 @@ void GLView::onGLFWWindowIconifyCallback(GLFWwindow* /*window*/, int iconified) void GLView::onGLFWWindowSizeFunCallback(GLFWwindow *window, int width, int height) { + Application::getInstance()->updateViewSize(width, height); EventDispatcher::dispatchResizeEvent(width, height); } diff --git a/cocos/platform/ios/CCApplication-ios.mm b/cocos/platform/ios/CCApplication-ios.mm index 3b97e2f741f..1f44813a28a 100644 --- a/cocos/platform/ios/CCApplication-ios.mm +++ b/cocos/platform/ios/CCApplication-ios.mm @@ -39,27 +39,17 @@ of this software and associated documentation files (the "Software"), to deal namespace { - cocos2d::Vec2 getResolution() - { - CGRect bounds = [UIScreen mainScreen].bounds; - float scale = [[UIScreen mainScreen] scale]; - float width = bounds.size.width * scale; - float height = bounds.size.height * scale; - - return cocos2d::Vec2(width, height); - } - bool setCanvasCallback(se::Object* global) { - cocos2d::Vec2 resolution = getResolution(); + auto &viewSize = cocos2d::Application::getInstance()->getViewSize(); se::ScriptEngine* se = se::ScriptEngine::getInstance(); uint8_t devicePixelRatio = cocos2d::Application::getInstance()->getDevicePixelRatio(); char commandBuf[200] = {0}; sprintf(commandBuf, "window.innerWidth = %d; window.innerHeight = %d;", - (int)(resolution.x / devicePixelRatio), - (int)(resolution.y / devicePixelRatio)); + (int)(viewSize.x / devicePixelRatio), + (int)(viewSize.y / devicePixelRatio)); se->evalString(commandBuf); - cocos2d::ccViewport(0, 0, resolution.x / devicePixelRatio, resolution.y / devicePixelRatio); + cocos2d::ccViewport(0, 0, viewSize.x / devicePixelRatio, viewSize.y / devicePixelRatio); glDepthMask(GL_TRUE); return true; } @@ -219,6 +209,8 @@ -(void) doCaller: (id) sender EventDispatcher::init(); _delegate = [[MainLoop alloc] initWithApplication:this]; + + updateViewSize(width, height); } Application::~Application() @@ -245,6 +237,17 @@ -(void) doCaller: (id) sender Application::_instance = nullptr; } +const cocos2d::Vec2& Application::getViewSize() const +{ + return _viewSize; +} + +void Application::updateViewSize(int width, int height) +{ + _viewSize.x = width; + _viewSize.y = height; +} + void Application::start() { if (_delegate) diff --git a/cocos/platform/mac/CCApplication-mac.mm b/cocos/platform/mac/CCApplication-mac.mm index d81d0f36fca..bf429d8ce25 100644 --- a/cocos/platform/mac/CCApplication-mac.mm +++ b/cocos/platform/mac/CCApplication-mac.mm @@ -100,6 +100,17 @@ bool setCanvasCallback(se::Object* global) Application::_instance = nullptr; } +const cocos2d::Vec2& Application::getViewSize() const +{ + return _viewSize; +} + +void Application::updateViewSize(int width, int height) +{ + _viewSize.x = width; + _viewSize.y = height; +} + void Application::start() { if (!_view) diff --git a/cocos/platform/win32/CCApplication-win32.cpp b/cocos/platform/win32/CCApplication-win32.cpp index df3dc26e64d..a6d2dd1d789 100644 --- a/cocos/platform/win32/CCApplication-win32.cpp +++ b/cocos/platform/win32/CCApplication-win32.cpp @@ -134,6 +134,17 @@ Application::~Application() Application::_instance = nullptr; } +const cocos2d::Vec2& Application::getViewSize() const +{ + return _viewSize; +} + +void Application::updateViewSize(int width, int height) +{ + _viewSize.x = width; + _viewSize.y = height; +} + void Application::start() { if (!_view) diff --git a/cocos/renderer/renderer/ForwardRenderer.cpp b/cocos/renderer/renderer/ForwardRenderer.cpp index 5bee83b78f0..6cbe76e4a58 100644 --- a/cocos/renderer/renderer/ForwardRenderer.cpp +++ b/cocos/renderer/renderer/ForwardRenderer.cpp @@ -39,6 +39,8 @@ #include "Light.h" #include +#include "CCApplication.h" + #include "math/MathUtil.h" RENDERER_BEGIN @@ -72,8 +74,6 @@ ForwardRenderer::~ForwardRenderer() bool ForwardRenderer::init(DeviceGraphics* device, std::vector& programTemplates, Texture2D* defaultTexture, int width, int height) { BaseRenderer::init(device, programTemplates, defaultTexture); - _width = width; - _height = height; registerStage("opaque", std::bind(&ForwardRenderer::opaqueStage, this, std::placeholders::_1, std::placeholders::_2)); registerStage("shadowcast", std::bind(&ForwardRenderer::shadowStage, this, std::placeholders::_1, std::placeholders::_2)); registerStage("transparent", std::bind(&ForwardRenderer::transparentStage, this, std::placeholders::_1, std::placeholders::_2)); @@ -92,10 +92,11 @@ void ForwardRenderer::render(Scene* scene) updateLights(scene); scene->sortCameras(); auto& cameras = scene->getCameras(); + auto &viewSize = Application::getInstance()->getViewSize(); for (auto& camera : cameras) { View* view = requestView(); - camera->extractView(*view, _width, _height); + camera->extractView(*view, viewSize.x, viewSize.y); } for (size_t i = 0, len = _views->getLength(); i < len; ++i) { @@ -109,8 +110,9 @@ void ForwardRenderer::render(Scene* scene) void ForwardRenderer::renderCamera(Camera* camera, Scene* scene) { reset(); - int width = _width; - int height = _height; + auto &viewSize = Application::getInstance()->getViewSize(); + int width = viewSize.x; + int height = viewSize.y; FrameBuffer* fb = camera->getFrameBuffer(); if (nullptr != fb) { width = fb->getWidth(); diff --git a/cocos/renderer/renderer/ForwardRenderer.h b/cocos/renderer/renderer/ForwardRenderer.h index 78aacc61dd1..0b0832103a3 100644 --- a/cocos/renderer/renderer/ForwardRenderer.h +++ b/cocos/renderer/renderer/ForwardRenderer.h @@ -85,9 +85,6 @@ class ForwardRenderer final : public BaseRenderer Vector _ambientLights; RecyclePool* _arrayPool = nullptr; - - int _width = 0; - int _height = 0; std::size_t _numLights = 0; }; diff --git a/templates/js-template-default/frameworks/runtime-src/proj.ios_mac/ios/AppController.mm b/templates/js-template-default/frameworks/runtime-src/proj.ios_mac/ios/AppController.mm index f1c4ca4819c..b64736adb22 100755 --- a/templates/js-template-default/frameworks/runtime-src/proj.ios_mac/ios/AppController.mm +++ b/templates/js-template-default/frameworks/runtime-src/proj.ios_mac/ios/AppController.mm @@ -78,6 +78,9 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( [window makeKeyAndVisible]; [[UIApplication sharedApplication] setStatusBarHidden:YES]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(statusBarOrientationChanged:) + name:UIApplicationDidChangeStatusBarOrientationNotification object:nil]; //run the cocos2d-x game scene app->start(); @@ -85,6 +88,14 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( return YES; } +- (void)statusBarOrientationChanged:(NSNotification *)notification { + CGRect bounds = [UIScreen mainScreen].bounds; + float scale = [[UIScreen mainScreen] scale]; + float width = bounds.size.width * scale; + float height = bounds.size.height * scale; + Application::getInstance()->updateViewSize(width, height); +} + - (void)applicationWillResignActive:(UIApplication *)application { /* Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. diff --git a/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/ios/AppController.mm b/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/ios/AppController.mm index f1c4ca4819c..b64736adb22 100755 --- a/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/ios/AppController.mm +++ b/templates/js-template-link/frameworks/runtime-src/proj.ios_mac/ios/AppController.mm @@ -78,6 +78,9 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( [window makeKeyAndVisible]; [[UIApplication sharedApplication] setStatusBarHidden:YES]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(statusBarOrientationChanged:) + name:UIApplicationDidChangeStatusBarOrientationNotification object:nil]; //run the cocos2d-x game scene app->start(); @@ -85,6 +88,14 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( return YES; } +- (void)statusBarOrientationChanged:(NSNotification *)notification { + CGRect bounds = [UIScreen mainScreen].bounds; + float scale = [[UIScreen mainScreen] scale]; + float width = bounds.size.width * scale; + float height = bounds.size.height * scale; + Application::getInstance()->updateViewSize(width, height); +} + - (void)applicationWillResignActive:(UIApplication *)application { /* Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state. diff --git a/tools/simulator/frameworks/runtime-src/proj.ios_mac/ios/AppController.mm b/tools/simulator/frameworks/runtime-src/proj.ios_mac/ios/AppController.mm index 733ef1f5952..a9d7fea027f 100755 --- a/tools/simulator/frameworks/runtime-src/proj.ios_mac/ios/AppController.mm +++ b/tools/simulator/frameworks/runtime-src/proj.ios_mac/ios/AppController.mm @@ -71,11 +71,22 @@ - (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:( [window makeKeyAndVisible]; [[UIApplication sharedApplication] setStatusBarHidden: YES]; + [[NSNotificationCenter defaultCenter] addObserver:self + selector:@selector(statusBarOrientationChanged:) + name:UIApplicationDidChangeStatusBarOrientationNotification object:nil]; app->start(); return YES; } +- (void)statusBarOrientationChanged:(NSNotification *)notification { + CGRect bounds = [UIScreen mainScreen].bounds; + float scale = [[UIScreen mainScreen] scale]; + float width = bounds.size.width * scale; + float height = bounds.size.height * scale; + Application::getInstance()->updateViewSize(width, height); +} + - (void)applicationWillResignActive:(UIApplication *)application { /*