展会信息港展会大全

【Cocos2d-X】中文乱码问题,cocos2d-x中文乱码
来源:互联网   发布日期:2015-09-28 11:25:56   浏览:1973次  

导读: 【Cocos2d-X】中文乱码问题,cocos2d-x中文乱码 初学Cocos就遇到了中文的显示问题,在使用CCLabelTTF调用系统字体时,出现的是乱码;使用CCLabelB...

【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;

intcharLen;

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);

效果图:

http://www.bkjia.com/Androidjc/989776.htmlwww.bkjia.comtruehttp://www.bkjia.com/Androidjc/989776.htmlTechArticle【Cocos2d-X】中文乱码问题,cocos2d-x中文乱码 初学Cocos就遇到了中文的显示问题,在使用CCLabelTTF调用系统字体时,出现的是乱码;使用CCLab...

赞助本站

人工智能实验室

相关热词: android开发 android教程

AiLab云推荐
展开

热门栏目HotCates

Copyright © 2010-2024 AiLab Team. 人工智能实验室 版权所有    关于我们 | 联系我们 | 广告服务 | 公司动态 | 免责声明 | 隐私条款 | 工作机会 | 展会港