cocos2d-x自适应屏幕分辨率

Android下分辨率太多,不太可能为每种分辨率做一套资源,目前一般来说比较流行的是320*480, 800*400, 854*400。当然现在720P的也出来了,但至少目前不是主流机型^_^.

对于不支持的分辨率,我希望的是能够按照屏幕大小按比例缩放,即有了下面的代码。

1:ViewAutoScale

写了一个ViewAutoScale函数,如下:

 

  1. #include "ViewAutoScale.h"  
  2. USING_NS_CC;  
  3.   
  4. bool IsMatchDisplay(int w, int h, CCSize& size )  
  5. {  
  6.     return (w==size.width && h==size.height) || (h==size.width && w==size.height);  
  7. }  
  8.   
  9. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)  
  10. int ViewAutoScale(cocos2d::CCEGLView* view,   
  11.                   void* title,  
  12.                   int width,   
  13.                   int height,  
  14.                   cocos2d::CCSize* supportDisplay,  
  15.                   int displays,  
  16.                   int defaultWidth,  
  17.                   int defaultHeight)  
  18. {  
  19.     if(view == NULL)  
  20.     {  
  21.         return -1;  
  22.     }  
  23.     for (int i=0; i < displays; i++)  
  24.     {  
  25.         if (IsMatchDisplay(width, height, supportDisplay[i]))  
  26.         {  
  27.             view->Create((LPCTSTR)title, width, height);  
  28.             return i+1;  
  29.         }  
  30.     }  
  31.     view->Create((LPCTSTR)title, defaultWidth, defaultHeight);  
  32.   
  33.     view->setScreenScale(min((float)width/ defaultWidth, (float)height/ defaultHeight));  
  34.     view->resize(width, height);  
  35.     view->centerWindow();  
  36.     return 0;  
  37. }  
  38.   
  39. #endif  
  40.   
  41. #if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)  
  42. int ViewAutoScale(cocos2d::CCEGLView* view,   
  43.                   void* title,  
  44.                   int width,   
  45.                   int height,  
  46.                   cocos2d::CCSize* supportDisplay,  
  47.                   int displays,  
  48.                   int defaultWidth,  
  49.                   int defaultHeight)  
  50. {  
  51.     if(view == NULL)  
  52.     {  
  53.         return -1;  
  54.     }  
  55.     for (int i=0; i < displays; i++)  
  56.     {  
  57.         if (IsMatchDisplay(width, height, supportDisplay[i]))  
  58.         {  
  59.             return i+1;  
  60.         }  
  61.     }  
  62.     view->create(defaultWidth, defaultHeight);  
  63.     return 0;  
  64. }  
  65. #endif  
  66.   
  67. #if (CC_TARGET_PLATFORM == CC_PLATFORM_IOS)  
  68. int ViewAutoScale(cocos2d::CCEGLView* view,   
  69.                   void* title,  
  70.                   int width,   
  71.                   int height,  
  72.                   cocos2d::CCSize* supportDisplay,  
  73.                   int displays,  
  74.                   int defaultWidth,  
  75.                   int defaultHeight)  
  76. {  
  77.     return 0;  
  78. }  
  79. #endif  


2:使用方法

 

(1) windows

修改AppDelegate.cpp文件:

 

  1. bool AppDelegate::initInstance()  
  2. {  
  3.     bool bRet = false;  
  4.     do   
  5.     {  
  6. #if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)  
  7.         CCSize sSupportDisplay[]={CCSize(480, 320), CCSize(1024, 768)};  
  8.         // Initialize OpenGLView instance, that release by CCDirector when application terminate.  
  9.         // The HelloWorld is designed as HVGA.  
  10.         CCEGLView * pMainWnd = new CCEGLView();  
  11.         CC_BREAK_IF(! pMainWnd);  
  12.         if (ViewAutoScale(pMainWnd, TEXT("Pyramid"),   
  13.                             g_winWidth,   
  14.                             g_winHeight,   
  15.                             sSupportDisplay,   
  16.                             sizeof(sSupportDisplay)/sizeof(CCSize),  
  17.                             480, 320) < 0)  
  18.         {  
  19.             return false;  
  20.         }  
  21. #endif  // CC_PLATFORM_WIN32  


其中g_winWidth和g_winHeight为参数传递进来的窗口大小,这样在windows上可任意设定窗口大小,改成对应值即可。

 

 

(2) Android

修改jni/helloworld/main.cpp文件:

 

  1. void Java_org_cocos2dx_lib_Cocos2dxRenderer_nativeInit(JNIEnv*  env, jobject thiz, jint w, jint h)  
  2. {  
  3.     cocos2d::CCSize sSupportDisplay[]={cocos2d::CCSize(480, 320)};  
  4.     if (!cocos2d::CCDirector::sharedDirector()->getOpenGLView())  
  5.     {  
  6.     cocos2d::CCEGLView *view = &cocos2d::CCEGLView::sharedOpenGLView();  
  7.         view->setFrameWidthAndHeight(w, h);  
  8.         // if you want to run in WVGA with HVGA resource, set it  
  9.         ViewAutoScale(view,  
  10.                     NULL,  
  11.                     w,  
  12.                     h,  
  13.                     sSupportDisplay,  
  14.                     sizeof(sSupportDisplay)/sizeof(CCSize),  
  15.                     480, 320);  
  16. ....  



 

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)还没研究过动态调整适配:).

posted on 2011-11-02 15:39  游戏开发:主席  阅读(9085)  评论(0编辑  收藏  举报

导航