cocos2d-x 3.0 分辨率自适应方案

本文转自:http://blog.csdn.net/myarrow/article/details/20541357 谢谢作者分享。

1. 简介

    对于cocos2dx的分辨率方案原来一知半解,终于今天有机会给搞清楚了。在cocos2d-x中的几种分辨率:

1.1 Framebuffer分辨率(其大小依赖于硬件设备)

          保存在EGLViewProtocol类的Size _screenSize;

          在nativeactivity.cpp的cocos_init中调用setFrameSize设置Framebuffer大小,Framebuffer大小在engine_init_display中调用以下代码获取:     

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. EGLint w, h;  
  2. eglQuerySurface(display, surface, EGL_WIDTH, &w);  
  3. eglQuerySurface(display, surface, EGL_HEIGHT, &h);  

              

           获取方式:

           EGLView::getInstance()->getFrameSize();

      

1.2 设计分辨率(其大小依赖于游戏设计人员,与硬件设备无关)

          游戏UI设计人员或程序人员都是基于此尺寸进行设计。

          获取方式:

          (1)游戏设计窗口大小:Director::getInstance()->getVisibleSize(); //实际从EGLView中获取

          (2)游戏设计窗口原点:Director::getInstance()->getVisibleOrigin(); //实际从EGView中获取

2. 分辨率自适应

    分辨率自适应:即把设计好的游戏以全屏方式(但背景图片不一定能全部显示出来)显示在目标设备上,以给用户最好的体验。

    可以通过EGLView::getInstance()->setDesignResolutionSize(float width, float height, ResolutionPolicy resolutionPolicy)来告诉cocos2d-x 3.0游戏设计分辨率,然后cocos2d-x 3.0将自动根据设置的resolutionPolicy进行scale,以实现全屏显示。

    其参考代码及相关log如下所示:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #define DESIGN_WIDTH 1280  
  2. #define DESIGN_HEIGHT 800  
  3. bool AppDelegate::applicationDidFinishLaunching() {  
  4.     // initialize director  
  5.     auto director = Director::getInstance();  
  6.     auto eglView = EGLView::getInstance();  
  7.   
  8.     director->setOpenGLView(eglView);  
  9.       
  10.     // turn on display FPS  
  11.     director->setDisplayStats(true);  
  12.   
  13.     // set FPS. the default value is 1.0/60 if you don't call this  
  14.     director->setAnimationInterval(1.0 / 60);  
  15.   
  16.   
  17.     // resolution information  
  18.     Size size;  
  19.     size= director->getWinSize();  
  20.     log("***IDONG: Director getWinSize:w=%f,h=%f",size.width,size.height);  
  21.   
  22.     size = director->getWinSizeInPixels();  
  23.     log("***IDONG: Director getWinSizeInPixels:w=%f,h=%f",size.width,size.height);  
  24.       
  25.     size = director->getVisibleSize();  
  26.     log("***IDONG: Director getVisibleSize:w=%f,h=%f",size.width,size.height);  
  27.   
  28.     Point point = director->getVisibleOrigin();  
  29.     log("***IDONG: Director getVisibleOrigin:x=%f,y=%f",point.x,point.y);  
  30.       
  31.       
  32.     log("***IDONG: Director BS: getContentScaleFactor: scaleFactor=%f",director->getContentScaleFactor());  
  33.   
  34.   
  35.     // set design resolution size  
  36.     eglView->setDesignResolutionSize(DESIGN_WIDTH,DESIGN_HEIGHT,ResolutionPolicy::NO_BORDER);  
  37.   
  38.     log("***IDONG:\n");  
  39.     log("***IDONG: Director AS: getContentScaleFactor: scaleFactor=%f",director->getContentScaleFactor());  
  40.   
  41.     size= director->getWinSize();  
  42.     log("***IDONG: Director getWinSize:w=%f,h=%f",size.width,size.height);  
  43.   
  44.     size = director->getWinSizeInPixels();  
  45.     log("***IDONG: Director getWinSizeInPixels:w=%f,h=%f",size.width,size.height);  
  46.       
  47.     size = director->getVisibleSize();  
  48.     log("***IDONG: Director getVisibleSize:w=%f,h=%f",size.width,size.height);  
  49.   
  50.     point = director->getVisibleOrigin();  
  51.     log("***IDONG: Director getVisibleOrigin:x=%f,y=%f",point.x,point.y);  
  52.   
  53.     // create a scene. it's an autorelease object  
  54.     auto scene = StartLayer::createScene();  
  55.   
  56.     // run  
  57.     director->runWithScene(scene);  
  58.   
  59.     return true;  
  60. }  

相关log信息:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. Director getWinSize:w=1920.000000,h=1032.000000  
  2. Director getWinSizeInPixels:w=1920.000000,h=1032.000000  
  3. Director getVisibleSize:w=1920.000000,h=1032.000000  
  4. Director getVisibleOrigin:x=0.000000,y=0.000000  
  5. Director BS: getContentScaleFactor: scaleFactor=1.000000  
  6.   
  7. Director AS: getContentScaleFactor: scaleFactor=1.000000  
  8. Director getWinSize:w=1280.000000,h=800.000000  
  9. Director getWinSizeInPixels:w=1280.000000,h=800.000000  
  10. Director getVisibleSize:w=1280.000000,h=688.000000  
  11. Director getVisibleOrigin:x=0.000000,y=56.000000  

2.1 显示规则

      1) 游戏背景画面不一定能全部显示,但关键部分必须显示,所以在背景画面上的四周不要放置重要信息

      2) 游戏元素必须全部显示,且其位置基于Director::getVisibleSize和Director::getVisibleOrigin 来进行放置,如需要把一个sprite放置于屏幕中央,其代码可以为:

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. Size visibleSize = Director::getInstance()->getVisibleSize();  
  2. Point origin = Director::getInstance()->getVisibleOrigin();  
  3.   
  4. //  
  5. // 2. add background  
  6. //auto sprite = Sprite::create("map_logo_ocean_001_sx.jpg",Rect(0,168,visibleSize.width,visibleSize.height));  
  7. auto sprite = Sprite::create("map_logo_ocean_001_sx.jpg");  
  8.   
  9. // position the sprite on the center of the screen  
  10. sprite->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));  
  11.   
  12. // add the sprite as a child to this layer  
  13. this->addChild(sprite, 0);    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值