频道栏目
首页 > 程序开发 > 移动开发 > 其他 > 正文
cocos2d-x多分布率适配(iphone5、ipad、android)
2013-06-08 08:09:32           
收藏   我要投稿

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

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

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


[cpp]
<SPAN style="WHITE-SPACE: pre"> </SPAN>// 填充模式  
    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); 
    } 

 // 填充模式
 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之音乐与音效
下一篇:游戏系统开发设计分享
相关文章
图文推荐
点击排行

关于我们 | 联系我们 | 广告服务 | 投资合作 | 版权申明 | 在线帮助 | 网站地图 | 作品发布 | Vip技术培训 | 举报中心

版权所有: 红黑联盟--致力于做实用的IT技术学习网站