Cocos2d-x自带的屏幕适配方案

前端之家收集整理的这篇文章主要介绍了Cocos2d-x自带的屏幕适配方案前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

下面是cocos2dx3.7生成的项目中的AppDelegate.cpp代码

static cocos2d::Size designResolutionSize = cocos2d::Size(480,800);

static cocos2d::Size smallResolutionSize = cocos2d::Size(320,480);

static cocos2d::Size mediumResolutionSize = cocos2d::Size(768,1024);//cocos2d::Size(768,1024);
static cocos2d::Size largeResolutionSize = cocos2d::Size(1536,2048);

AppDelegate::AppDelegate()

{

}
AppDelegate::~AppDelegate()
{
}

//if you want a different context,just modify the value of glContextAttrs //it will takes effect on all platforms void AppDelegate::initGLContextAttrs() { //set OpenGL context attributions,now can only set six attributions: //red,green,blue,alpha,depth,stencil GLContextAttrs glContextAttrs = {8,8,24,8}; GLView::setGLContextAttrs(glContextAttrs); } // If you want to use packages manager to install more packages,// don't modify or remove this function static int register_all_packages() { return 0; //flag for packages manager } bool AppDelegate::applicationDidFinishLaunching() { // initialize director auto director = Director::getInstance(); auto glview = director->getOpenGLView(); if(!glview) { glview = GLViewImpl::createWithRect("CatBackyard",Rect(0.0f,0.0f,designResolutionSize.width,designResolutionSize.height),1.0f); director->setOpenGLView(glview); } // 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); // Set the design resolution //设计分辨率到屏幕分辨率的完美适配 glview->setDesignResolutionSize(designResolutionSize.width,designResolutionSize.height,ResolutionPolicy::NO_BORDER); Size frameSize = glview->getFrameSize();// 获取屏幕尺寸 // if the frame's height is larger than the height of medium size. if (frameSize.height > mediumResolutionSize.height) { director->setContentScaleFactor(MIN(largeResolutionSize.height/designResolutionSize.height,largeResolutionSize.width/designResolutionSize.width)); } // if the frame's height is larger than the height of small size. else if (frameSize.height > smallResolutionSize.height) { //使资源适尺寸用设计分辨率:图片根据设计分辨率做缩放效果 director->setContentScaleFactor(MIN(mediumResolutionSize.height / designResolutionSize.height,mediumResolutionSize.width / designResolutionSize.width)); } // if the frame's height is smaller than the height of medium size. else { director->setContentScaleFactor(MIN(smallResolutionSize.height/designResolutionSize.height,smallResolutionSize.width/designResolutionSize.width)); } register_all_packages(); // create a scene. it's an autorelease object auto scene = Loading::createScene(); // run director->runWithScene(scene); return true; }

原文链接:https://www.f2er.com/cocos2dx/340920.html

猜你在找的Cocos2d-x相关文章