bool AppDelegate::applicationDidFinishLaunching() { // initialize director auto director = Director::getInstance(); auto glview = director->getOpenGLView(); if(!glview) { glview = GLViewImpl::createWithRect("My Game",Rect(0,960,500),0.7f); director->setOpenGLView(glview); } auto winSize = Size(480,320);//设计分辨率 auto screenSize = glview->getFrameSize();//屏幕分辨率, // turn on display FPS director->setDisplayStats(true); // set FPS. the default value is 1.0/60 if you don't call this director->setAnimationInterval(1.0 / 60); register_all_packages(); float widthRate = screenSize.width / winSize.width; float heightRate = screenSize.height / winSize.height; if (widthRate < heightRate) { //说明设计分辨率宽度偏大 //这时候我们让高度适配,宽度裁剪掉。也就是图片宽度会超出屏幕外 //设置设计分辨率的和适配模式,进去看setDesignResolutionSize和updateDesignResolutionSize这两个个方法的源码可以知道: //下面这句代码走完之后,设计分辨率的高度被设置成480,但是宽度并不是800,而是做了缩放,下面注释详细介绍 director->getOpenGLView()->setDesignResolutionSize(winSize.width,winSize.height,ResolutionPolicy::FIXED_HEIGHT); } else { //说明设计分辨率高度偏大 //这时候我们让宽度适配,高度裁剪掉。也就是图片高度会超出屏幕外 director->getOpenGLView()->setDesignResolutionSize(winSize.width,ResolutionPolicy::FIXED_WIDTH); } /* updateDesignResolutionSize函数部分源码: _scaleX = (float)_screenSize.width / _designResolutionSize.width; _scaleY = (float)_screenSize.height / _designResolutionSize.height; if ( _resolutionPolicy == ResolutionPolicy::FIXED_HEIGHT) { _scaleX = _scaleY; //为了让_screenSize.width / _designResolutionSize.width = _screenSize.height / _designResolutionSize.height,//将_designResolutionSize.width重新赋值 _designResolutionSize.width = ceilf(_screenSize.width/_scaleY); } */ auto scene = HelloWorld::createScene(); director->runWithScene(scene); return true; }
@H_502_116@void GLView::setDesignResolutionSize(float width,float height,ResolutionPolicy resolutionPolicy) { CCASSERT(resolutionPolicy != ResolutionPolicy::UNKNOWN,"should set resolutionPolicy"); if (width == 0.0f || height == 0.0f) { return; } _designResolutionSize.setSize(width,height); _resolutionPolicy = resolutionPolicy; updateDesignResolutionSize(); } 原文链接:https://www.f2er.com/cocos2dx/339040.html