加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

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

发布时间:2020-12-14 16:41:25 所属栏目:百科 来源:网络整理
导读:下面是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

下面是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; }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读