diff --git a/cocos/2d/platform/android/CCApplication.cpp b/cocos/2d/platform/android/CCApplication.cpp index 61b3f288cd..07c5814b3e 100644 --- a/cocos/2d/platform/android/CCApplication.cpp +++ b/cocos/2d/platform/android/CCApplication.cpp @@ -128,4 +128,8 @@ Application::Platform Application::getTargetPlatform() return Platform::OS_ANDROID; } +void Application::applicationScreenSizeChanged(int newWidth, int newHeight) { + +} + NS_CC_END diff --git a/cocos/2d/platform/android/CCApplication.h b/cocos/2d/platform/android/CCApplication.h index e321266c81..d9b7d00f34 100644 --- a/cocos/2d/platform/android/CCApplication.h +++ b/cocos/2d/platform/android/CCApplication.h @@ -50,6 +50,13 @@ public: */ virtual Platform getTargetPlatform(); + /** + @brief This function will be called when the application screen size is changed. + @param new width + @param new height + */ + virtual void applicationScreenSizeChanged(int newWidth, int newHeight); + protected: static Application * sm_pSharedApplication; }; diff --git a/cocos/2d/platform/android/nativeactivity.cpp b/cocos/2d/platform/android/nativeactivity.cpp index fa4fb434d4..1cc60c406c 100644 --- a/cocos/2d/platform/android/nativeactivity.cpp +++ b/cocos/2d/platform/android/nativeactivity.cpp @@ -12,6 +12,7 @@ #include #include +#include #include "CCDirector.h" #include "CCApplication.h" @@ -69,6 +70,9 @@ struct engine { struct saved_state state; }; +static bool isContentRectChanged = false; +static std::chrono::steady_clock::time_point timeRectChanged; + static struct engine engine; static char* editboxText = NULL; @@ -556,6 +560,11 @@ static void engine_handle_cmd(struct android_app* app, int32_t cmd) { } } +static void onContentRectChanged(ANativeActivity* activity, const ARect* rect) { + timeRectChanged = std::chrono::steady_clock::now(); + isContentRectChanged = true; +} + /** * This is the main entry point of a native application that is using * android_native_app_glue. It runs in its own thread, with its own @@ -584,6 +593,9 @@ void android_main(struct android_app* state) { engine.state = *(struct saved_state*)state->savedState; } + // Screen size change support + state->activity->callbacks->onContentRectChanged = onContentRectChanged; + // loop waiting for stuff to do. while (1) { @@ -671,5 +683,20 @@ void android_main(struct android_app* state) { } else { LOG_RENDER_DEBUG("android_main : !engine.animating"); } + + // Check if screen size changed + if (isContentRectChanged) { + std::chrono::duration duration( + std::chrono::duration_cast>(std::chrono::steady_clock::now() - timeRectChanged)); + + // Wait about 30 ms to get new width and height. Without waiting we can get old values sometime + if (duration.count() > 30) { + isContentRectChanged = false; + + int32_t newWidth = ANativeWindow_getWidth(engine.app->window); + int32_t newHeight = ANativeWindow_getHeight(engine.app->window); + cocos2d::Application::getInstance()->applicationScreenSizeChanged(newWidth, newHeight); + } + } } } diff --git a/cocos/2d/platform/ios/CCApplication.h b/cocos/2d/platform/ios/CCApplication.h index 86be67bd29..a281a4f554 100644 --- a/cocos/2d/platform/ios/CCApplication.h +++ b/cocos/2d/platform/ios/CCApplication.h @@ -76,6 +76,13 @@ public: */ virtual Platform getTargetPlatform(); + /** + @brief This function will be called when the application screen size is changed. + @param new width + @param new height + */ + virtual void applicationScreenSizeChanged(int newWidth, int newHeight); + protected: static Application * sm_pSharedApplication; }; diff --git a/cocos/2d/platform/ios/CCApplication.mm b/cocos/2d/platform/ios/CCApplication.mm index 8c8d543c2e..c09a0aa8a9 100644 --- a/cocos/2d/platform/ios/CCApplication.mm +++ b/cocos/2d/platform/ios/CCApplication.mm @@ -146,4 +146,8 @@ Application::Platform Application::getTargetPlatform() } } +void Application::applicationScreenSizeChanged(int newWidth, int newHeight) { + +} + NS_CC_END diff --git a/template/multi-platform-cpp/proj.ios_mac/ios/RootViewController.mm b/template/multi-platform-cpp/proj.ios_mac/ios/RootViewController.mm index 906aeaf082..187b274b91 100644 --- a/template/multi-platform-cpp/proj.ios_mac/ios/RootViewController.mm +++ b/template/multi-platform-cpp/proj.ios_mac/ios/RootViewController.mm @@ -1,5 +1,6 @@ #import "RootViewController.h" - +#import "cocos2d.h" +#import "EAGLView.h" @implementation RootViewController @@ -43,6 +44,14 @@ return YES; } +- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { + [super didRotateFromInterfaceOrientation:fromInterfaceOrientation]; + + CGSize s = CGSizeMake([[CCEAGLView sharedEGLView] getWidth], [[CCEAGLView sharedEGLView] getHeight]); + + cocos2d::Application::getInstance()->applicationScreenSizeChanged((int) s.width, (int) s.height); +} + //fix not hide status on ios7 - (BOOL)prefersStatusBarHidden { diff --git a/template/multi-platform-js/proj.ios_mac/ios/RootViewController.mm b/template/multi-platform-js/proj.ios_mac/ios/RootViewController.mm index a0f626dcd6..187b274b91 100644 --- a/template/multi-platform-js/proj.ios_mac/ios/RootViewController.mm +++ b/template/multi-platform-js/proj.ios_mac/ios/RootViewController.mm @@ -1,6 +1,6 @@ - #import "RootViewController.h" - +#import "cocos2d.h" +#import "EAGLView.h" @implementation RootViewController @@ -44,6 +44,14 @@ return YES; } +- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { + [super didRotateFromInterfaceOrientation:fromInterfaceOrientation]; + + CGSize s = CGSizeMake([[CCEAGLView sharedEGLView] getWidth], [[CCEAGLView sharedEGLView] getHeight]); + + cocos2d::Application::getInstance()->applicationScreenSizeChanged((int) s.width, (int) s.height); +} + //fix not hide status on ios7 - (BOOL)prefersStatusBarHidden { diff --git a/template/multi-platform-lua/proj.ios_mac/ios/RootViewController.mm b/template/multi-platform-lua/proj.ios_mac/ios/RootViewController.mm index a00da00584..093a9b4c3a 100644 --- a/template/multi-platform-lua/proj.ios_mac/ios/RootViewController.mm +++ b/template/multi-platform-lua/proj.ios_mac/ios/RootViewController.mm @@ -24,7 +24,8 @@ ****************************************************************************/ #import "RootViewController.h" - +#import "cocos2d.h" +#import "EAGLView.h" @implementation RootViewController @@ -68,6 +69,14 @@ return YES; } +- (void)didRotateFromInterfaceOrientation:(UIInterfaceOrientation)fromInterfaceOrientation { + [super didRotateFromInterfaceOrientation:fromInterfaceOrientation]; + + CGSize s = CGSizeMake([[CCEAGLView sharedEGLView] getWidth], [[CCEAGLView sharedEGLView] getHeight]); + + cocos2d::Application::getInstance()->applicationScreenSizeChanged((int) s.width, (int) s.height); +} + //fix not hide status on ios7 - (BOOL)prefersStatusBarHidden {