cocos2d-x多分布率适配(iphone5、ipad、android)

游戏已经提交app store等待审核了,现在整理下相关资料。先说说分辨率的问题。

cocos2d-x从2.0.4版本已经完美适配了各种分辨率,下面贴下相关代码,

在AppDelegate::applicationDidFinishLaunching(){}里面加这些代码:

 

// 填充模式

	ResolutionPolicy kResolutionNum = kResolutionNoBorder;



	if (target == kTargetIpad)

	{

		// ipad

		CCFileUtils::sharedFileUtils()->setResourceDirectory("iphonehd");



		// don't enable retina because we don't have ipad hd resource

		CCEGLView::sharedOpenGLView()->setDesignResolutionSize(1024, 768, kResolutionNum);

	}

	else if (target == kTargetIphone)

	{

		// iphone



		// 设计尺寸width、height

		float visibleSizeWidth = 960;

		float visibleSizeHeight = 640;



		// 根据屏幕尺寸与设计尺寸的比,重新定义设计尺寸(参考kResolutionNum来适配)

		CCSize winSize = CCDirector::sharedDirector()->getWinSize();

		if (winSize.width/winSize.height > visibleSizeWidth/visibleSizeHeight)

		{

			visibleSizeWidth = visibleSizeHeight * winSize.width/winSize.height;

		}

		else if (winSize.width/winSize.height < visibleSizeWidth/visibleSizeHeight)

		{

			visibleSizeHeight = visibleSizeWidth / (winSize.width/winSize.height);

		}



		CCFileUtils::sharedFileUtils()->setResourceDirectory("iphonehd");

		CCSize VisibleSize = CCEGLView::sharedOpenGLView()->getVisibleSize();

		CCEGLView::sharedOpenGLView()->setDesignResolutionSize(visibleSizeWidth, visibleSizeHeight, kResolutionNum);

	}

	else 

	{

		// android, windows, blackberry, linux or mac

		// use 960*640 resources as design resolution size

		

		// 设计尺寸width、height

		float visibleSizeWidth = 960;

		float visibleSizeHeight = 640;



		// 根据屏幕尺寸与设计尺寸的比,重新定义设计尺寸(参考kResolutionNum来适配)

		CCSize winSize = CCDirector::sharedDirector()->getWinSize();

		if (winSize.width/winSize.height > visibleSizeWidth/visibleSizeHeight)

		{

			visibleSizeWidth = visibleSizeHeight * winSize.width/winSize.height;

		}

		else if (winSize.width/winSize.height < visibleSizeWidth/visibleSizeHeight)

		{

			visibleSizeHeight = visibleSizeWidth / (winSize.width/winSize.height);

		}

		

		CCFileUtils::sharedFileUtils()->setResourceDirectory("iphonehd");

		CCEGLView::sharedOpenGLView()->setDesignResolutionSize(visibleSizeWidth, visibleSizeHeight, kResolutionNum);

		CCFileUtils::sharedFileUtils()->setPopupNotify(false);

	}

 

 

 

注意:1.适配iphone5时要添加一张启动图片,图片命名为Default-568h@2x.png,不然iphone5两边还是黑边。

     2.setDesignResolutionSize()是指设计尺寸,而getWinSize()是窗口尺寸,显示时是把设计尺寸按照设置的填充模式映射到窗口上去。

     3.三种填充模式:kResolutionExactFit(宽高非等比缩放,全填充)、kResolutionNoBorder(宽高等比缩放,部分区域可能被裁减)、kResolutionShowAll(宽高等比缩放,部分区域可能出现黑边),我觉得用kResolutionNoBorder好些。

你可能感兴趣的:(cocos2d-x)