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

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

 

本站文章均为 李华明Himi 原创,转载务必在明显处注明:
转载自【黑米GameDev街区】 原文链接: http://www.himigame.com/android-game/799.html

          ☞ 点击订阅 ☜
 本博客最新动态!及时将最新博文通知您!

 

本篇主要介绍Cocos2dx项目开发过程中或者说项目务必遇到的一些知识点(ps.貌似Himi博客写的都是务必的 【iOS-cocos2d-X 游戏开发之十六】配置您的Cocos2dx项目编译后的Android自动使用(-hd)高清图&设置Android自适应屏幕、缩放比例  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
2
// 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 文件,就能看到:

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

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

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

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

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

 

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

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

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

    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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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   继续在你的项目下找到CCFileUtils_android.cpp  类,双击打开:

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
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;
}

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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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 中声明两个函数的定义:

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

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

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

 

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