Fixing resolution handling for iOS and Android

1. Adding iPhone5 support
2. Changing Android resolution management based on DPI (Tablet mode or Phone mode)
3. Modifying distribution of xlarge,large,medium,small and xsmall assets to do it properly
4. Using kResolutionFixedWidth (kResolutionFixedHeight for landscape games) to properly calculate aspect ratio
5. Tested on all iOS devices and Nexus 4 (which is correctly identified as Retina phone)
This commit is contained in:
Rohan Kuruvilla 2013-04-23 18:03:54 -07:00
parent f95b802866
commit 7bd3f2a260
1 changed files with 228 additions and 184 deletions

View File

@ -12,6 +12,8 @@
#include "js_bindings_ccbreader.h"
#include "js_bindings_system_registration.h"
#include "jsb_opengl_registration.h"
#include "platform/CCDevice.h"
char *_js_log_buf_ccbuilder = NULL;
@ -57,7 +59,7 @@ void handle_ccb_run() {
}
void handle_connected() {
CCBHelper::setStatusMessage("Connected!");
CCBHelper::setStatusMessage("Connected!");
}
void resetCocosApp();
@ -72,12 +74,12 @@ void initViews(CCSize designSize) {
}
static void setViewValues(CCEGLView *pView, CCSize frameSize, CCSize designSize) {
//#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
// (CCScriptEngineManager::sharedManager())->removeScriptEngine();
// resetCocosApp();
//#endif
//
#if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
(CCScriptEngineManager::sharedManager())->removeScriptEngine();
resetCocosApp();
#endif
pView->setFrameSize(frameSize.height, frameSize.width);
pView->setDesignResolutionSize(designSize.height, designSize.width, kResolutionNoBorder);
CCLOG("Design Size %f x %f", designSize.height, designSize.width);
@ -85,7 +87,7 @@ static void setViewValues(CCEGLView *pView, CCSize frameSize, CCSize designSize)
}
void handle_set_orient(bool isPortrait) {
cocos2d::CCEGLView* pView = CCEGLView::sharedOpenGLView();
cocos2d::CCEGLView* pView = CCEGLView::sharedOpenGLView();
if (pView != NULL)
{
CCSize frameSize = pView->getFrameSize();
@ -108,7 +110,7 @@ void handle_set_status(const char* msg) {
}
void handle_disconnected() {
CCBHelper::setStatusMessage("Disconnected");
CCBHelper::setStatusMessage("Disconnected");
}
void handle_ccb_stop() {
@ -134,187 +136,229 @@ void cocos2d::CCLog(const char * pszFormat, ...)
#endif
extern "C" {
const char * getCCBDirectoryPath();
bool AppDelegate::applicationDidFinishLaunching()
{
// initialize director
CCDirector *pDirector = CCDirector::sharedDirector();
pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());
pDirector->setProjection(kCCDirectorProjection2D);
const char * getCCBDirectoryPath();
CCSize screenSize = CCEGLView::sharedOpenGLView()->getFrameSize();
CCSize designSize = CCSizeMake(320, 480);
CCSize resourceSize = CCSizeMake(320, 480);
std::vector<std::string> resDirOrders;
std::string res;
TargetPlatform platform = CCApplication::sharedApplication()->getTargetPlatform();
if (platform == kTargetIphone || platform == kTargetIpad)
{
std::vector<std::string> searchPaths = CCFileUtils::sharedFileUtils()->getSearchPaths();
searchPaths.insert(searchPaths.begin(), "Published files iOS");
searchPaths.insert(searchPaths.begin(), getCCBDirectoryPath());
CCFileUtils::sharedFileUtils()->setSearchPaths(searchPaths);
if (screenSize.height > 1024)
{
res = "iPad";
static void setResolutionSizes(bool isTablet, bool isRetina, bool isPortrait) {
CCSize designSize, resourceSize;
if(isTablet && isPortrait) {
designSize = CCSizeMake(768, 1024);
resourceSize = CCSizeMake(1536, 2048);
resDirOrders.push_back("resources-ipadhd");
resDirOrders.push_back("resources-ipad");
resDirOrders.push_back("resources-iphonehd");
isIPhone = false;
isRetina = true;
}
else if (screenSize.height > 960)
{
res = "iPad";
designSize = CCSizeMake(768, 1024);
resourceSize = CCSizeMake(768, 1024);
resDirOrders.push_back("resources-ipad");
resDirOrders.push_back("resources-iphonehd");
isIPhone = false;
isRetina = false;
}
else if (screenSize.height > 480)
{
res = "iPhone";
resourceSize = CCSizeMake(640, 960);
resDirOrders.push_back("resources-iphonehd");
resDirOrders.push_back("resources-iphone");
isIPhone = true;
isRetina = true;
}
else
{
res = "iPhone";
resourceSize = CCSizeMake(320, 480);
resDirOrders.push_back("resources-iphone");
isIPhone = true;
isRetina = false;
}
}
else if (platform == kTargetAndroid || platform == kTargetWindows)
{
if (screenSize.height > 960)
{
res = "xlarge";
resourceSize = CCSizeMake(1280, 1920);
resDirOrders.push_back("resources-xlarge");
resDirOrders.push_back("resources-large");
resDirOrders.push_back("resources-medium");
resDirOrders.push_back("resources-small");
}
else if (screenSize.height > 720)
{
res = "large";
resourceSize = CCSizeMake(640, 960);
resDirOrders.push_back("resources-large");
resDirOrders.push_back("resources-medium");
resDirOrders.push_back("resources-small");
}
else if (screenSize.height > 480)
{
res = "medium";
resourceSize = CCSizeMake(480, 720);
resDirOrders.push_back("resources-medium");
resDirOrders.push_back("resources-small");
}
else
{
res = "small";
resourceSize = CCSizeMake(320, 480);
resDirOrders.push_back("resources-small");
}
}
CCFileUtils *pFileUtils = CCFileUtils::sharedFileUtils();
pFileUtils->setSearchResolutionsOrder(resDirOrders);
pDirector->setContentScaleFactor(resourceSize.width/designSize.width);
CCEGLView::sharedOpenGLView()->setDesignResolutionSize(designSize.width, designSize.height, kResolutionNoBorder);
std::vector<std::string> searchPaths = pFileUtils->getSearchPaths();
searchPaths.insert(searchPaths.begin(), pFileUtils->getWritablePath());
pFileUtils->setSearchPaths(searchPaths);
PlayerStatus::setDeviceResolution(res);
// turn on display FPS
pDirector->setDisplayStats(true);
// set FPS. the default value is 1.0/60 if you don't call this
pDirector->setAnimationInterval(1.0 / 60);
ScriptingCore* sc = ScriptingCore::getInstance();
sc->addRegisterCallback(register_all_cocos2dx);
sc->addRegisterCallback(register_all_cocos2dx_extension);
sc->addRegisterCallback(register_cocos2dx_js_extensions);
sc->addRegisterCallback(register_all_cocos2dx_extension_manual);
sc->addRegisterCallback(register_CCBuilderReader);
sc->addRegisterCallback(jsb_register_system);
sc->addRegisterCallback(jsb_register_chipmunk);
sc->addRegisterCallback(JSB_register_opengl);
sc->start();
CCScriptEngineProtocol *pEngine = ScriptingCore::getInstance();
CCScriptEngineManager::sharedManager()->setScriptEngine(pEngine);
if(firstTime) {
runMainScene();
firstTime = false;
} else {
handle_ccb_run();
}
}
void handle_signal(int signal) {
static int internal_state = 0;
ScriptingCore* sc = ScriptingCore::getInstance();
// should start everything back
CCDirector* director = CCDirector::sharedDirector();
if (director->getRunningScene()) {
director->popToRootScene();
} else {
CCPoolManager::sharedPoolManager()->finalize();
if (internal_state == 0) {
//sc->dumpRoot(NULL, 0, NULL);
sc->start();
internal_state = 1;
if(isRetina) resourceSize = CCSizeMake(1536, 2048);
else resourceSize = CCSizeMake(768, 1024);
} else if(isTablet) {
designSize = CCSizeMake(1024, 768);
if(isRetina) resourceSize = CCSizeMake(2048, 1536);
else resourceSize = CCSizeMake(1024, 768);
} else if(isPortrait) {
designSize = CCSizeMake(320, 480);
if(isRetina) resourceSize = CCSizeMake(640, 960);
else resourceSize = CCSizeMake(320, 480);
} else {
sc->runScript("hello.js");
internal_state = 0;
designSize = CCSizeMake(480, 320);
if(isRetina) resourceSize = CCSizeMake(960, 640);
else resourceSize = CCSizeMake(480, 320);
}
CCDirector::sharedDirector()->setContentScaleFactor(resourceSize.width/designSize.width);
CCEGLView::sharedOpenGLView()->setDesignResolutionSize(designSize.width, designSize.height, kResolutionFixedWidth);
}
bool AppDelegate::applicationDidFinishLaunching()
{
// initialize director
CCDirector *pDirector = CCDirector::sharedDirector();
pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());
pDirector->setProjection(kCCDirectorProjection2D);
CCSize screenSize = CCEGLView::sharedOpenGLView()->getFrameSize();
std::vector<std::string> resDirOrders;
std::string res;
TargetPlatform platform = CCApplication::sharedApplication()->getTargetPlatform();
if (platform == kTargetIphone || platform == kTargetIpad)
{
std::vector<std::string> searchPaths = CCFileUtils::sharedFileUtils()->getSearchPaths();
searchPaths.insert(searchPaths.begin(), "Published files iOS");
searchPaths.insert(searchPaths.begin(), getCCBDirectoryPath());
CCFileUtils::sharedFileUtils()->setSearchPaths(searchPaths);
if (screenSize.height > 1136)
{
res = "iPad";
setResolutionSizes(true, true, true);
resDirOrders.push_back("resources-ipadhd");
resDirOrders.push_back("resources-ipad");
resDirOrders.push_back("resources-iphonehd");
isIPhone = false;
isRetina = true;
cocos2d::extension::CCBReader::setResolutionScale(2);
} else if(screenSize.height > 1024) {
res = "iPhone";
setResolutionSizes(false, true, true);
resDirOrders.push_back("resources-iphonehd");
resDirOrders.push_back("resources-iphone");
isIPhone = true;
isRetina = true;
}
else if (screenSize.height > 960)
{
res = "iPad";
setResolutionSizes(true, false, true);
resDirOrders.push_back("resources-ipad");
resDirOrders.push_back("resources-iphonehd");
isIPhone = false;
isRetina = false;
cocos2d::extension::CCBReader::setResolutionScale(2);
}
else if (screenSize.height > 480)
{
res = "iPhone";
setResolutionSizes(false, true, true);
resDirOrders.push_back("resources-iphonehd");
resDirOrders.push_back("resources-iphone");
isIPhone = true;
isRetina = true;
}
else
{
res = "iPhone";
setResolutionSizes(false, false, true);
resDirOrders.push_back("resources-iphone");
isIPhone = true;
isRetina = false;
}
}
else if (platform == kTargetAndroid || platform == kTargetWindows)
{
int dpi = -1;
dpi = CCDevice::getDPI();
if(dpi > 300) { // retina
if (screenSize.height > 1920) {
res = "xlarge";
setResolutionSizes(true, true, true);
resDirOrders.push_back("resources-xlarge");
resDirOrders.push_back("resources-large");
resDirOrders.push_back("resources-medium");
resDirOrders.push_back("resources-small");
} else {
res = "large";
setResolutionSizes(false, true, true);
resDirOrders.push_back("resources-large");
resDirOrders.push_back("resources-medium");
resDirOrders.push_back("resources-small");
}
} else { // non retina
if (screenSize.height > 960)
{
res = "large";
setResolutionSizes(true, false, true);
resDirOrders.push_back("resources-large");
resDirOrders.push_back("resources-medium");
resDirOrders.push_back("resources-small");
}
else if (screenSize.height > 768)
{
res = "medium";
setResolutionSizes(true, false, true);
resDirOrders.push_back("resources-medium");
resDirOrders.push_back("resources-small");
}
else if (screenSize.height > 480)
{
res = "small";
setResolutionSizes(false, false, true);
resDirOrders.push_back("resources-small");
}
else
{
setResolutionSizes(false, false, true);
res = "xsmall";
resDirOrders.push_back("resources-xsmall");
}
}
}
CCFileUtils *pFileUtils = CCFileUtils::sharedFileUtils();
pFileUtils->setSearchResolutionsOrder(resDirOrders);
std::vector<std::string> searchPaths = pFileUtils->getSearchPaths();
searchPaths.insert(searchPaths.begin(), pFileUtils->getWritablePath());
pFileUtils->setSearchPaths(searchPaths);
PlayerStatus::setDeviceResolution(res);
// turn on display FPS
pDirector->setDisplayStats(true);
// set FPS. the default value is 1.0/60 if you don't call this
pDirector->setAnimationInterval(1.0 / 60);
ScriptingCore* sc = ScriptingCore::getInstance();
sc->addRegisterCallback(register_all_cocos2dx);
sc->addRegisterCallback(register_all_cocos2dx_extension);
sc->addRegisterCallback(register_cocos2dx_js_extensions);
sc->addRegisterCallback(register_all_cocos2dx_extension_manual);
sc->addRegisterCallback(register_CCBuilderReader);
sc->addRegisterCallback(jsb_register_system);
sc->addRegisterCallback(jsb_register_chipmunk);
sc->addRegisterCallback(JSB_register_opengl);
sc->start();
CCScriptEngineProtocol *pEngine = ScriptingCore::getInstance();
CCScriptEngineManager::sharedManager()->setScriptEngine(pEngine);
if(firstTime) {
runMainScene();
firstTime = false;
} else {
handle_ccb_run();
}
}
}
// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground()
{
CCDirector::sharedDirector()->stopAnimation();
SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
SimpleAudioEngine::sharedEngine()->pauseAllEffects();
}
// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground()
{
CCDirector::sharedDirector()->startAnimation();
SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
SimpleAudioEngine::sharedEngine()->resumeAllEffects();
}
void handle_signal(int signal) {
static int internal_state = 0;
ScriptingCore* sc = ScriptingCore::getInstance();
// should start everything back
CCDirector* director = CCDirector::sharedDirector();
if (director->getRunningScene()) {
director->popToRootScene();
} else {
CCPoolManager::sharedPoolManager()->finalize();
if (internal_state == 0) {
//sc->dumpRoot(NULL, 0, NULL);
sc->start();
internal_state = 1;
} else {
sc->runScript("hello.js");
internal_state = 0;
}
}
}
// This function will be called when the app is inactive. When comes a phone call,it's be invoked too
void AppDelegate::applicationDidEnterBackground()
{
CCDirector::sharedDirector()->stopAnimation();
SimpleAudioEngine::sharedEngine()->pauseBackgroundMusic();
SimpleAudioEngine::sharedEngine()->pauseAllEffects();
}
// this function will be called when the app is active again
void AppDelegate::applicationWillEnterForeground()
{
CCDirector::sharedDirector()->startAnimation();
SimpleAudioEngine::sharedEngine()->resumeBackgroundMusic();
SimpleAudioEngine::sharedEngine()->resumeAllEffects();
}
}