diff --git a/cocos2dx/CCDirector.cpp b/cocos2dx/CCDirector.cpp index 80c9a6f60b..638111a0b5 100644 --- a/cocos2dx/CCDirector.cpp +++ b/cocos2dx/CCDirector.cpp @@ -131,7 +131,7 @@ bool CCDirector::init(void) // purge ? m_bPurgeDirecotorInNextLoop = false; - m_obWinSizeInPixels = m_obWinSizeInPoints = CCSizeZero; + m_obWinSizeInPoints = CCSizeZero; m_pobOpenGLView = NULL; @@ -301,8 +301,7 @@ void CCDirector::setOpenGLView(CCEGLView *pobOpenGLView) m_pobOpenGLView = pobOpenGLView; // set size - m_obWinSizeInPoints = m_pobOpenGLView->getSize(); - m_obWinSizeInPixels = CCSizeMake(m_obWinSizeInPoints.width * m_fContentScaleFactor, m_obWinSizeInPoints.height * m_fContentScaleFactor); + m_obWinSizeInPoints = m_pobOpenGLView->getDesignResolutionSize(); createStatsLabel(); @@ -454,7 +453,7 @@ CCSize CCDirector::getWinSize(void) CCSize CCDirector::getWinSizeInPixels() { - return m_obWinSizeInPixels; + return CCSizeMake(m_obWinSizeInPoints.width * m_fContentScaleFactor, m_obWinSizeInPoints.height * m_fContentScaleFactor); } CCSize CCDirector::getVisibleSize() @@ -481,20 +480,6 @@ CCPoint CCDirector::getVisibleOrigin() } } -void CCDirector::reshapeProjection(const CCSize& newWindowSize) -{ - CC_UNUSED_PARAM(newWindowSize); - if (m_pobOpenGLView) - { - m_obWinSizeInPoints = m_pobOpenGLView->getSize(); - m_obWinSizeInPixels = CCSizeMake(m_obWinSizeInPoints.width * m_fContentScaleFactor, - m_obWinSizeInPoints.height * m_fContentScaleFactor); - - setProjection(m_eProjection); - } - -} - // scene management void CCDirector::runWithScene(CCScene *pScene) @@ -775,7 +760,16 @@ void CCDirector::createStatsLabel() m_pDrawsLabel = new CCLabelAtlas(); m_pDrawsLabel->initWithString("000", "fps_images.png", 12, 32, '.'); */ - int fontSize = (int)(m_obWinSizeInPoints.height / 320.0f * 24); + int fontSize = 0; + if (m_obWinSizeInPoints.width > m_obWinSizeInPoints.height) + { + fontSize = (int)(m_obWinSizeInPoints.height / 320.0f * 24); + } + else + { + fontSize = (int)(m_obWinSizeInPoints.width / 320.0f * 24); + } + m_pFPSLabel = CCLabelTTF::create("00.0", "Arial", fontSize); m_pFPSLabel->retain(); m_pSPFLabel = CCLabelTTF::create("0.000", "Arial", fontSize); @@ -809,6 +803,7 @@ void CCDirector::setContentScaleFactor(float scaleFactor) if (scaleFactor != m_fContentScaleFactor) { m_fContentScaleFactor = scaleFactor; + createStatsLabel(); } } diff --git a/cocos2dx/CCDirector.h b/cocos2dx/CCDirector.h index 7b02fcb59b..1005003c77 100644 --- a/cocos2dx/CCDirector.h +++ b/cocos2dx/CCDirector.h @@ -180,9 +180,6 @@ public: */ CCPoint getVisibleOrigin(); - /** changes the projection size */ - void reshapeProjection(const CCSize& newWindowSize); - /** converts a UIKit coordinate to an OpenGL coordinate Useful to convert (multi) touches coordinates to the current layout (portrait or landscape) */ @@ -389,9 +386,6 @@ protected: /* window size in points */ CCSize m_obWinSizeInPoints; - - /* window size in pixels */ - CCSize m_obWinSizeInPixels; /* content scale factor */ float m_fContentScaleFactor; diff --git a/cocos2dx/platform/CCEGLViewProtocol.cpp b/cocos2dx/platform/CCEGLViewProtocol.cpp index d1e8d90772..3a1a25266a 100644 --- a/cocos2dx/platform/CCEGLViewProtocol.cpp +++ b/cocos2dx/platform/CCEGLViewProtocol.cpp @@ -88,13 +88,12 @@ void CCEGLViewProtocol::setDesignResolutionSize(float width, float height, Resol m_eResolutionPolicy = resolutionPolicy; // reset director's member variables to fit visible rect - CCDirector::sharedDirector()->m_obWinSizeInPoints = getSize(); - CCDirector::sharedDirector()->m_obWinSizeInPixels = CCSizeMake(m_obDesignResolutionSize.width*CC_CONTENT_SCALE_FACTOR(), m_obDesignResolutionSize.height*CC_CONTENT_SCALE_FACTOR()); + CCDirector::sharedDirector()->m_obWinSizeInPoints = getDesignResolutionSize(); CCDirector::sharedDirector()->createStatsLabel(); CCDirector::sharedDirector()->setGLDefaultValues(); } -const CCSize& CCEGLViewProtocol::getSize() const +const CCSize& CCEGLViewProtocol::getDesignResolutionSize() const { return m_obDesignResolutionSize; } diff --git a/cocos2dx/platform/CCEGLViewProtocol.h b/cocos2dx/platform/CCEGLViewProtocol.h index 0c710241c2..f76aa5161d 100644 --- a/cocos2dx/platform/CCEGLViewProtocol.h +++ b/cocos2dx/platform/CCEGLViewProtocol.h @@ -48,13 +48,6 @@ public: /** Open or close IME keyboard , subclass must implement this method. */ virtual void setIMEKeyboardState(bool bOpen) = 0; - /** - * Get design resolution size. - * If setDesignResolutionSize wasn't invoked, the result of this function return is the same as 'getFrameSize' - */ - - virtual const CCSize& getSize() const; - /** * Get the frame size of EGL view. * In general, it returns the screen size since the EGL view is a fullscreen view. @@ -78,16 +71,20 @@ public: /** * Set the design resolution size. - * Behavior undefined when enableRetina == true. * @param width Design resolution width. * @param height Design resolution height. * @param resolutionPolicy The resolution policy desired, you may choose: - * [1] kCCResolutionExactFit Fill screen by stretch-to-fit: if the design resolution ratio of width to height is different from the screen resolution ratio, your game view will be stretched. - * [2] kCCResolutionNoBorder Full screen without black border: if the design resolution ratio of width to height is different from the screen resolution ratio, two areas of your game view will be cut. - * [3] kCCResolutionShowAll Full screen with black border: if the design resolution ratio of width to height is different from the screen resolution ratio, two black borders will be shown. + * [1] kResolutionExactFit Fill screen by stretch-to-fit: if the design resolution ratio of width to height is different from the screen resolution ratio, your game view will be stretched. + * [2] kResolutionNoBorder Full screen without black border: if the design resolution ratio of width to height is different from the screen resolution ratio, two areas of your game view will be cut. + * [3] kResolutionShowAll Full screen with black border: if the design resolution ratio of width to height is different from the screen resolution ratio, two black borders will be shown. */ virtual void setDesignResolutionSize(float width, float height, ResolutionPolicy resolutionPolicy); + /** Get design resolution size. + * If setDesignResolutionSize wasn't invoked, the result of this function return is the same as 'getFrameSize' + */ + virtual const CCSize& getDesignResolutionSize() const; + /** Set touch delegate */ virtual void setTouchDelegate(EGLTouchDelegate * pDelegate); diff --git a/samples/HelloCpp/Classes/AppDelegate.cpp b/samples/HelloCpp/Classes/AppDelegate.cpp index 84e2527106..c3b597c085 100644 --- a/samples/HelloCpp/Classes/AppDelegate.cpp +++ b/samples/HelloCpp/Classes/AppDelegate.cpp @@ -14,30 +14,37 @@ AppDelegate::~AppDelegate() bool AppDelegate::applicationDidFinishLaunching() { // initialize director - CCDirector *pDirector = CCDirector::sharedDirector(); + CCDirector* pDirector = CCDirector::sharedDirector(); + CCEGLView* pEGLView = CCEGLView::sharedOpenGLView(); - pDirector->setOpenGLView(CCEGLView::sharedOpenGLView()); - //pDirector->setProjection(kCCDirectorProjection2D); - CCSize screenSize = CCEGLView::sharedOpenGLView()->getFrameSize(); - - if (screenSize.height > 768) - { - CCFileUtils::sharedFileUtils()->setResourceDirectory("ipadhd"); - pDirector->setContentScaleFactor(1536.0f/kDesignResolutionSize_height); + pDirector->setOpenGLView(pEGLView); + + // Set the design resolution + pEGLView->setDesignResolutionSize(designResolutionSize.width, designResolutionSize.height, kResolutionNoBorder); + + CCSize frameSize = pEGLView->getFrameSize(); + // if the frame size is larger than medium resource size, select large resource. + if (frameSize.height > mediumResource.size.height) + { + CCFileUtils::sharedFileUtils()->setResourceDirectory(largeResource.directory); + // The contentScaleFactor is set by the ratio of resource's height and design resolution's height when your game is in landscape. + // Oppositely, if your game is in portrait, it need to be set by width. + // This can make sure your game's status is in full screen. + pDirector->setContentScaleFactor(largeResource.size.height/designResolutionSize.height); } - else if (screenSize.height > 320) - { - CCFileUtils::sharedFileUtils()->setResourceDirectory("ipad"); - pDirector->setContentScaleFactor(768.0f/kDesignResolutionSize_height); + // if the frame size is larger than small resource size, select medium resource. + else if (frameSize.height > smallResource.size.height) + { + CCFileUtils::sharedFileUtils()->setResourceDirectory(mediumResource.directory); + pDirector->setContentScaleFactor(mediumResource.size.height/designResolutionSize.height); } + // if the frame size is smaller than medium resource size, select small resource. else - { - CCFileUtils::sharedFileUtils()->setResourceDirectory("iphone"); - pDirector->setContentScaleFactor(320.0f/kDesignResolutionSize_height); + { + CCFileUtils::sharedFileUtils()->setResourceDirectory(smallResource.directory); + pDirector->setContentScaleFactor(smallResource.size.height/designResolutionSize.height); } - CCEGLView::sharedOpenGLView()->setDesignResolutionSize(kDesignResolutionSize_width, kDesignResolutionSize_height, kResolutionNoBorder); - // turn on display FPS pDirector->setDisplayStats(true); diff --git a/samples/HelloCpp/Classes/AppMacros.h b/samples/HelloCpp/Classes/AppMacros.h index 355f25a76f..b9ca794821 100644 --- a/samples/HelloCpp/Classes/AppMacros.h +++ b/samples/HelloCpp/Classes/AppMacros.h @@ -1,30 +1,56 @@ #ifndef __APPMACROS_H__ #define __APPMACROS_H__ +#include "cocos2d.h" -#define kDesignResolution_480x320 0 -#define kDesignResolution_1024x768 1 -#define kDesignResolution_2048x1536 2 +/* For demonstrating using one design resolution to match different resources, + or one resource to match different design resolutions. -#define kTargetDesignResolutionSize kDesignResolution_2048x1536 + [Situation 1] Using the same design resolution to match different resources. + Please look into Appdelegate::applicationDidFinishLaunching. + We check current device frame size to decide which resource need to be selected. + So if you want to test this situation which said in title '[Situation 1]', + you should change ios simulator to different device(e.g. iphone, iphone-retina3.5, iphone-retina4.0, ipad, ipad-retina), + or change the window size in "proj.XXX/main.cpp" by "CCEGLView::setFrameSize" if you are using win32 or linux plaform + and modify "proj.mac/AppController.mm" by changing the window rectangle. -#if (kTargetDesignResolutionSize == kDesignResolution_480x320) -#define kDesignResolutionSize_width 480.0f -#define kDesignResolutionSize_height 320.0f + [Situation 2] Using the same resource to match different design resolutions. + The coordinates in your codes is based on your current design resolution rather than resource size. + Therefore, your design resolution could be very large and your resource could be small. + To test this, just define the marco 'TARGET_DESIGN_RESOLUTION_SIZE' to 'DESIGN_RESOLUTION_2048X1536' + and open iphone simulator or create a window of 480x320 size. + [Note] Normally, developer just need to define one design resolution(e.g. 960x640) with one or more resources. + */ -#elif (kTargetDesignResolutionSize == kDesignResolution_1024x768) -#define kDesignResolutionSize_width 1024.0f -#define kDesignResolutionSize_height 768.0f +#define DESIGN_RESOLUTION_480X320 0 +#define DESIGN_RESOLUTION_1024X768 1 +#define DESIGN_RESOLUTION_2048X1536 2 -#elif (kTargetDesignResolutionSize == kDesignResolution_2048x1536) -#define kDesignResolutionSize_width 2048.0f -#define kDesignResolutionSize_height 1536.0f +/* If you want to switch design resolution, change next line */ +#define TARGET_DESIGN_RESOLUTION_SIZE DESIGN_RESOLUTION_2048X1536 +typedef struct tagResource +{ + cocos2d::CCSize size; + char directory[100]; +}Resource; + +static Resource smallResource = { cocos2d::CCSizeMake(480, 320), "iphone" }; +static Resource mediumResource = { cocos2d::CCSizeMake(1024, 768), "ipad" }; +static Resource largeResource = { cocos2d::CCSizeMake(2048, 1536), "ipadhd" }; + +#if (TARGET_DESIGN_RESOLUTION_SIZE == DESIGN_RESOLUTION_480X320) +static cocos2d::CCSize designResolutionSize = cocos2d::CCSizeMake(480, 320); +#elif (TARGET_DESIGN_RESOLUTION_SIZE == DESIGN_RESOLUTION_1024X768) +static cocos2d::CCSize designResolutionSize = cocos2d::CCSizeMake(1024, 768); +#elif (TARGET_DESIGN_RESOLUTION_SIZE == DESIGN_RESOLUTION_2048X1536) +static cocos2d::CCSize designResolutionSize = cocos2d::CCSizeMake(2048, 1536); #else #error unknown target design resolution! #endif -#define kTitleFontSize (kDesignResolutionSize_width / 480.0f * 24) +// The font size 24 is designed for small resolution, so we should change it to fit for current design resolution +#define TITLE_FONT_SIZE (cocos2d::CCEGLView::sharedOpenGLView()->getDesignResolutionSize().width / smallResource.size.width * 24) #endif /* __APPMACROS_H__ */ diff --git a/samples/HelloCpp/Classes/HelloWorldScene.cpp b/samples/HelloCpp/Classes/HelloWorldScene.cpp index 20f7bc6767..94c515a196 100644 --- a/samples/HelloCpp/Classes/HelloWorldScene.cpp +++ b/samples/HelloCpp/Classes/HelloWorldScene.cpp @@ -56,7 +56,7 @@ bool HelloWorld::init() // add a label shows "Hello World" // create and initialize a label - CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", kTitleFontSize); + CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", TITLE_FONT_SIZE); // position the label on the center of the screen pLabel->setPosition(ccp(origin.x + visibleSize.width/2, diff --git a/samples/HelloCpp/proj.win32/HelloCpp.vcproj b/samples/HelloCpp/proj.win32/HelloCpp.vcproj index 3d9326ae5f..f854468dc9 100644 --- a/samples/HelloCpp/proj.win32/HelloCpp.vcproj +++ b/samples/HelloCpp/proj.win32/HelloCpp.vcproj @@ -190,6 +190,10 @@ RelativePath="..\Classes\AppDelegate.h" > + +