http://blog.csdn.net/itcastcpp/article/details/18608437 背景 在學習cocos2dx時,我們在main函數中發現一句代碼, [cpp] view plaincopyprint? #includemain.h #includeAppDelegate.h #includeCCEGLView.h USING_NS_CC; int APIENTRY_tWinMain( HINSTANCE
http://blog.csdn.net/itcastcpp/article/details/18608437
在學習cocos2dx時,我們在main函數中發現一句代碼,
[cpp]
view plaincopyprint?
#include "main.h" #include "AppDelegate.h" #include "CCEGLView.h" USING_NS_CC; int APIENTRY _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow) { UNREFERENCED_PARAMETER(hPrevInstance); UNREFERENCED_PARAMETER(lpCmdLine); // create the application instance AppDelegate app; CCEGLView* eglView = CCEGLView::sharedOpenGLView(); eglView->setViewName("CrazyMario"); eglView->setFrameSize(480, 320); return CCApplication::sharedApplication()->run(); }
那就是eglView->setFrameSize(480,320),這句代碼設置了窗口的大小,一般說來手機游戲需要全屏顯示,所以對于不同分辨率的手機,setFrameSize要求不一樣的。這樣是不是很崩潰?因為我們代碼里很多地方可能也要改,圖片大小可能也要改,那怎么辦呢?
其實cocos2dx已經為我們做好了這一切
這個方案就是調用eglView->setDesignResolutionSize(480, 320, kResolutionShowAll);來告訴cocos2dx,我的程序是按照480,320來設計的,那么setFrameSize如果不是480,320那么cocos2dx會按照比例給我們做適配,不用我們做別的事情。
在超級馬里奧這個游戲里,在AppDelegate中已經調用了setDesignResolutionSize函數設置設計大小為480,320
那么在setFrameSize不同的情況下,也不會引起圖片比例不合適的情況,只是窗口大小會發生變化而已
在480*320的情況下
在960*640的情況下,只是界面變大了,圖片沒有任何的不適合
第三個參數的取值范圍如下:
[cpp]
view plaincopyprint?
enum 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. kResolutionExactFit, // The entire application fills the specified area, without distortion but possibly with some cropping, // while maintaining the original aspect ratio of the application. kResolutionNoBorder, // 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. kResolutionShowAll, // 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 kResolutionFixedHeight, // 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 kResolutionFixedWidth, kResolutionUnKnown, };
kResolutionExactFit:會靠拉伸來填滿屏幕,本例來說背景圖會變形來填充屏幕,因為1024:768=1.3, 480:320=1.5,寬高比不同,圖片也就無法等比縮放來填滿屏幕,只能變形了。
kResolutionNoBorder: 看不到黑邊,實際就是寬高等比縮放,但縮放比例取寬比和高比之中大的那一個。
kResolutionShowAll:全部顯示,可以理解為保證內容都顯示在屏幕之內,實際也是寬高等比縮放,但縮放比例取寬比和高比之中小的那一個。
聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com