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

来源:互联网 发布:网络视频监控系统方案 编辑:程序博客网 时间:2024/06/05 00:32

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

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



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



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

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

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. //! convert from wstring to UTF8 using self-coding-converting  
  2. void WStrToUTF8(std::string& dest, const std::wstring& src){  
  3.     dest.clear();  
  4.   
  5.     for (size_t i = 0; i < src.size(); i++){  
  6.         wchar_t w = src[i];  
  7.         if (w <= 0x7f)  
  8.             dest.push_back((char)w);  
  9.         else if (w <= 0x7ff)  
  10.         {  
  11.             dest.push_back(0xc0 | ((w >> 6)& 0x1f));  
  12.             dest.push_back(0x80| (w & 0x3f));  
  13.         }  
  14.         else if (w <= 0xffff)  
  15.         {  
  16.             dest.push_back(0xe0 | ((w >> 12)& 0x0f));  
  17.             dest.push_back(0x80| ((w >> 6) & 0x3f));  
  18.             dest.push_back(0x80| (w & 0x3f));  
  19.         }  
  20.         else if (sizeof(wchar_t) > 2 && w <= 0x10ffff)  
  21.         {  
  22.             dest.push_back(0xf0 | ((w >> 18)& 0x07)); // wchar_t 4-bytes situation  
  23.             dest.push_back(0x80| ((w >> 12) & 0x3f));  
  24.             dest.push_back(0x80| ((w >> 6) & 0x3f));  
  25.             dest.push_back(0x80| (w & 0x3f));  
  26.         }  
  27.         else  
  28.             dest.push_back('?');  
  29.     }  
  30. }  
  31.   
  32. //! simple warpper  
  33. std::string WStrToUTF8(const std::wstring& str)  
  34. {  
  35.     std::string result;  
  36.     WStrToUTF8(result, str);  
  37.     return result;  
  38. }  

在代码中使用方式如下:

[cpp] view plaincopy在CODE上查看代码片派生到我的代码片
  1. Button* button = dynamic_cast<Button*>(m_layout->getChildByTag(UI_BUTTON_START));  
  2. button->setTitleText(WStrToUTF8(L"运行"));  



就可以正确显示:

0 0