在 cocos2d-x 3.0 中中文显示为乱码的问题

时间:2023-02-09 08:42:49

开发环境:Windows 8   VS2012  cocos2d-x 3.0 beta cocostudio 1.2

当通过 cocostudio UI编辑 编辑了一个界面,如下所示

在 cocos2d-x 3.0 中中文显示为乱码的问题


当加载到 cocos2d-x代码中,但是在设置按钮文字时会出现乱码。

在 cocos2d-x 3.0 中中文显示为乱码的问题


本人是通过如下方法转换编码:

以下代码来源于【tkokof1博客】 http://blog.csdn.net/tkokof1/article/details/7426706

//! convert from wstring to UTF8 using self-coding-converting
void WStrToUTF8(std::string& dest, const std::wstring& src){
dest.clear();

for (size_t i = 0; i < src.size(); i++){
wchar_t w = src[i];
if (w <= 0x7f)
dest.push_back((char)w);
else if (w <= 0x7ff)
{
dest.push_back(0xc0 | ((w >> 6)& 0x1f));
dest.push_back(0x80| (w & 0x3f));
}
else if (w <= 0xffff)
{
dest.push_back(0xe0 | ((w >> 12)& 0x0f));
dest.push_back(0x80| ((w >> 6) & 0x3f));
dest.push_back(0x80| (w & 0x3f));
}
else if (sizeof(wchar_t) > 2 && w <= 0x10ffff)
{
dest.push_back(0xf0 | ((w >> 18)& 0x07)); // wchar_t 4-bytes situation
dest.push_back(0x80| ((w >> 12) & 0x3f));
dest.push_back(0x80| ((w >> 6) & 0x3f));
dest.push_back(0x80| (w & 0x3f));
}
else
dest.push_back('?');
}
}

//! simple warpper
std::string WStrToUTF8(const std::wstring& str)
{
std::string result;
WStrToUTF8(result, str);
return result;
}

在代码中使用方式如下:

Button* button = dynamic_cast<Button*>(m_layout->getChildByTag(UI_BUTTON_START));
button->setTitleText(WStrToUTF8(L"运行"));



就可以正确显示:

在 cocos2d-x 3.0 中中文显示为乱码的问题