Merge pull request #3997 from ondesly/screen_size_change

[ci skip]android & ios screen size change support
This commit is contained in:
minggo 2013-11-10 23:10:13 -08:00
commit cd22f5543b
8 changed files with 79 additions and 4 deletions

View File

@ -128,4 +128,8 @@ Application::Platform Application::getTargetPlatform()
return Platform::OS_ANDROID;
}
void Application::applicationScreenSizeChanged(int newWidth, int newHeight) {
}
NS_CC_END

View File

@ -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;
};

View File

@ -12,6 +12,7 @@
#include <android/configuration.h>
#include <pthread.h>
#include <chrono>
#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;
@ -558,6 +562,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
@ -586,6 +595,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) {
@ -673,5 +685,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<int, std::milli> duration(
std::chrono::duration_cast<std::chrono::duration<int, std::milli>>(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);
}
}
}
}

View File

@ -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;
};

View File

@ -146,4 +146,8 @@ Application::Platform Application::getTargetPlatform()
}
}
void Application::applicationScreenSizeChanged(int newWidth, int newHeight) {
}
NS_CC_END

View File

@ -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
{

View File

@ -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
{

View File

@ -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
{