背景ios
在学习cocos2dx时,咱们在main函数中发现一句代码,canvas
1 #include "main.h" 2 3 #include "AppDelegate.h" 4 5 #include "CCEGLView.h" 6 7 8 9 USING_NS_CC; 10 11 12 13 int APIENTRY _tWinMain(HINSTANCE hInstance, 14 15 HINSTANCE hPrevInstance, 16 17 LPTSTR lpCmdLine, 18 19 int nCmdShow) 20 21 { 22 23 UNREFERENCED_PARAMETER(hPrevInstance); 24 25 UNREFERENCED_PARAMETER(lpCmdLine); 26 27 28 29 // create the application instance
30 31 AppDelegate app; 32 33 CCEGLView* eglView = CCEGLView::sharedOpenGLView(); 34 35 eglView->setViewName("CrazyMario"); 36 37 eglView->setFrameSize(480, 320); 38 39 return CCApplication::sharedApplication()->run(); 40 41 }
那就是eglView->setFrameSize(480,320),这句代码设置了窗口的大小,通常说来手机游戏须要全屏显示,因此对于不一样分辨率的手机,setFrameSize要求不同的。这样是否是很崩溃?由于咱们代码里不少地方可能也要改,图片大小可能也要改,那怎么办呢?app
其实cocos2dx已经为咱们作好了这一切ide
结局方案函数
这个方案就是调用eglView->setDesignResolutionSize(480, 320, kResolutionShowAll);来告诉cocos2dx,个人程序是按照480,320来设计的,那么setFrameSize若是不是480,320那么cocos2dx会按照比例给咱们作适配,不用咱们作别的事情。学习
在超级马里奥这个游戏里,在AppDelegate中已经调用了setDesignResolutionSize函数设置设计大小为480,320。spa
那么在setFrameSize不一样的状况下,也不会引发图片比例不合适的状况,只是窗口大小会发生变化而已设计
在480*320的状况下code
在960*640的状况下,只是界面变大了,图片没有任何的不适合blog
setDesignResolutionSize的参数
第三个参数的取值范围以下:
1 enum ResolutionPolicy 2 { 3 // The entire application is visible in the specified area without trying to preserve the original aspect ratio. 4 // Distortion can occur, and the application may appear stretched or compressed.
5 kResolutionExactFit, 6 // The entire application fills the specified area, without distortion but possibly with some cropping, 7 // while maintaining the original aspect ratio of the application.
8 kResolutionNoBorder, 9 // The entire application is visible in the specified area without distortion while maintaining the original 10 // aspect ratio of the application. Borders can appear on two sides of the application.
11 kResolutionShowAll, 12 // The application takes the height of the design resolution size and modifies the width of the internal 13 // canvas so that it fits the aspect ratio of the device 14 // no distortion will occur however you must make sure your application works on different 15 // aspect ratios
16 kResolutionFixedHeight, 17 // The application takes the width of the design resolution size and modifies the height of the internal 18 // canvas so that it fits the aspect ratio of the device 19 // no distortion will occur however you must make sure your application works on different 20 // aspect ratios
21 kResolutionFixedWidth, 22 23 kResolutionUnKnown, 24 };
kResolutionExactFit:会靠拉伸来填满屏幕,本例来讲背景图会变形来填充屏幕,由于1024:768=1.3, 480:320=1.5,宽高比不一样,图片也就没法等比缩放来填满屏幕,只能变形了。
kResolutionNoBorder: 看不到黑边,实际就是宽高等比缩放,但缩放比例取宽比和高比之中大的那一个。
kResolutionShowAll:所有显示,能够理解为保证内容都显示在屏幕以内,实际也是宽高等比缩放,但缩放比例取宽比和高比之中小的那一个。