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

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,resolutionSize.y, 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();
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值