天天看點

【IOS-COCOS2D-X 遊戲開發之十六】配置你的COCOS2DX項目編譯後的ANDROID自動使用(-HD)高清圖&設定ANDROID自适應螢幕、縮放比例方法!

本篇主要介紹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

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,設定代碼如下:

ccdirector::shareddirector()->setcontentscalefactor(2.0);

預設值是1.0,縮放2倍,從下面這兩張圖可以明顯看出設定後的差別:(點選放大圖檔)

【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) 函數,然後函數内替換成如下代碼:

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) 函數,然後替換如下内容:

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;

然後接着在本類添加如下兩個函數:

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 中聲明兩個函數的定義:

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最近感冒,還沒吃晚飯,咳咳,先晚飯去了。。。北京最近下雨天氣偏涼~大家多注意身體,

繼續閱讀