cocos2d-x 3 0 分辨率自适应方案
发布日期:2021-09-29 04:02:25 浏览次数:0 分类:技术文章

分享一下我老师大神的人工智能教程!零基础,通俗易懂!

也欢迎大家转载本篇文章。分享知识,造福人民,实现我们中华民族伟大复兴!

               

1. 简介

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

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

          保存在EGLViewProtocol类的Size _screenSize;

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

    EGLint w, h;    eglQuerySurface(display, surface, EGL_WIDTH, &w);    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如下所示:

#define DESIGN_WIDTH 1280#define DESIGN_HEIGHT 800bool AppDelegate::applicationDidFinishLaunching() {    // initialize director    auto director = Director::getInstance();    auto eglView = EGLView::getInstance();    director->setOpenGLView(eglView);     // 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);    // resolution information    Size size;    size= director->getWinSize();    log("***IDONG: Director getWinSize:w=%f,h=%f",size.width,size.height);    size = director->getWinSizeInPixels();    log("***IDONG: Director getWinSizeInPixels:w=%f,h=%f",size.width,size.height);        size = director->getVisibleSize();    log("***IDONG: Director getVisibleSize:w=%f,h=%f",size.width,size.height);    Point point = director->getVisibleOrigin();    log("***IDONG: Director getVisibleOrigin:x=%f,y=%f",point.x,point.y);            log("***IDONG: Director BS: getContentScaleFactor: scaleFactor=%f",director->getContentScaleFactor());    // set design resolution size    eglView->setDesignResolutionSize(DESIGN_WIDTH,DESIGN_HEIGHT,ResolutionPolicy::NO_BORDER);    log("***IDONG:\n");    log("***IDONG: Director AS: getContentScaleFactor: scaleFactor=%f",director->getContentScaleFactor());    size= director->getWinSize();    log("***IDONG: Director getWinSize:w=%f,h=%f",size.width,size.height);    size = director->getWinSizeInPixels();    log("***IDONG: Director getWinSizeInPixels:w=%f,h=%f",size.width,size.height);        size = director->getVisibleSize();    log("***IDONG: Director getVisibleSize:w=%f,h=%f",size.width,size.height);    point = director->getVisibleOrigin();    log("***IDONG: Director getVisibleOrigin:x=%f,y=%f",point.x,point.y);    // create a scene. it's an autorelease object    auto scene = StartLayer::createScene();    // run    director->runWithScene(scene);    return true;}

相关log信息:

Director getWinSize:w=1920.000000,h=1032.000000Director getWinSizeInPixels:w=1920.000000,h=1032.000000Director getVisibleSize:w=1920.000000,h=1032.000000Director getVisibleOrigin:x=0.000000,y=0.000000Director BS: getContentScaleFactor: scaleFactor=1.000000Director AS: getContentScaleFactor: scaleFactor=1.000000Director getWinSize:w=1280.000000,h=800.000000Director getWinSizeInPixels:w=1280.000000,h=800.000000Director getVisibleSize:w=1280.000000,h=688.000000Director getVisibleOrigin:x=0.000000,y=56.000000

2.1 显示规则

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

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

    Size visibleSize = Director::getInstance()->getVisibleSize();    Point origin = Director::getInstance()->getVisibleOrigin();    //    // 2. add background    //auto sprite = Sprite::create("map_logo_ocean_001_sx.jpg",Rect(0,168,visibleSize.width,visibleSize.height));    auto sprite = Sprite::create("map_logo_ocean_001_sx.jpg");    // position the sprite on the center of the screen    sprite->setPosition(Point(visibleSize.width/2 + origin.x, visibleSize.height/2 + origin.y));    // add the sprite as a child to this layer    this->addChild(sprite, 0);  

 


           

给我老师的人工智能教程打call!

这里写图片描述
上一篇:Linux USB驱动工作流程
下一篇:ARM体系结构

关于作者

    白红宇是个全栈工程师,前端vue,小程序,app开发到后端框架设计,数据库设计,环境部署上线运维。

最新文章