cocos2d-x自适应屏幕分辨率

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


  • 2
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 10
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 10
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值