• 首页 首页 icon
  • 工具库 工具库 icon
    • IP查询 IP查询 icon
  • 内容库 内容库 icon
    • 快讯库 快讯库 icon
    • 精品库 精品库 icon
    • 问答库 问答库 icon
  • 更多 更多 icon
    • 服务条款 服务条款 icon

iOS-cocos2d-X 游戏开发:十六配置你的Cocos2dx项目编译后的Android自动使用(-hd)高清图和amp;设置Android自适应屏幕、缩放比例...

武飞扬头像
lishuai052086
帮助1

本篇主要介绍Cocos2dx项目开发过程中或者说项目务必遇到的一些知识点(ps.貌似Himi博客写的都是务必的 

  Himi认为写别人没写的才更容易吸引人不是~)

OK,不多说废话,第一个介绍的是修改项目配置让你的Android项目支持自适应屏幕;其实关于Android项目自适应屏幕这个问题,Himi实在不想再多费口舌,一方面因为Himi之前博文有说过,另外一方面现在Android开源缘故造成分辨率泛滥也成必然。大家注意做项目尽可能使用相对位置,别写死坐标,另外一点就是针对流行分辨率做适应就好了,如果你们公司很有必要铺Android市场的量,那么只能一个一个分辨率去搞了=。 = Himi身为Kjava(J2me)一路走过来的Dev来说,我是在是对自适应虐到习惯…..

1.  咳咳,本不想说,回到正题,那么对于Cocos2dx中如何设置项目Android版自适应,其实很easy,直接在编译好的Android项目中如下路径查找:

your Project name/Jni/helloworld/main.cpp

OK,找到main.cpp后双击打开,然后看到如下代码段:

// if you want to run in WVGA with HVGA resource, set it          

           view->create(           480           ,            320           );            // Please change it to (320, 480);if you're in portrait mode.

view->create(480,320);默认关闭的,这里打开即可;其实Himi也是从cocos2dx引擎框架中看到的,打开你的任意一个cocos2dx引擎框架的项目,然后打开AppDelegate.cpp 文件,就能看到:

2. 下面继续介绍如何让你的cocos2dx-Android项目设置缩放比例,一样很easy,设置代码如下:

CCDirector::sharedDirector()->setContentScaleFactor(2.0);

默认值是1.0,缩放2倍,从下面这两张图可以明显看出设置后的区别:(点击放大图片)

为了便于后续讲解更容易理解,那么这里Himi博文讲解使用的两行图片这里先给出,大家先看下:

rect.png     规格: 40*40         |            rect-hd.png 规格:80*80

    3.下面介绍如何让cocos2dx的Android版项目使用iOS Retina类似@2x的-hd功能也直接使用高清图片,当然cocos2dx引擎默认找的高清图为-hd;但是编译Xcode的cocos2dx项目到Android版后,Android版可不会那么聪明自动使用你的-hd的版图片,所以下面Himi来手把手教你设置;具体步骤如下:

 3.1

找到  void CCEGLView::create(int width, int height) 函数,然后函数内替换成如下代码:

void           CCEGLView::create(           int           width,            int           height)          

           {          

                      

                      if           (width == 0 || height == 0)          

                      {          

                      return           ;          

                      }          

                      

                      m_sSizeInPoint.width = width;          

                      m_sSizeInPoint.height = height;          

                      

                      // calculate the factor and the rect of viewport          

                      m_fScreenScaleFactor =  MIN((           float           )m_sSizeInPixel.width / m_sSizeInPoint.width, (           float           )m_sSizeInPixel.height / m_sSizeInPoint.height);          

                      CCLOG(           "CCEGLView::Create / Screen Scale Factor = %f"           , m_fScreenScaleFactor);          

                      if           (m_fScreenScaleFactor >= 1.5f)          

                      {          

                      CCLOG(           "CCEGLView::Create / HD Scale Factor => Increase Content Scale Factor"           );          

                      cocos2d::CCDirector::sharedDirector()->setContentScaleFactor(2.0f);          

                      }          

                      int           viewPortW = (           int           )(m_sSizeInPoint.width * m_fScreenScaleFactor);          

                      int           viewPortH = (           int           )(m_sSizeInPoint.height * m_fScreenScaleFactor);          

                      m_rcViewPort.origin.x = (m_sSizeInPixel.width - viewPortW) / 2;          

                      m_rcViewPort.origin.y = (m_sSizeInPixel.height - viewPortH) / 2;          

                      m_rcViewPort.size.width = viewPortW;          

                      m_rcViewPort.size.height = viewPortH;          

                      

                      m_bNotHVGA =            true           ;          

           }

 3.2

