Merge branch 'multi_resolution_support' of https://github.com/BorMor/cocos2d-x into bormor-multi-res

Conflicts:
	samples/HelloCpp/Classes/AppDelegate.cpp
	samples/HelloCpp/Classes/HelloWorldScene.cpp
This commit is contained in:
James Chen 2012-10-11 16:12:22 +08:00
commit ade54a3de7
9 changed files with 56 additions and 197 deletions

View File

@ -336,7 +336,7 @@ void CCDirector::setProjection(ccDirectorProjection kProjection)
if (m_pobOpenGLView) if (m_pobOpenGLView)
{ {
m_pobOpenGLView->setViewPortInPoints(0, 0, sizePoint.width, sizePoint.height); m_pobOpenGLView->setViewPortInPoints(0, 0, size.width, size.height);
} }
switch (kProjection) switch (kProjection)
@ -346,7 +346,7 @@ void CCDirector::setProjection(ccDirectorProjection kProjection)
kmGLMatrixMode(KM_GL_PROJECTION); kmGLMatrixMode(KM_GL_PROJECTION);
kmGLLoadIdentity(); kmGLLoadIdentity();
kmMat4 orthoMatrix; kmMat4 orthoMatrix;
kmMat4OrthographicProjection(&orthoMatrix, 0, size.width / CC_CONTENT_SCALE_FACTOR(), 0, size.height / CC_CONTENT_SCALE_FACTOR(), -1024, 1024 ); kmMat4OrthographicProjection(&orthoMatrix, 0, size.width, 0, size.height, -1024, 1024 );
kmGLMultMatrix(&orthoMatrix); kmGLMultMatrix(&orthoMatrix);
kmGLMatrixMode(KM_GL_MODELVIEW); kmGLMatrixMode(KM_GL_MODELVIEW);
kmGLLoadIdentity(); kmGLLoadIdentity();
@ -371,8 +371,8 @@ void CCDirector::setProjection(ccDirectorProjection kProjection)
kmGLMatrixMode(KM_GL_MODELVIEW); kmGLMatrixMode(KM_GL_MODELVIEW);
kmGLLoadIdentity(); kmGLLoadIdentity();
kmVec3 eye, center, up; kmVec3 eye, center, up;
kmVec3Fill( &eye, sizePoint.width/2, sizePoint.height/2, zeye ); kmVec3Fill( &eye, size.width/2, size.height/2, zeye );
kmVec3Fill( &center, sizePoint.width/2, sizePoint.height/2, 0.0f ); kmVec3Fill( &center, size.width/2, size.height/2, 0.0f );
kmVec3Fill( &up, 0.0f, 1.0f, 0.0f); kmVec3Fill( &up, 0.0f, 1.0f, 0.0f);
kmMat4LookAt(&matrixLookup, &eye, &center, &up); kmMat4LookAt(&matrixLookup, &eye, &center, &up);
kmGLMultMatrix(&matrixLookup); kmGLMultMatrix(&matrixLookup);
@ -404,7 +404,7 @@ void CCDirector::purgeCachedData(void)
float CCDirector::getZEye(void) float CCDirector::getZEye(void)
{ {
return (m_obWinSizeInPixels.height / 1.1566f / CC_CONTENT_SCALE_FACTOR()); return (m_obWinSizeInPixels.height / 1.1566f);
} }
void CCDirector::setAlphaBlending(bool bOn) void CCDirector::setAlphaBlending(bool bOn)
@ -810,33 +810,6 @@ void CCDirector::updateContentScaleFactor()
m_bIsContentScaleSupported = m_pobOpenGLView->setContentScaleFactor(m_fContentScaleFactor); m_bIsContentScaleSupported = m_pobOpenGLView->setContentScaleFactor(m_fContentScaleFactor);
} }
bool CCDirector::enableRetinaDisplay(bool enabled)
{
// Already enabled?
if (enabled && m_fContentScaleFactor == 2)
{
return true;
}
// Already disabled?
if (!enabled && m_fContentScaleFactor == 1)
{
return false;
}
if (! m_pobOpenGLView->enableRetina())
{
return false;
}
float newScale = (float)(enabled ? 2 : 1);
setContentScaleFactor(newScale);
createStatsLabel();
return true;
}
float CCDirector::getContentScaleFactor(void) float CCDirector::getContentScaleFactor(void)
{ {
return m_fContentScaleFactor; return m_fContentScaleFactor;

View File

@ -160,8 +160,6 @@ public:
CCNode* getNotificationNode(); CCNode* getNotificationNode();
void setNotificationNode(CCNode *node); void setNotificationNode(CCNode *node);
bool enableRetinaDisplay(bool bEnabelRetina);
// window size // window size
/** returns the size of the OpenGL view in points. /** returns the size of the OpenGL view in points.

View File

@ -46,7 +46,6 @@ CCEGLViewProtocol::CCEGLViewProtocol()
: m_pDelegate(NULL) : m_pDelegate(NULL)
, m_fScaleY(1.0f) , m_fScaleY(1.0f)
, m_fScaleX(1.0f) , m_fScaleX(1.0f)
, m_bIsRetinaEnabled(false)
, m_eResolutionPolicy(kResolutionUnKnown) , m_eResolutionPolicy(kResolutionUnKnown)
{ {
} }
@ -58,7 +57,6 @@ CCEGLViewProtocol::~CCEGLViewProtocol()
void CCEGLViewProtocol::setDesignResolutionSize(float width, float height, ResolutionPolicy resolutionPolicy) void CCEGLViewProtocol::setDesignResolutionSize(float width, float height, ResolutionPolicy resolutionPolicy)
{ {
CCAssert(m_bIsRetinaEnabled == false, "can not enable retina while set design resolution size!");
CCAssert(resolutionPolicy != kResolutionUnKnown, "should set resolutionPolicy"); CCAssert(resolutionPolicy != kResolutionUnKnown, "should set resolutionPolicy");
if (width == 0.0f || height == 0.0f) if (width == 0.0f || height == 0.0f)
@ -97,11 +95,6 @@ void CCEGLViewProtocol::setDesignResolutionSize(float width, float height, Resol
CCDirector::sharedDirector()->setGLDefaultValues(); CCDirector::sharedDirector()->setGLDefaultValues();
} }
bool CCEGLViewProtocol::enableRetina()
{
return false;
}
const CCSize& CCEGLViewProtocol::getSize() const const CCSize& CCEGLViewProtocol::getSize() const
{ {
return m_obDesignResolutionSize; return m_obDesignResolutionSize;
@ -193,18 +186,8 @@ void CCEGLViewProtocol::handleTouchesBegin(int num, int ids[], float xs[], float
} }
CCTouch* pTouch = s_pTouches[nUnusedIndex] = new CCTouch(); CCTouch* pTouch = s_pTouches[nUnusedIndex] = new CCTouch();
if (m_bIsRetinaEnabled)
{
// on iOS, though retina is enabled, the value got from os is also
// relative to its original size
pTouch->setTouchInfo(nUnusedIndex, (x - m_obViewPortRect.origin.x),
(y - m_obViewPortRect.origin.y));
}
else
{
pTouch->setTouchInfo(nUnusedIndex, (x - m_obViewPortRect.origin.x) / m_fScaleX, pTouch->setTouchInfo(nUnusedIndex, (x - m_obViewPortRect.origin.x) / m_fScaleX,
(y - m_obViewPortRect.origin.y) / m_fScaleY); (y - m_obViewPortRect.origin.y) / m_fScaleY);
}
//CCLOG("x = %f y = %f", pTouch->getLocationInView().x, pTouch->getLocationInView().y); //CCLOG("x = %f y = %f", pTouch->getLocationInView().x, pTouch->getLocationInView().y);
@ -242,17 +225,9 @@ void CCEGLViewProtocol::handleTouchesMove(int num, int ids[], float xs[], float
CCLOGINFO("Moving touches with id: %d, x=%f, y=%f", id, x, y); CCLOGINFO("Moving touches with id: %d, x=%f, y=%f", id, x, y);
CCTouch* pTouch = s_pTouches[pIndex->getValue()]; CCTouch* pTouch = s_pTouches[pIndex->getValue()];
if (pTouch) if (pTouch)
{
if (m_bIsRetinaEnabled)
{
pTouch->setTouchInfo(pIndex->getValue(), (x - m_obViewPortRect.origin.x),
(y - m_obViewPortRect.origin.y));
}
else
{ {
pTouch->setTouchInfo(pIndex->getValue(), (x - m_obViewPortRect.origin.x) / m_fScaleX, pTouch->setTouchInfo(pIndex->getValue(), (x - m_obViewPortRect.origin.x) / m_fScaleX,
(y - m_obViewPortRect.origin.y) / m_fScaleY); (y - m_obViewPortRect.origin.y) / m_fScaleY);
}
set.addObject(pTouch); set.addObject(pTouch);
} }
@ -292,17 +267,8 @@ void CCEGLViewProtocol::getSetOfTouchesEndOrCancel(CCSet& set, int num, int ids[
if (pTouch) if (pTouch)
{ {
CCLOGINFO("Ending touches with id: %d, x=%f, y=%f", id, x, y); CCLOGINFO("Ending touches with id: %d, x=%f, y=%f", id, x, y);
if (m_bIsRetinaEnabled)
{
pTouch->setTouchInfo(pIndex->getValue(), (x - m_obViewPortRect.origin.x),
(y - m_obViewPortRect.origin.y));
}
else
{
pTouch->setTouchInfo(pIndex->getValue(), (x - m_obViewPortRect.origin.x) / m_fScaleX, pTouch->setTouchInfo(pIndex->getValue(), (x - m_obViewPortRect.origin.x) / m_fScaleX,
(y - m_obViewPortRect.origin.y) / m_fScaleY); (y - m_obViewPortRect.origin.y) / m_fScaleY);
}
set.addObject(pTouch); set.addObject(pTouch);
@ -358,9 +324,4 @@ float CCEGLViewProtocol::getScaleY() const
return m_fScaleY; return m_fScaleY;
} }
bool CCEGLViewProtocol::isRetinaEnabled() const
{
return m_bIsRetinaEnabled;
}
NS_CC_END NS_CC_END

View File

@ -107,12 +107,6 @@ public:
*/ */
virtual void setScissorInPoints(float x , float y , float w , float h); virtual void setScissorInPoints(float x , float y , float w , float h);
/**
* Enable retina mode.
* You can't use it with setDesignResolutionSize
*/
virtual bool enableRetina();
/** Touch events are handled by default; if you want to customize your handlers, please override these functions: */ /** Touch events are handled by default; if you want to customize your handlers, please override these functions: */
virtual void handleTouchesBegin(int num, int ids[], float xs[], float ys[]); virtual void handleTouchesBegin(int num, int ids[], float xs[], float ys[]);
virtual void handleTouchesMove(int num, int ids[], float xs[], float ys[]); virtual void handleTouchesMove(int num, int ids[], float xs[], float ys[]);
@ -133,11 +127,6 @@ public:
* Get scale factor of the vertical direction. * Get scale factor of the vertical direction.
*/ */
float getScaleY() const; float getScaleY() const;
/**
* Get retina mode status (on if true).
*/
bool isRetinaEnabled() const;
private: private:
void getSetOfTouchesEndOrCancel(CCSet& set, int num, int ids[], float xs[], float ys[]); void getSetOfTouchesEndOrCancel(CCSet& set, int num, int ids[], float xs[], float ys[]);
@ -156,7 +145,6 @@ protected:
float m_fScaleX; float m_fScaleX;
float m_fScaleY; float m_fScaleY;
ResolutionPolicy m_eResolutionPolicy; ResolutionPolicy m_eResolutionPolicy;
bool m_bIsRetinaEnabled;
}; };
// end of platform group // end of platform group

