cocos2dx 3.x designResolutionSize须主动设置
cocos2dx 3.x最初设置screenSize和designResolutionSize的地方如下:
bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
if(!glview) {
glview = GLViewImpl::create("title");
director->setOpenGLView(glview);
}
...
}
在iOS target下(在mac target下代码不一样),GLViewImpl::create(...)的实现代码如下:
GLViewImpl* GLViewImpl::create(const std::string& viewName)
{
auto ret = new (std::nothrow) GLViewImpl;
if(ret && ret->initWithFullScreen(viewName)) {
ret->autorelease();
return ret;
}
return nullptr;
}
bool GLViewImpl::initWithFullScreen(const std::string& viewName)
{
CGRect rect = [[UIScreen mainScreen] bounds];
Rect r;
r.origin.x = rect.origin.x;
r.origin.y = rect.origin.y;
r.size.width = rect.size.width;
r.size.height = rect.size.height;
return initWithRect(viewName, r, 1);
}
bool GLViewImpl::initWithRect(const std::string& viewName, Rect rect, float frameZoomFactor)
{
CGRect r = CGRectMake(rect.origin.x, rect.origin.y, rect.size.width, rect.size.height);
convertAttrs();
CCEAGLView *eaglview = [CCEAGLView viewWithFrame: r
pixelFormat: (NSString*)_pixelFormat
depthFormat: _depthFormat
preserveBackbuffer: NO
sharegroup: nil
multiSampling: NO
numberOfSamples: 0];
[eaglview setMultipleTouchEnabled:YES];
_screenSize.width = _designResolutionSize.width = [eaglview getWidth];
_screenSize.height = _designResolutionSize.height = [eaglview getHeight];
// _scaleX = _scaleY = [eaglview contentScaleFactor];
_eaglview = eaglview;
return true;
}
可见,默认cocos2dx 3.x会将designresolutionsize设成与screensize相等,于是显示效果如果在iphone上正常的话在ipad上肯定就不对了,所以我们必须主动设designresolutionsize。
例如,可以在创建完glview后设置designResolutionSize:
bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
auto director = Director::getInstance();
auto glview = director->getOpenGLView();
if(!glview) {
glview = GLViewImpl::create("title");
director->setOpenGLView(glview);
}
//added by[yang chao]
//by default glview is created "fullscreen", namely screenSize = designResolutionSize.width = eaglview size
//but this is not what we want, we want use our own designResolutionSize, so we should set designResolution ourselves
{
director->getOpenGLView()->setDesignResolutionSize(1136,640,ResolutionPolicy::NO_BORDER);
}
...
}