http://www.cnblogs.com/GameDeveloper/archive/2011/11/02/cocos2d-x.html
Android下分辨率太多,不太可能为每种分辨率做一套资源,目前一般来说比较流行的是320*480, 800*400, 854*400。当然现在720P的也出来了,但至少目前不是主流机型^_^.
对于不支持的分辨率,我希望的是能够按照屏幕大小按比例缩放,即有了下面的代码。
1:ViewAutoScale
写了一个ViewAutoScale函数,如下:
- #include "ViewAutoScale.h"
- USING_NS_CC;
- bool IsMatchDisplay(int w, int h, CCSize& size )
- {
- return (w==size.width && h==size.height) || (h==size.width && w==size.height);
- }
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
- int ViewAutoScale(cocos2d::CCEGLView* view,
- void* title,
- int width,
- int height,
- cocos2d::CCSize* supportDisplay,
- int displays,
- int defaultWidth,
- int defaultHeight)
- {
- if(view == NULL)
- {
- return -1;
- }
- for (int i=0; i < displays; i++)
- {
- if (IsMatchDisplay(width, height, supportDisplay[i]))
- {
- view->Create((LPCTSTR)title, width, height);
- return i+1;
- }
- }
- view->Create((LPCTSTR)title, defaultWidth, defaultHeight);
- view->setScreenScale(min((float)width/ defaultWidth, (float)height/ defaultHeight));
- view->resize(width, height);
- view->centerWindow();
- return 0;
- }
- #endif
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
- int ViewAutoScale(cocos2d::CCEGLView* view,
- void* title,
- int width,
- int height,
- cocos2d::CCSize* supportDisplay,
- int displays,
- int defaultWidth,
- int defaultHeight)
- {
- if(view == NULL)
- {
- return -1;
- }
- for (int i=0; i < displays; i++)
- {
- if (IsMatchDisplay(width, height, supportDisplay[i]))
- {
- return i+1;
- }
- }
- view->create(defaultWidth, defaultHeight);
- return 0;
- }
- #endif
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)
- int ViewAutoScale(cocos2d::CCEGLView* view,
- void* title,
- int width,
- int height,
- cocos2d::CCSize* supportDisplay,
- int displays,
- int defaultWidth,
- int defaultHeight)
- {
- return 0;
- }
- #endif
2:使用方法
(1) windows
修改AppDelegate.cpp文件:
- bool AppDelegate::initInstance()
- {
- bool bRet = false;
- do
- {
- #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
- CCSize sSupportDisplay[]={CCSize(480, 320), CCSize(1024, 768)};
- // Initialize OpenGLView instance, that release by CCDirector when application terminate.
- // The HelloWorld is designed as HVGA.
- CCEGLView * pMainWnd = new CCEGLView();
- CC_BREAK_IF(! pMainWnd);
- if (ViewAutoScale(pMainWnd, TEXT("Pyramid"),
- g_winWidth,
- g_winHeight,
- sSupportDisplay,
- sizeof(sSupportDisplay)/sizeof(CCSize),
- 480, 320) < 0)
- {
- return false;
- }
- #endif // CC_PLATFORM_WIN32
其中g_winWidth和g_winHeight为参数传递进来的窗口大小,这样在windows上可任意设定窗口大小,改成对应值即可。
(2) Android
修改jni/helloworld/main.cpp文件:
- void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv* env, jobject thiz, jint w, jint h)
- {
- cocos2d::CCSize sSupportDisplay[]={cocos2d::CCSize(480, 320)};
- if (!cocos2d::CCDirector::sharedDirector()->getOpenGLView())
- {
- cocos2d::CCEGLView *view = &cocos2d::CCEGLView::sharedOpenGLView();
- view->setFrameWidthAndHeight(w, h);
- // if you want to run in WVGA with HVGA resource, set it
- ViewAutoScale(view,
- NULL,
- w,
- h,
- sSupportDisplay,
- sizeof(sSupportDisplay)/sizeof(CCSize),
- 480, 320);
- ....
3:说明
(1) ViewAutoScale里我偷懒了,发现没有找到匹配分辨率时,就选择默认的分辨率。稍微改一下可以匹配最接近的分辨率
(2)目前实现的是等比缩放,资源按照目前屏幕大小适配。如果想实现铺满整个屏幕,修改如下:
windows下直接修改ViewAutoScale:
将view->setScreenScale(min((float)width/ defaultWidth, (float)height/ defaultHeight)); 修改为:
view->setScreenScale(max((float)width/ defaultWidth, (float)height/ defaultHeight));
android 下需要修改cocos2dx代码,在文件platform\android\CCEGLView_android.cpp 的void CCEGLView::create(int width, int height)中,修改如下:
// calculate the factor and the rect of viewport
m_fScreenScaleFactor = MAX((float)m_sSizeInPixel.width / m_sSizeInPoint.width,
(float)m_sSizeInPixel.height / m_sSizeInPoint.height);
(3)还没研究过动态调整适配:).