(转) cocos2d-x自适应屏幕分辨率

原文地址:http://blog.csdn.net/dragoncheng/article/details/6927687

Android下分辨率太多,不太可能为每种分辨率做一套资源,目前一般来说比较流行的是320*480, 800*480, 854*480。当然现在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),cocos2d::CCSize(800,480),cocos2d::CCSize(854,480)};  
 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)还没研究过动态调整适配:).

转载于:https://www.cnblogs.com/FireStudio/archive/2012/07/26/2609868.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值