【iOS-cocos2d-X 游戏开发之十六】Cocos2dx编译后的Android自动使用(-hd)高清图&设置自适应屏幕

简介:

 本篇主要介绍Cocos2dx项目开发过程中或者说项目务必遇到的一些知识点(ps.貌似Himi博客写的都是务必的 :tx:  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后双击打开,然后看到如下代码段:

 

 
 
  1. // if you want to run in WVGA with HVGA resource, set it 
  2.        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,设置代码如下:

 

 
 
  1. 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  首先在你的项目下找到  CCEGLView_android.cpp ,双击打开:

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

 

 
 
  1. void CCEGLView::create(int width, int height) 
  2.      
  3.     if (width == 0 || height == 0) 
  4.     { 
  5.         return; 
  6.     } 
  7.      
  8.     m_sSizeInPoint.width = width; 
  9.     m_sSizeInPoint.height = height; 
  10.      
  11.     // calculate the factor and the rect of viewport     
  12.     m_fScreenScaleFactor =  MIN((float)m_sSizeInPixel.width / m_sSizeInPoint.width, (float)m_sSizeInPixel.height / m_sSizeInPoint.height); 
  13.     CCLOG("CCEGLView::Create / Screen Scale Factor = %f", m_fScreenScaleFactor); 
  14.     if (m_fScreenScaleFactor >= 1.5f) 
  15.     { 
  16.         CCLOG("CCEGLView::Create / HD Scale Factor => Increase Content Scale Factor"); 
  17.         cocos2d::CCDirector::sharedDirector()->setContentScaleFactor(2.0f); 
  18.     } 
  19.     int viewPortW = (int)(m_sSizeInPoint.width * m_fScreenScaleFactor); 
  20.     int viewPortH = (int)(m_sSizeInPoint.height * m_fScreenScaleFactor); 
  21.     m_rcViewPort.origin.x = (m_sSizeInPixel.width - viewPortW) / 2; 
  22.     m_rcViewPort.origin.y = (m_sSizeInPixel.height - viewPortH) / 2; 
  23.     m_rcViewPort.size.width = viewPortW
  24.     m_rcViewPort.size.height = viewPortH
  25.      
  26.     m_bNotHVGA = true;     

 3.2   继续在你的项目下找到CCFileUtils_android.cpp  类,双击打开:

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

 

 
 
  1. const char* CCFileUtils::fullPathFromRelativePath(const char *pszRelativePath) 
  2.     
  3.     if (CC_CONTENT_SCALE_FACTOR() == 2.0f) 
  4.     { 
  5.          
  6.         //CC_RETINA_DISPLAY_FILENAME_SUFFIX 
  7.         // verifier si suffix deja present 
  8.         std::string path = pszRelativePath; 
  9.         std::string::size_type pos = path.rfind("/") + 1// the begin index of last part of path 
  10.          
  11.         std::string::size_type suffixPos = path.rfind(CC_RETINA_DISPLAY_FILENAME_SUFFIX); 
  12.         if ((std::string::npos != suffixPos) && (suffixPos > pos)) 
  13.         { 
  14.             // => if yes, return path directly 
  15.         } 
  16.         else 
  17.         { 
  18.             // => if no, add "retina"/hd suffix and test if file exist 
  19.             CCString *pRet = new CCString(); 
  20.             pRet->autorelease(); 
  21.             pRet->m_sString = path.substr(0, path.rfind(".")) + CC_RETINA_DISPLAY_FILENAME_SUFFIX + path.substr(path.rfind("."), path.length()); 
  22.              
  23.             if (existFileData(pRet->m_sString.c_str())) 
  24.             { 
  25.                 //    => if yes, return path with suffix 
  26.                 CCLog("cocos2d: FilePath(%s) with suffix(%s) exist, use it.", pRet->m_sString.c_str(), CC_RETINA_DISPLAY_FILENAME_SUFFIX); 
  27.                  
  28.                 return pRet->m_sString.c_str(); 
  29.             } 
  30.             else 
  31.             { 
  32.                 //    => if no, return path without suffix 
  33.             } 
  34.         } 
  35.     }    
  36.      
  37.     return pszRelativePath; 

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

 

 
 
  1. bool CCFileUtils::existFileData(const char* pszFileName) 
  2.     string fullPath(pszFileName); 
  3.      
  4.     if ((! pszFileName)) 
  5.     { 
  6.         return false
  7.     } 
  8.      
  9.     if (pszFileName[0] != '/'
  10.     { 
  11.         // read from apk 
  12.         fullPath.insert(0"assets/"); 
  13.         return CCFileUtils::existFileDataFromZip(s_strResourcePath.c_str(), fullPath.c_str()); 
  14.     } 
  15.     else 
  16.     { 
  17.         do 
  18.         { 
  19.             // read rrom other path than user set it 
  20.             FILE *fp = fopen(pszFileName, "rb"); 
  21.             if (fp != NULL) 
  22.             { 
  23.                 fclose(fp); 
  24.                 return true
  25.             } 
  26.         } 
  27.         while (0); 
  28.     } 
  29.     return false
  30. bool CCFileUtils::existFileDataFromZip(const char* pszZipFilePath, const char* pszFileName) 
  31.     unzFile pFile = NULL; 
  32.     bool res = false
  33.     do 
  34.     { 
  35.         CC_BREAK_IF(!pszZipFilePath || !pszFileName); 
  36.         CC_BREAK_IF(strlen(pszZipFilePath) == 0); 
  37.          
  38.         pFile = unzOpen(pszZipFilePath); 
  39.          
  40.         int nRet = unzLocateFile(pFile, pszFileName, 1); 
  41.         res = UNZ_OK == nRet; 
  42.          
  43.     } while (0); 
  44.      
  45.     if (pFile) 
  46.     { 
  47.         unzClose(pFile); 
  48.     } 
  49.     return res; 

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

 

 
 
  1. static bool existFileData(const char* pszFileName); 
  2. static bool existFileDataFromZip(const char* pszZipFilePath, const char* pszFileName); 

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

 

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










本文转自 xiaominghimi 51CTO博客,原文链接:http://blog.51cto.com/xiaominghimi/969777,如需转载请自行联系原作者
目录
相关文章
|
编解码 视频直播 Android开发
节选—Android 视频直播 ( 从快播到直播,从高清到无码 )十年视频开发项目
本文转载自Android 视频直播 ( 从快播到直播,从高清到无码 )十年视频开发项目,截取其中技术概念比较相关的部分,并做了重新的排版。
2124 0