View File

@ -40,7 +40,6 @@ public:
virtual bool isOpenGLReady(); virtual bool isOpenGLReady();
virtual bool setContentScaleFactor(float contentScaleFactor); virtual bool setContentScaleFactor(float contentScaleFactor);
virtual bool enableRetina();
// keep compatible // keep compatible
virtual void end(); virtual void end();

View File

@ -50,33 +50,10 @@ bool CCEGLView::setContentScaleFactor(float contentScaleFactor)
{ {
assert(m_eResolutionPolicy == kResolutionUnKnown); // cannot enable retina mode assert(m_eResolutionPolicy == kResolutionUnKnown); // cannot enable retina mode
if ([[EAGLView sharedEGLView] respondsToSelector:@selector(setContentScaleFactor:)])
{
UIView * view = [EAGLView sharedEGLView];
view.contentScaleFactor = contentScaleFactor;
[view setNeedsLayout];
m_fScaleX = m_fScaleY = contentScaleFactor; m_fScaleX = m_fScaleY = contentScaleFactor;
m_bIsRetinaEnabled = true; [[EAGLView sharedEGLView] setNeedsLayout];
return true; return true;
}
else
{
return false;
}
}
bool CCEGLView::enableRetina()
{
bool ret = true;
// can set content scale factor?
ret &= [[EAGLView sharedEGLView] respondsToSelector:@selector(setContentScaleFactor:)];
// SD device?
ret &= ([[UIScreen mainScreen] scale] != 1.0f);
return ret;
} }
void CCEGLView::end() void CCEGLView::end()

