[已解决]cocos2dx 中文显示问题,求帮助。

#ifndef SUPPORT_TOOL_H
#define SUPPORT_TOOL_H
//////////////////////////////////////////////////////////////////////////
#include “cocos2d.h”

//////////////////////////////////////////////////////////////////////////
#define A2U(varString) wSupportTool::AStrToUTF8(varString).c_str() //用于显示中文(窄字符)
#define W2U(varString) wSupportTool::WStrToUTF8(varString).c_str() //用于显示中文(宽字符)
//////////////////////////////////////////////////////////////////////////

class UTF8Tool
{
public:
//string to UTF8
static std::string AStrToUTF8(const std::string& src)
{
std::string curLocale=setlocale(LC_ALL,NULL);
setlocale(LC_ALL, “”);
int len=src.length()+1;
wchar_t *str =new wchar_t;
int total=mbstowcs(str,src.c_str(),len);
str=0;
std::wstring wstr=str;
delete] str;
setlocale(LC_ALL, curLocale.c_str());
return WStrToUTF8(wstr);
}

//wstring to UTF8  
static std::string WStrToUTF8(const std::wstring& src)  
{  

   std::string dest;  
   for (size_t i = 0; i < src.size(); i++){  
        wchar_t w = src*;  
       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('?');  
    }  
    return dest;  
}  

};
#endif

string Astr=A2U(“欢迎来到这个世界!”);
log(Astr.c_str());
LabelTTF::create(Astr, “Arial”, 14);
为什么这种方法在WIN32平台上面可以显示中文。但是一编绎成APK就运行不了,去掉这些代码之后,再编译又可以运行,希望高手指点一下,谢谢大家了
*

@偶尔e网事 希望大神能帮我解决一下,小弟不胜感谢

关于显示中文这块,我给个建议吧。你使用xml文件像android中的string.xml一样存入你要的中文字。然后在代码中使用ValueVector、ValueMap去读取,再显示

谢谢,我已经解决了,只需要把文件保存成UTF-8的型式就可以了

能贴出代码吗?

据我了解你如果把文件都保存为UTF-8在VS中运行或许会出现莫名其妙的错误的

代码其实不多的。
如果Chinese.plist文件如下


<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"/>
<plist version="1.0">
    <dict>
        <key>show</key>
        <string>中文显示</string>
    </dict>
</plist>

在代码中读取获取string,然后在Label中显示,这样中文就可以显示了


value_map = FileUtils::getInstance()->getValueMapFromFile("Chinese.plist");
 std::string info = value_map.at("show").asString();

auto label_1 = Label::createWithSystemFont(info, "Arial", 25);
label_1->setPosition(Vec2(visibleSize.width / 2, visibleSize.height / 2));
this->addChild(label_1, 1);

赞一个,啥,少于10个字,这样够了吧

恩,我也是用的这样的方法 :2:

保存为utf8 确实出了很多问题

mark一下 就这样子吧 撒的发生的

— Begin quote from ____

引用第2楼略丑于2014-07-21 15:30发表的 :
关于显示中文这块,我给个建议吧。你使用xml文件像android中的string.xml一样存入你要的中文字。然后在代码中使用ValueVector、ValueMap去读取,再显示 http://www.cocoachina.com/bbs/job.php?action=topost&tid=215686&pid=1009082

— End quote

目前比较靠谱的解决方案,而且不用担心vs的奇怪报错。