Cocos2d-x Default string constant encoding is UTF8, and the VC in Windows is the same as the system, such as Windows is GB2312 or GBK. Traditional is BIG5 code.
And most of us Chinese compiled with VC string constants are GBK encoded strings.
When drawing on the Cocos2d-x interface It is not known, only think this is the UTF8 string, the result is garbled.
The solution is also very simple. Find the header file add the following code
#ifdef WIN32
inline std::wstring AnsiToUnicode(
const char
* buf)
{
int len = ::MultiByteToWideChar(CP_ACP, 0, buf, -1, NULL, 0);
if (len == 0)
return L
""
;
std::wstring unicode;
unicode.resize(len);
::MultiByteToWideChar(CP_ACP, 0, buf, -1, &unicode[0], len);
return unicode;
}
inline std::string UnicodeToUtf8(
const wchar_t
* buf)
{
int len = ::WideCharToMultiByte(CP_UTF8, 0, buf, -1, NULL, 0, NULL, NULL);
if (len == 0)
return ""
;
std::string utf8;
utf8.resize(len);
::WideCharToMultiByte(CP_UTF8, 0, buf, -1, &utf8[0], len, NULL, NULL);
return utf8;
}
inline
std::string AnsioUTF8(
const char *strChar)
{
return UnicodeToUtf8(AnsiToUnicode(strChar).c_str());
}
//Windows中做编码转换,
#define UTEXT(str) AnsioUTF8(str)
#else
//Android/iOS中不处理.
#define UTEXT(str) str
#endif
|
When you need to use the Chinese string
CCLOG(UTEXT( "这是中文字符串!" )); Text* txt = Text::create(); txt->setString(UTEXT( "文本中的中文字符正常的!" )); |
Added, two days after this blog post was published, there was a more straightforward approach.
#ifdef WIN32 #pragma execution_character_set("utf-8") #endif |
The VC compiler will then encode the string constants as UTF8.
http://www.raysoftware.cn/?p=535
Cocos2d-x program Windows VC Chinese garbled solution (converted with MultiByteToWideChar, VC2010 have very strong execution_character_set)