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(); } }原文链接:https://www.f2er.com/cocos2dx/345287.html