View File

@ -151,6 +151,11 @@ static EAGLView *view = 0;
originalRect_ = self.frame; originalRect_ = self.frame;
self.keyboardShowNotification = nil; self.keyboardShowNotification = nil;
if ([view respondsToSelector:@selector(setContentScaleFactor:)])
{
view.contentScaleFactor = [[UIScreen mainScreen] scale];
}
} }
return self; return self;
@ -200,13 +205,13 @@ static EAGLView *view = 0;
-(int) getWidth -(int) getWidth
{ {
CGSize bound = [self bounds].size; CGSize bound = [self bounds].size;
return bound.width; return bound.width * self.contentScaleFactor;
} }
-(int) getHeight -(int) getHeight
{ {
CGSize bound = [self bounds].size; CGSize bound = [self bounds].size;
return bound.height; return bound.height * self.contentScaleFactor;
} }
@ -401,8 +406,8 @@ static EAGLView *view = 0;
int i = 0; int i = 0;
for (UITouch *touch in touches) { for (UITouch *touch in touches) {
ids[i] = (int)touch; ids[i] = (int)touch;
xs[i] = [touch locationInView: [touch view]].x; xs[i] = [touch locationInView: [touch view]].x * view.contentScaleFactor;;
ys[i] = [touch locationInView: [touch view]].y; ys[i] = [touch locationInView: [touch view]].y * view.contentScaleFactor;;
++i; ++i;
} }
cocos2d::CCEGLView::sharedOpenGLView()->handleTouchesBegin(i, ids, xs, ys); cocos2d::CCEGLView::sharedOpenGLView()->handleTouchesBegin(i, ids, xs, ys);
@ -421,8 +426,8 @@ static EAGLView *view = 0;
int i = 0; int i = 0;
for (UITouch *touch in touches) { for (UITouch *touch in touches) {
ids[i] = (int)touch; ids[i] = (int)touch;
xs[i] = [touch locationInView: [touch view]].x; xs[i] = [touch locationInView: [touch view]].x * view.contentScaleFactor;;
ys[i] = [touch locationInView: [touch view]].y; ys[i] = [touch locationInView: [touch view]].y * view.contentScaleFactor;;
++i; ++i;
} }
cocos2d::CCEGLView::sharedOpenGLView()->handleTouchesMove(i, ids, xs, ys); cocos2d::CCEGLView::sharedOpenGLView()->handleTouchesMove(i, ids, xs, ys);
@ -442,8 +447,8 @@ static EAGLView *view = 0;
int i = 0; int i = 0;
for (UITouch *touch in touches) { for (UITouch *touch in touches) {
ids[i] = (int)touch; ids[i] = (int)touch;
xs[i] = [touch locationInView: [touch view]].x; xs[i] = [touch locationInView: [touch view]].x * view.contentScaleFactor;;
ys[i] = [touch locationInView: [touch view]].y; ys[i] = [touch locationInView: [touch view]].y * view.contentScaleFactor;;
++i; ++i;
} }
cocos2d::CCEGLView::sharedOpenGLView()->handleTouchesEnd(i, ids, xs, ys); cocos2d::CCEGLView::sharedOpenGLView()->handleTouchesEnd(i, ids, xs, ys);
@ -463,8 +468,8 @@ static EAGLView *view = 0;
int i = 0; int i = 0;
for (UITouch *touch in touches) { for (UITouch *touch in touches) {
ids[i] = (int)touch; ids[i] = (int)touch;
xs[i] = [touch locationInView: [touch view]].x; xs[i] = [touch locationInView: [touch view]].x * view.contentScaleFactor;;
ys[i] = [touch locationInView: [touch view]].y; ys[i] = [touch locationInView: [touch view]].y * view.contentScaleFactor;;
++i; ++i;
} }
cocos2d::CCEGLView::sharedOpenGLView()->handleTouchesCancel(i, ids, xs, ys); cocos2d::CCEGLView::sharedOpenGLView()->handleTouchesCancel(i, ids, xs, ys);
@ -806,8 +811,6 @@ static EAGLView *view = 0;
notiInfo.end.size.height -= offestY; notiInfo.end.size.height -= offestY;
} }
if (!cocos2d::CCEGLView::sharedOpenGLView()->isRetinaEnabled())
{
float scaleX = cocos2d::CCEGLView::sharedOpenGLView()->getScaleX(); float scaleX = cocos2d::CCEGLView::sharedOpenGLView()->getScaleX();
float scaleY = cocos2d::CCEGLView::sharedOpenGLView()->getScaleY(); float scaleY = cocos2d::CCEGLView::sharedOpenGLView()->getScaleY();
@ -820,7 +823,6 @@ static EAGLView *view = 0;
notiInfo.end.origin.y /= scaleY; notiInfo.end.origin.y /= scaleY;
notiInfo.end.size.width /= scaleX; notiInfo.end.size.width /= scaleX;
notiInfo.end.size.height /= scaleY; notiInfo.end.size.height /= scaleY;
}
cocos2d::CCIMEDispatcher* dispatcher = cocos2d::CCIMEDispatcher::sharedDispatcher(); cocos2d::CCIMEDispatcher* dispatcher = cocos2d::CCIMEDispatcher::sharedDispatcher();
if (UIKeyboardWillShowNotification == type) if (UIKeyboardWillShowNotification == type)
@ -860,10 +862,7 @@ static EAGLView *view = 0;
if (dis < 0.0f) dis = 0.0f; if (dis < 0.0f) dis = 0.0f;
if (!cocos2d::CCEGLView::sharedOpenGLView()->isRetinaEnabled())
{
dis *= cocos2d::CCEGLView::sharedOpenGLView()->getScaleY(); dis *= cocos2d::CCEGLView::sharedOpenGLView()->getScaleY();
}
switch ([[UIApplication sharedApplication] statusBarOrientation]) switch ([[UIApplication sharedApplication] statusBarOrientation])
{ {

View File

@ -16,40 +16,17 @@ bool AppDelegate::applicationDidFinishLaunching() {
CCDirector *pDirector = CCDirector::sharedDirector(); CCDirector *pDirector = CCDirector::sharedDirector();
pDirector->setOpenGLView(CCEGLView::sharedOpenGLView()); pDirector->setOpenGLView(CCEGLView::sharedOpenGLView());
//pDirector->setProjection(kCCDirectorProjection2D);
TargetPlatform target = getTargetPlatform(); if (pDirector->getWinSize().height > 320)
if (target == kTargetIpad)
{ {
// ipad
CCFileUtils::sharedFileUtils()->setResourceDirectory("iphonehd");
// don't enable retina because we don't have ipad hd resource
CCEGLView::sharedOpenGLView()->setDesignResolutionSize(960, 640, kResolutionNoBorder);
}
else if (target == kTargetIphone)
{
// iphone
// try to enable retina on device
if (true == CCDirector::sharedDirector()->enableRetinaDisplay(true))
{
// iphone hd
CCFileUtils::sharedFileUtils()->setResourceDirectory("iphonehd"); CCFileUtils::sharedFileUtils()->setResourceDirectory("iphonehd");
pDirector->setContentScaleFactor(2.0f);
} }
else else
{
CCFileUtils::sharedFileUtils()->setResourceDirectory("iphone"); CCFileUtils::sharedFileUtils()->setResourceDirectory("iphone");
}
} CCEGLView::sharedOpenGLView()->setDesignResolutionSize(480, 320, kResolutionShowAll);
else
{
// android, windows, blackberry, linux or mac
// use 960*640 resources as design resolution size
CCFileUtils::sharedFileUtils()->setResourceDirectory("iphonehd");
CCEGLView::sharedOpenGLView()->setDesignResolutionSize(960, 640, kResolutionNoBorder);
}
// turn on display FPS // turn on display FPS
pDirector->setDisplayStats(true); pDirector->setDisplayStats(true);

View File

@ -27,7 +27,7 @@ bool HelloWorld::init()
return false; return false;
} }
CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize(); CCSize winSize = CCDirector::sharedDirector()->getWinSize();
CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin(); CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();
///////////////////////////// /////////////////////////////
@ -41,8 +41,7 @@ bool HelloWorld::init()
this, this,
menu_selector(HelloWorld::menuCloseCallback)); menu_selector(HelloWorld::menuCloseCallback));
pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 , pCloseItem->setPosition(ccp(winSize.width - 20 + origin.x, 20 + origin.y));
origin.y + pCloseItem->getContentSize().height/2));
// create menu, it's an autorelease object // create menu, it's an autorelease object
CCMenu* pMenu = CCMenu::create(pCloseItem, NULL); CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
@ -57,8 +56,7 @@ bool HelloWorld::init()
CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24); CCLabelTTF* pLabel = CCLabelTTF::create("Hello World", "Arial", 24);
// position the label on the center of the screen // position the label on the center of the screen
pLabel->setPosition(ccp(origin.x + visibleSize.width/2, pLabel->setPosition(ccp(winSize.width/2 + origin.x, winSize.height - 50 + origin.y));
origin.y + visibleSize.height - pLabel->getContentSize().height));
// add the label as a child to this layer // add the label as a child to this layer
this->addChild(pLabel, 1); this->addChild(pLabel, 1);
@ -67,14 +65,11 @@ bool HelloWorld::init()
CCSprite* pSprite = CCSprite::create("HelloWorld.png"); CCSprite* pSprite = CCSprite::create("HelloWorld.png");
// position the sprite on the center of the screen // position the sprite on the center of the screen
pSprite->setPosition(ccp(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y)); pSprite->setPosition(ccp(winSize.width/2 + origin.x, winSize.height/2 + origin.y));
// add the sprite as a child to this layer // add the sprite as a child to this layer
this->addChild(pSprite, 0); this->addChild(pSprite, 0);
// enable standard touch
this->setTouchEnabled(true);
return true; return true;
} }
@ -86,11 +81,3 @@ void HelloWorld::menuCloseCallback(CCObject* pSender)
exit(0); exit(0);
#endif #endif
} }
void HelloWorld::ccTouchesBegan(CCSet *pTouches, CCEvent *pEvent)
{
CCTouch* touch = (CCTouch*)(* pTouches->begin());
CCPoint pos = touch->getLocation();
CCLog("touch, x = %f, y = %f", pos.x, pos.y);
}