//float width,float height 设计分辨率
//resolutionPolicy 适配策略 这里用kResolutionFixedHeight:高度固定
void CCEGLViewProtocol::setDesignResolutionSize(float width,float height,ResolutionPolicy resolutionPolicy) { CCAssert(resolutionPolicy != kResolutionUnKnown,"should set resolutionPolicy"); if (width == 0.0f || height == 0.0f) { return; } //设置设计分辨率 m_obDesignResolutionSize.setSize(width,height); //计算缩放比率 m_obScreenSize(屏幕大小) m_fScaleX = (float)m_obScreenSize.width / m_obDesignResolutionSize.width; m_fScaleY = (float)m_obScreenSize.height / m_obDesignResolutionSize.height; if (resolutionPolicy == kResolutionNoBorder) { m_fScaleX = m_fScaleY = MAX(m_fScaleX,m_fScaleY); } if (resolutionPolicy == kResolutionShowAll) { m_fScaleX = m_fScaleY = MIN(m_fScaleX,m_fScaleY); } //我们这里使用的,高度固定的意思,就是高度的缩放比例固定,根据高度缩放比率计算宽度 //这里会根据我们设置的设计分辨率,然后根据适配策略,调整设计分辨率 if ( resolutionPolicy == kResolutionFixedHeight) { m_fScaleX = m_fScaleY; m_obDesignResolutionSize.width = ceilf(m_obScreenSize.width/m_fScaleX); } if ( resolutionPolicy == kResolutionFixedWidth) { m_fScaleY = m_fScaleX; m_obDesignResolutionSize.height = ceilf(m_obScreenSize.height/m_fScaleY); } // calculate the rect of viewport float viewPortW = m_obDesignResolutionSize.width * m_fScaleX; float viewPortH = m_obDesignResolutionSize.height * m_fScaleY; m_obViewPortRect.setRect((m_obScreenSize.width - viewPortW) / 2,(m_obScreenSize.height - viewPortH) / 2,viewPortW,viewPortH); m_eResolutionPolicy = resolutionPolicy; // reset director's member variables to fit visible rect //下面说明: CCDirector::sharedDirector()->m_obWinSizeInPoints = getDesignResolutionSize(); CCDirector::sharedDirector()->createStatsLabel(); CCDirector::sharedDirector()->setGLDefaultValues(); }
通过这句赋值:
m_obWinSizeInPoints = m_pobOpenGLView->getDesignResolutionSize();
我们平时使用的接口,获取winSize:
CCSize CCDirector::getWinSize(void) { return m_obWinSizeInPoints; }
举例:
小米2s 720*1280
设计分辨率 640*960
固定高度,缩放比率:
720/640 = 1.125
计算宽度为:1280/1.125 = 1138
由此得到我们的winSize为:640*1138
通过打印信息得到:
03-16 20:49:22.579: D/cocos2d-x debug info(8833): [LUA-print] =====winSize width=====1138 03-16 20:49:22.579: D/cocos2d-x debug info(8833): [LUA-print] =====winSize height=====640原文链接:https://www.f2er.com/cocos2dx/343978.html