【Cocos2d-X】中文亂碼問題,cocos2d-x中文亂碼

來源:互聯網
上載者:User

【Cocos2d-X】中文亂碼問題,cocos2d-x中文亂碼

初學Cocos就遇到了中文的顯示問題,在使用CCLabelTTF調用系統字型時,出現的是亂碼;使用CCLabelBMFont調用自己設計的fnt檔案時,就會什麼都不顯示。百度了很久之後才明白,字型檔需要的中文得是UTF-8編碼的,而項目又是Unicode,所以我們只需要把中文字串的Unicode編碼轉化為UTF-8就行了。

Unicode轉UTF-8函數:

char* EncodeToUTF8(const char* mbcsStr){    wchar_t*  wideStr;    char*     utf8Str;    int       charLen;    charLen = MultiByteToWideChar(936, 0, mbcsStr, -1, NULL, 0);     wideStr = (wchar_t*)malloc(sizeof(wchar_t)*charLen);    MultiByteToWideChar(936, 0, mbcsStr, -1, wideStr, charLen);    charLen = WideCharToMultiByte(CP_UTF8, 0, wideStr, -1, NULL, 0, NULL, NULL);    utf8Str = (char*)malloc(charLen);    WideCharToMultiByte(CP_UTF8, 0, wideStr, -1, utf8Str, charLen, NULL, NULL);    free(wideStr);    return utf8Str;}

使用樣本:

CCLabelTTF* pLabel = CCLabelTTF::create(EncodeToUTF8("海賊王"), "Marker Felt", 24);    CCLabelBMFont* label = CCLabelBMFont::create(EncodeToUTF8("海賊王"), "fonts/_chapter7.fnt",24);    // position the label on the center of the screen    pLabel->setPosition(ccp(origin.x + visibleSize.width/2 -100,                            origin.y + visibleSize.height - pLabel->getContentSize().height));    label->setPosition(ccp(origin.x + visibleSize.width / 2 + 100,        origin.y + visibleSize.height - label->getContentSize().height));    // add the label as a child to this layer    this->addChild(pLabel, 1);    this->addChild(label, 1);


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.