cocos2dx 分辨率适配问题

分辨率适配问题好文章:

http://www.cocoachina.com/bbs/read.php?tid=201987&page=1&toread=1#tpc

http://www.zaojiahua.com/adaptation.html

http://blog.csdn.net/xiaominghimi/article/details/8918235

实践第二个文章,是可行的。这里感谢下作者的分享精神

看代码:

bool AppDelegate::applicationDidFinishLaunching() {
    // initialize director
    auto director = Director::getInstance();
    auto glview = director->getOpenGLView();
    //靠近方形的分辨率如 480×320,比例为 3:2,还有 960×540 的16:9 标准宽屏,米3 1920*1080
    //这也算是两种总极端情况了,如果能在这两种比例情况做好适配基本就可以了,
    //如果比 3:2 “更方”如 4:3,比 16:9 “更长”,那么不论如何布局,
    //
//也就是说比例一样的时候用show_all或者no_border都可以完美适配
  //通过getVisibleSize()获得的可视区域的大小和通过getWinSize()获得的逻辑大小是一样的就解决了我们的问题。 auto winSize = Size(640, 960); if(!glview) { log("glview is null"); glview = GLView::create("test war"); director->setOpenGLView(glview); glview->setFrameSize(winSize.width, winSize.height); } else {//进入设备的时候是有glview的 log("glview is not null"); } director->setOpenGLView(glview); //实际屏幕分辨率 auto frameSize = Director::getInstance()->getOpenGLView()->getFrameSize(); /**if (screenSize.height > designSize.height) { director->setContentScaleFactor(screenSize.height / designSize.height); }*/ //将宽和高做一个比,通过这个比,来具体的调整逻辑分辨率的大小 float widthRate = frameSize.width / winSize.width;//1920 /640 float heightRate = frameSize.height / winSize.height;//1080 / 960 //如果是if中的语句,说明逻辑的高度有点大了,就把逻辑的高缩小到和宽度一样的比率 if (widthRate > heightRate) { //里边传入的前俩个参数就是逻辑分辨率的大小,也就是通过getWinSize()得到的大小 glview->setDesignResolutionSize(winSize.width, winSize.height*heightRate / widthRate, kResolutionNoBorder); } else { glview->setDesignResolutionSize(winSize.width*widthRate / heightRate, winSize.height, kResolutionNoBorder); } // 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); // create a scene. it's an autorelease object auto scene = GameMenu::createScene(); // run director->runWithScene(scene); return true; }

 

posted on 2014-08-10 20:15  防空洞123  阅读(320)  评论(0编辑  收藏  举报

导航