// cocos2d-x源码解析 3.9版本
void GLView::updateDesignResolutionSize()
{
// _screenSize 屏幕分辨率大小
//
_designResolutionSize 设计分辨率大小(游戏设计值,以此为准进行的开发,也是在开发中我们正常看到的大小)
//
_scaleX,
_scaleY
私有变量 其值会直接作用在显示上
if (_screenSize.width > 0 && _screenSize.height > 0 && _designResolutionSize.width > 0 && _designResolutionSize.height > 0)
{
// 分别结算屏幕分辨率与设计分辨率的比值 宽,高
_scaleX = (float)
_screenSize.width / _designResolutionSize.width;
_scaleY = (float)
_screenSize.height / _designResolutionSize.height;
// _resolutionPolicy 自适应模式
if (_resolutionPolicy == ResolutionPolicy::NO_BORDER)
{
//
ResolutionPolicy::NO_BORDER –没有黑边的自适应模式,这种情况下会出现某个方向画面超出屏幕外看不到的情况
// 取 _scaleX,_
scaleY 中最大的
_scaleX = _
scaleY = MAX(_
scaleX,_scaleY);
}else if (_resolutionPolicy == ResolutionPolicy::SHOW_ALL)
{
//
ResolutionPolicy::SHOW_ALL–全部显示的自适应模式,这种情况下某个方向可能会出现黑边 _scaleX最小则Y方向上有黑边,_scaleY最小则x方向上有黑边
// 取 _scaleX,_
scaleY 中最小的
_scaleX = _
scaleY = MIN(
_scaleX,_scaleY);
}else if (_resolutionPolicy ==ResolutionPolicy::FIXED_HEIGHT)
{
//
ResolutionPolicy::FIXED_HEIGHT–以高为标准的自适应模式,这种情况下不考虑横向,完全以高为准 当 _scaleX >_scaleY时 x方向出现黑边,当 _scaleX <_scaleY时x方向超出屏幕
// 以_scaleY为准
_scaleX = _scaleY;
_designResolutionSize.width = ceilf(
_screenSize.width/_scaleX);
}else if (_resolutionPolicy == ResolutionPolicy::FIXED_WIDTH)
{
//
ResolutionPolicy::FIXED_WIDTH–以宽为标准的自适应模式,这种情况下不考虑纵向,完全以宽为准 当 _scaleY >_scaleX时 y方向出现黑边,当 _scaleY <_scaleX时y方向超出屏幕
// 以_scaleY为准
_scaleY = _scaleX;
_designResolutionSize.height = ceilf(_
screenSize.height / _scaleY);
}
// calculate the rect of viewport
// 计算出需要的屏幕区域大小
float viewPortW = _designResolutionSize.width * _scaleX;
float viewPortH = _designResolutionSize.height * _scaleY;
// 将视图放到屏幕中央
_viewPortRect.setRect((_
screenSize.width – viewPortW) / 2,(_screenSize.height – viewPortH) / 2,viewPortW,viewPortH);
// reset director’s member variables to fit visible rect
// director 字典,又名导演,总管全局
auto director = Director::getInstance();
director->_winSizeInpoints = getDesignResolutionSize();
director->_isStatusLabelUpdated = true;
// Sets the OpenGL default values.
director->setGLDefaultValues();
}
}
黑边可以通过背景大一点的方式优化