最近开始搞cocos了,虽然开发起来看起来还挺简单的……但是感觉坑真是好多。。。
ok,现在讲一下我遇到的一个问题。在cocos2dx-3.x的版本中,增加了DesignResolutionSize的概念,这个东西可以使得自己设计的屏幕大小适应各种机器的屏幕。听起来还是很方便的。我开始想要模拟一个960*640的屏幕,但逻辑上的分辨率是480*320,于是我这么写了:
auto glview = director->getOpenGLView(); if(!glview) { glview = GLViewImpl::create("Cpp Empty Test"); director->setOpenGLView(glview); } // 设定分辨率 glview->setDesignResolutionSize(480,320,ResolutionPolicy::EXACT_FIT); glview->setFrameSize(960,640);
glview->setFrameSize(960,640); glview->setDesignResolutionSize(480,320,ResolutionPolicy::EXACT_FIT);
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->createStatsLabel(); director->setGLDefaultValues(); } }
void GLView::setFrameSize(float width, float height) { _designResolutionSize = _screenSize = Size(width, height); }