最近看了几篇关于这个文章,自己试验了一下,这里主要使用FIXED_HEIGHT这种模式,用FIXED_WIDTH也一样。
主要思想是:
ResolutionPolicy::FIXED_HEIGHT
保持传入的设计分辨率高度不变,根据屏幕分辨率修正设计分辨率的宽度。
ResolutionPolicy::FIXED_WIDTH
保持传入的设计分辨率宽度不变,根据屏幕分辨率修正设计分辨率的高度。
只要在AppDelegate.cpp的AppDelegate::applicationDidFinishLaunching()方法中加几句代码即可:
bool AppDelegate::applicationDidFinishLaunching() { // initialize director auto director = Director::getInstance(); auto glview = director->getOpenGLView(); if(!glview) { #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32) //在Win32下面做测试,假设手机分辨率(屏幕尺寸)是800x480 glview = GLViewImpl::createWithRect("My Game",Rect(0,800,480),1.0f); #endif } 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 / 30); auto frameSize = glview->getFrameSize();//获取屏幕尺寸, //我们的设计分辨率是800x480 auto winSize = Size(800,480); //设置设计分辨率的和适配模式,进去看setDesignResolutionSize和updateDesignResolutionSize这两个个方法的源码可以知道, //下面这句代码走完之后,设计分辨率的高度被设置成480,但是宽度并不是800,而是做了缩放 director->getOpenGLView()->setDesignResolutionSize(winSize.width,winSize.height,ResolutionPolicy::FIXED_HEIGHT); //下面这个是关键 director->setContentScaleFactor(frameSize.width/director->getOpenGLView()->getDesignResolutionSize().width); // create a scene. it's an autorelease object auto scene = LoginScene::createScene(); // run director->runWithScene(scene); return true; }
用到了下面这些接口:
复制代码
|