Cocos2d-x屏幕适配
Cocos2d-x屏幕适配资源分辨率,设计分辨率,屏幕分辨率
从资源分辨率到设计分辨率setSearchPaths()需要根据当前屏幕分辨率做恰当的设置。 从设计分辨率到屏幕分辨率首先,我们通过一张图了解下Cocos2d-X的几种屏幕适配规则: FIXED_HEIGHT: 源码面前无秘密class CC_DLL GLView : public Ref
{
/** * Get the frame size of EGL view. * In general,it returns the screen size since the EGL view is a fullscreen view. * * @return The frame size of EGL view. */
const Size& GLView::getFrameSize() const
{
return _screenSize;
}
/** * Set the frame size of EGL view. * * @param width The width of the fram size. * @param height The height of the fram size. */
void GLView::setFrameSize(float width,float height)
{
_screenSize = Size(width,height);
}
/** * Get the visible area size of opengl viewport. * * @return The visible area size of opengl viewport. */
Size GLView::getVisibleSize() const
{
if (_resolutionPolicy == ResolutionPolicy::NO_BORDER)
{
return Size(_screenSize.width/_scaleX,_screenSize.height/_scaleY);
}
else
{
return _designResolutionSize;
}
}
/** * Get the visible origin point of opengl viewport. * * @return The visible origin point of opengl viewport. */
Vec2 GLView::getVisibleOrigin() const
{
if (_resolutionPolicy == ResolutionPolicy::NO_BORDER)
{
return Vec2((_designResolutionSize.width - _screenSize.width/_scaleX)/2,(_designResolutionSize.height - _screenSize.height/_scaleY)/2);
}
else
{
return Vec2::ZERO;
}
}
/** * Set the design resolution size. * @param width Design resolution width. * @param height Design resolution height. * @param resolutionPolicy The resolution policy desired,you may choose: * [1] EXACT_FIT Fill screen by stretch-to-fit: if the design resolution ratio of width to height is different from the screen resolution ratio,your game view will be stretched. * [2] NO_BORDER Full screen without black border: if the design resolution ratio of width to height is different from the screen resolution ratio,two areas of your game view will be cut. * [3] SHOW_ALL Full screen with black border: if the design resolution ratio of width to height is different from the screen resolution ratio,two black borders will be shown. */
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();
}
void GLView::updateDesignResolutionSize()
{
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;
if (_resolutionPolicy == ResolutionPolicy::NO_BORDER)
{
_scaleX = _scaleY = MAX(_scaleX,_scaleY);
}
else if (_resolutionPolicy == ResolutionPolicy::SHOW_ALL)
{
_scaleX = _scaleY = MIN(_scaleX,_scaleY);
}
else if ( _resolutionPolicy == ResolutionPolicy::FIXED_HEIGHT) {
_scaleX = _scaleY;
_designResolutionSize.width = ceilf(_screenSize.width/_scaleX);
}
else if ( _resolutionPolicy == ResolutionPolicy::FIXED_WIDTH) {
_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
auto director = Director::getInstance();
director->_winSizeInPoints = getDesignResolutionSize();
director->_isStatusLabelUpdated = true;
director->setProjection(director->getProjection());
// Github issue #16139
// A default viewport is needed in order to display the FPS,
// since the FPS are rendered in the Director,and there is no viewport there.
// Everything,including the FPS should renderer in the Scene.
glViewport(0,0,_screenSize.width,_screenSize.height);
}
}
};
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |