对于cocos2dx的分辨率方案原来一知半解,终于今天有机会给搞清楚了。在cocos2d-x中的几种分辨率:
保存在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();
游戏UI设计人员或程序人员都是基于此尺寸进行设计。
获取方式:
(1)游戏设计窗口大小:Director::getInstance()->getVisibleSize(); //实际从EGLView中获取
(2)游戏设计窗口原点:Director::getInstance()->getVisibleOrigin(); //实际从EGView中获取
分辨率自适应:即把设计好的游戏以全屏方式(但背景图片不一定能全部显示出来)显示在目标设备上,以给用户最好的体验。
可以通过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 800 bool 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; }
Director getWinSize:w=1920.000000,h=1032.000000 Director getWinSizeInPixels:w=1920.000000,h=1032.000000 Director getVisibleSize:w=1920.000000,h=1032.000000 Director getVisibleOrigin:x=0.000000,y=0.000000 Director BS: getContentScaleFactor: scaleFactor=1.000000 Director AS: getContentScaleFactor: scaleFactor=1.000000 Director getWinSize:w=1280.000000,h=800.000000 Director getWinSizeInPixels:w=1280.000000,h=800.000000 Director getVisibleSize:w=1280.000000,h=688.000000 Director getVisibleOrigin:x=0.000000,y=56.000000
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);