cocos2dx 3.3 适配(缩放距离, 不缩放图片)

cocos2dx 3.3 适配(缩放距离, 不缩放图片),第1张

概述bool AppDelegate::applicationDidFinishLaunching() 中修改为 Vec2 frameSize(1500, 300); Vec2 resolutionSize(960,640); bool AppDelegate::applicationDidFinishLaunching() { // initialize director au

bool AppDelegate::applicationDIDFinishLaunching() 中修改为

Vec2 frameSize(1500,300);  Vec2 resolutionSize(960,640); bool AppDelegate::applicationDIDFinishLaunching() {    // initialize director    auto director = Director::getInstance();    auto glvIEw = director->getopenGLVIEw();    if(!glvIEw) {        glvIEw = GLVIEwImpl::create("My Game");		 glvIEw->setFrameSize(frameSize.x,frameSize.y);          director->setopenGLVIEw(glvIEw);		float currentAspectRatio = director->getWinSize().wIDth/director->getWinSize().height;  		float originalAspectRatio = resolutionSize.x/resolutionSize.y;  		if(currentAspectRatio>originalAspectRatio)//采用根据宽度自适应  		{  			glvIEw->setDesignResolutionSize(resolutionSize.x,resolutionSize.y,kResolutionFixedHeight );  		}  		if(currentAspectRatio<=originalAspectRatio)// 采用根据长度自适应  		{  			glvIEw->setDesignResolutionSize(resolutionSize.x,kResolutionFixeDWIDth );  		}      }    // 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);    // create a scene. it's an autorelease object    auto scene = HelloWorld::createScene();    // run    director->runWithScene(scene);    return true;}

修改CCGLVIEw.h文件

添加 成员

public:	Size getResolutionSize();	float getCurrentAndOrignalRatioX();	float getCurrentAndOrignalRatioY();	Vec2 MyPoint(cocos2d::Vec2& pos);

修改CCGLVIEw.cpp文件

添加实现

float GLVIEw::getCurrentAndOrignalRatioX(){	return m_currentAndOrignalRatioX;}float GLVIEw::getCurrentAndOrignalRatioY(){	return m_currentAndOrignalRatioY;}Size GLVIEw::getResolutionSize(){	return m_resolutionSize;}cocos2d::Vec2 GLVIEw::MyPoint( cocos2d::Vec2& pos ){	Vec2 _point = pos-=m_resolutionSize/2;  	Vec2 _offset;  	switch (this->getResolutionPolicy())  	{  	case ResolutionPolicy::FIXED_HEIGHT:  		{  			_offset = Vec2 (_point.x/getCurrentAndOrignalRatioY()*getCurrentAndOrignalRatioX(),_point.y );  			break;  		}  	case ResolutionPolicy::FIXED_WIDTH:  		{  			_offset =Vec2 (_point.x,_point.y/getCurrentAndOrignalRatioX()*getCurrentAndOrignalRatioY() );   			break;  		}  	default:  		break;  	}  	return (Vec2(m_resolutionSize/2) +_offset );  }  
修改函数定义
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);	m_resolutionSize = _designResolutionSize;    _resolutionPolicy = resolutionPolicy;        updateDesignResolutionSize(); }

voID GLVIEw::updateDesignResolutionSize(){    if (_screenSize.wIDth > 0 && _screenSize.height > 0        && _designResolutionSize.wIDth > 0 && _designResolutionSize.height > 0)    {        m_currentAndOrignalRatioX =  _scaleX = (float)_screenSize.wIDth / _designResolutionSize.wIDth;        m_currentAndOrignalRatioY = _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();    }}
总结

以上是内存溢出为你收集整理的cocos2dx 3.3 适配(缩放距离, 不缩放图片)全部内容,希望文章能够帮你解决cocos2dx 3.3 适配(缩放距离, 不缩放图片)所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1026157.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存