找到  const char* CCFileUtils::fullPathFromRelativePath(const char *pszRelativePath) 函数,然后替换如下内容:

const           char           * CCFileUtils::fullPathFromRelativePath(           const           char           *pszRelativePath)          

           {          

                      

                      if           (CC_CONTENT_SCALE_FACTOR() == 2.0f)          

                      {          

                      

                      //CC_RETINA_DISPLAY_FILENAME_SUFFIX          

                      // verifier si suffix deja present          

                      std::string path = pszRelativePath;          

                      std::string::size_type pos = path.rfind(           "/"           )   1;            // the begin index of last part of path          

                      

                      std::string::size_type suffixPos = path.rfind(CC_RETINA_DISPLAY_FILENAME_SUFFIX);          

                      if           ((std::string::npos != suffixPos) && (suffixPos > pos))          

                      {          

                      // => if yes, return path directly          

                      }          

                      else          

                      {          

                      // => if no, add "retina"/hd suffix and test if file exist          

                      CCString *pRet =            new           CCString();          

                      pRet->autorelease();          

                      pRet->m_sString = path.substr(0, path.rfind(           "."           ))   CC_RETINA_DISPLAY_FILENAME_SUFFIX   path.substr(path.rfind(           "."           ), path.length());          

                      

                      if           (existFileData(pRet->m_sString.c_str()))          

                      {          

                      //    => if yes, return path with suffix          

                      CCLog(           "cocos2d: FilePath(%s) with suffix(%s) exist, use it."           , pRet->m_sString.c_str(), CC_RETINA_DISPLAY_FILENAME_SUFFIX);          

                      

                      return           pRet->m_sString.c_str();          

                      }          

                      else          

                      {          

                      //    => if no, return path without suffix          

                      }          

                      }          

                      }             

                      

                      return           pszRelativePath;          

           }

然后接着在本类添加如下两个函数:

bool           CCFileUtils::existFileData(           const           char           * pszFileName)          

           {          

                      string fullPath(pszFileName);          

                      

                      if           ((! pszFileName))          

                      {          

                      return           false           ;          

                      }          

                      

                      if           (pszFileName[0] !=            '/'           )          

                      {          

                      // read from apk          

                      fullPath.insert(0,            "assets/"           );          

                      return           CCFileUtils::existFileDataFromZip(s_strResourcePath.c_str(), fullPath.c_str());          

                      }          

                      else          

                      {          

                      do          

                      {          

                      // read rrom other path than user set it          

                      FILE           *fp =            fopen           (pszFileName,            "rb"           );          

                      if           (fp != NULL)          

                      {          

                      fclose           (fp);          

                      return           true           ;          

                      }          

                      }          

                      while           (0);          

                      }          

                      return           false           ;          

           }          

           bool           CCFileUtils::existFileDataFromZip(           const           char           * pszZipFilePath,            const           char           * pszFileName)          

           {          

                      unzFile pFile = NULL;          

                      bool           res =            false           ;          

                      do          

                      {          

                      CC_BREAK_IF(!pszZipFilePath || !pszFileName);          

                      CC_BREAK_IF(           strlen           (pszZipFilePath) == 0);          

                      

                      pFile = unzOpen(pszZipFilePath);          

                      

                      int           nRet = unzLocateFile(pFile, pszFileName, 1);          

                      res = UNZ_OK == nRet;          

                      

                      }            while           (0);          

                      

                      if           (pFile)          

                      {          

                      unzClose(pFile);          

                      }          

                      return           res;          

           }

最后在CCFileUtils.h 中声明两个函数的定义:

static           bool           existFileData(           const           char           * pszFileName);          

           static           bool           existFileDataFromZip(           const           char           * pszZipFilePath,            const           char           * pszFileName);

3.3  最后记得设置缩放比例的值2.0,那么重新编译你的项目到Android运行则如下图所示:

OK,本篇就到这里,Himi最近感冒,还没吃晚饭,咳咳,先晚饭去了。。。北京最近下雨天气偏凉~大家多注意身体,

这篇好文章是转载于:学新通技术网

  • 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
  • 本站站名: 学新通技术网
  • 本文地址: /boutique/detail/tanhigkkhf
系列文章
更多 icon
同类精品
更多 icon
继续加载