当前位置:编程学习 > wap >>

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

游戏已经提交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()->setDesi

补充:移动开发 , 其他 ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,