fixed #1520: Added comments for using multiresolution in HelloCpp.

This commit is contained in:
James Chen 2012-10-23 10:50:47 +08:00
parent 677ba22b5c
commit f4f63fd824
8 changed files with 94 additions and 72 deletions

View File

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

View File

@ -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)
*/
@ -390,9 +387,6 @@ protected:
/* window size in points */
CCSize m_obWinSizeInPoints;
/* window size in pixels */
CCSize m_obWinSizeInPixels;
/* content scale factor */
float m_fContentScaleFactor;

View File

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

View File

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

View File

@ -15,29 +15,36 @@ AppDelegate::~AppDelegate()
bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
CCDirector* pDirector = CCDirector::sharedDirector();
CCEGLView* pEGLView = CCEGLView::sharedOpenGLView();
pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());
//pDirector->setProjection(kCCDirectorProjection2D);
CCSize screenSize = CCEGLView::sharedOpenGLView()->getFrameSize();
pDirector->setOpenGLView(pEGLView);
if (screenSize.height > 768)
// 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("ipadhd");
pDirector->setContentScaleFactor(1536.0f/kDesignResolutionSize_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)
// if the frame size is larger than small resource size, select medium resource.
else if (frameSize.height > smallResource.size.height)
{
CCFileUtils::sharedFileUtils()->setResourceDirectory("ipad");
pDirector->setContentScaleFactor(768.0f/kDesignResolutionSize_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);

View File

@ -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__ */

View File

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

View File

@ -190,6 +190,10 @@
RelativePath="..\Classes\AppDelegate.h"
>
</File>
<File
RelativePath="..\Classes\AppMacros.h"
>
</File>
<File
RelativePath="..\Classes\HelloWorldScene.cpp"
>