我们都知道现在手机的分辨率越来越杂。480*320 800*480 1280*720 等等分辨率。如果要让游戏运行在所有的屏幕分辨率上,似乎是很困难,美工要为不同的分辨率创建不同分辨率的素材,这样 美工可是要累死的节奏。也是很让人头疼的。 所以coco引进了一个"设计分辨率" 所有素材按照设计分辨率的大小来设计制造。以及在编码时精灵的位置等等 都依据这个"设计分辨率"作为参照。之后如果游戏运行在不同的设备上 coco会自动为目标设备的分辨率进行缩放或者拉伸。这样就做到了屏幕所有的适配。
下面贴一段代码
bool AppDelegate::applicationDidFinishLaunching() {
// initialize director
auto director = Director::getInstance();
auto eglView = EGLView::getInstance();
director->setOpenGLView(eglView);
eglView->setDesignResolutionSize(480,800,ResolutionPolicy(0)); //设计分辨率为480*800的分辨率 我们的素材也按照480*800作为标准设计。
// 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 = HelloWorld::createScene();
// run
director->runWithScene(scene);
return true;
}
第三个参数:ResolutionPolicy(0) 因为我这里没有引用这个声明枚举的文件。下面贴一下其声明
enum class ResolutionPolicy
{
// The entire application is visible in the specified area without trying to preserve the original aspect ratio.
// Distortion can occur, and the application may appear stretched or compressed.
EXACT_FIT,//缩放拉伸全屏
// The entire application fills the specified area, without distortion but possibly with some cropping,
// while maintaining the original aspect ratio of the application.
NO_BORDER, //不显示操作面板
// The entire application is visible in the specified area without distortion while maintaining the original
// aspect ratio of the application. Borders can appear on two sides of the application.
SHOW_ALL,//显示全部,但是保持宽高比
// The application takes the height of the design resolution size and modifies the width of the internal
// canvas so that it fits the aspect ratio of the device
// no distortion will occur however you must make sure your application works on different
// aspect ratios
FIXED_HEIGHT,//高度不变
// The application takes the width of the design resolution size and modifies the height of the internal
// canvas so that it fits the aspect ratio of the device
// no distortion will occur however you must make sure your application works on different
// aspect ratios
FIXED_WIDTH,//宽度不变
UNKNOWN,
};
这里面我试过就 EXACT_FIT 和NO_BORDER效果是一样的 SHOW_ALL 是保持素材的宽高比 但是 上下方或者左右方可能出现黑条 最后面两个可以忽略,实用性基本没有。
然后在入口函数设置宽高分辨率,因为我这是在win下 在安卓下入口不一样 并且宽度和高度肯定是要调用某些API获取(还没弄android抱歉)
int APIENTRY _tWinMain(HINSTANCE hInstance,
HINSTANCE hPrevInstance,
LPTSTR lpCmdLine,
int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// create the application instance
AppDelegate app;
EGLView eglView;
eglView.init("Test",320,480); //设置其分辨率为320*480
return Application::getInstance()->run();
运行可以看到,适配成功 不管多少分辨率coco会为我们拉伸或者缩放。
下面图素材是480*800,分别使用320*480 和240*320测试。