VS2010中Cocos2d-x中文乱码问题

不罗嗦,直接进入主题,VS2010的默认编码是"GB2312",那么以创建一个label为例,当我们使用

CCLabelTTF::create(const char *label, const char *fontName, float fontSize)

来创建label的时候,const char* 参数要求是utf-8编码的字符串,如果传入一个非utf-8编码的字符串,那么创建的label显示就会有些不正常。

这是一个编译器相关的问题,和源文件保存的编码没有太大关系。

使用VS C++开发,如果硬要解决这个问题,可以将文件保存为"无签名的UTF-8"编码。

但是在VS C++中使用"无签名的UTF-8"又会带来新的问题

#include "stdafx.h"
int _tmain(int argc, _TCHAR* argv[])
{
  const char str[] = "退出";
  return 0;
}

然后保存为"无签名的UTF-8"并编译

1>: warning C4819: 该文件包含不能在当前代码页(936)中表示的字符。请将该文件保存为 Unicode 格式以防止数据丢失
1>: error C2001: 常量中有换行符
1>: error C2143: 语法错误 : 缺少“;”(在“return”的前面)

有人提交过类似的问题,根据微软的解释:

The compiler when faced with a source file that does not have a BOM the compiler reads ahead a certain distance into the file to see if it can detect any Unicode characters - it specifically looks for UTF-16 and UTF-16BE - if it doesn't find either then it assumes that it has MBCS. I suspect that in this case that in this case it falls back to MBCS and this is what is causing the problem.

所以微软也没打算解决这个问题。

使用其他的编译器是否会有这个问题。但是最好的解决方案还是将文字全部都放到外部资源文件中,然后提供一套文本资源获取的接口。

posted @ 2013-12-12 14:54  iak  阅读(309)  评论(0编辑  收藏  举报