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

    }

  

 ...

 }

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值