cocos2d-x是一个不错的手机游戏框架,不过由于在中文的windows系统下使用visual stdio2008默认创建的类文件,包括.h和.cpp,其文件编码都是gb2312的。当将这些文件在cygwin上用ndk编译时,虽然编译没问题,但其中的中文在读取的时候会出现乱码,这是cocos2d-x的一个不足,例如CCLabelTTF显示中文的时候会出现乱码,而且CCSpriteFrame的spriteFrameByName方法也不能传入中文,这确实是一件头疼的事。
解决方法有两种,一种是将源代码文件保存为utf8格式,另一种方法在使用中文字符的时候转码,如果使用第二种方法的,为了跨平台,一般使用iconv这个库进行编码转换。但由于iconv是基于LGPL协议,cocos2d-x已经在最新的 cocos2d-1.0.1-x-0.12.0 release里将iconv的静态库去掉了,使用其实并不建议大量频繁地调用iconv进行编码转换,毕竟效率有所降低。
第二种方法则是将源代码文件编码转换为utf8编码,手动的方法是在visual stdio的文件菜单里有一个高级保存选项,将编码设为Unicode(UTF8带签名)然后保存即可,不过这种方法比较笨,纯粹是体力活。我研究过打算修改一下visual stdio的类向导,想把类向导创建的文件修改为utf8格式,但不得其果。如果是单纯的创建.h和.cpp文件则是可以的,方法是将Microsoft Visual Studio 9.0/VC/vcprojectitems下的newc++file.cpp和hfile.h保存为utf8格式,这样每次创建新的.h和.cpp都将会是utf8格式。但一般来说都会使用类向导创建类文件,所以我写了一个小程序,可以将源代码转换了utf8格式。代码如下:
// ChangeFileEncoding.cpp : 定义控制台应用程序的入口点。
//
#include "stdafx.h"
#include "ChangeFileEncoding.h"
#include <string>
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 唯一的应用程序对象
CWinApp theApp;
using namespace std;
void recursiveFile(CString strFileType);
void convertGBToUTF8(CString strWritePath, const char* gb2312);
int _tmain(int argc, TCHAR* argv[], TCHAR* envp[])
{
int nRetCode = 0;
// 初始化 MFC 并在失败时显示错误
if (!AfxWinInit(::GetModuleHandle(NULL), NULL, ::GetCommandLine(), 0))
{
// TODO: 更改错误代码以符合您的需要
_tprintf(_T("错误: MFC 初始化失败\n"));
nRetCode = 1;
}
else
{
/*for(int i = 0; i < argc; i++)
{
MessageBox(NULL, argv[i], L"Arglist contents", MB_OK);
}*/
//声明一个CFileFind类变量,以用来搜索
//接受一个参数作为源代码文件的根目录
TCHAR *lpszDirName = argv[1];
CString strFileType;
strFileType.Format(_T("%s\\*.*"), lpszDirName);
//递归此目录下的.h文件和.cpp文件,如果发现不是utf8编码则转换为utf8编码
recursiveFile(strFileType);
}
return nRetCode;
}
void recursiveFile( CString strFileType)
{
CFileFind finder;
BOOL isFinded = finder.FindFile(strFileType);//查找第一个文件
while(isFinded)
{
isFinded = finder.FindNextFile(); //递归搜索其他的文件
if(!finder.IsDots()) //如果不是"."目录
{
CString strFoundFile = finder.GetFilePath();
if(finder.IsDirectory()) //如果是目录,则递归地调用
{
CString strNextFileType;
strNextFileType.Format(_T("%s\\*.*"), strFoundFile);
recursiveFile(strNextFileType);
}
else
{
//如果是头文件或cpp文件
if(strFoundFile.Right(4) == _T(".cpp") || strFoundFile.Right(2) == _T(".h")) {
CFile fileReader(strFoundFile, CFile::modeRead);
byte head[3];
fileReader.Read(head, 3);
//判断是否带有BOM文件头
if(head[0] == 0xef && head[1]==0xbb && head[2] == 0xbf )
{
fileReader.Close();
continue;
}
fileReader.SeekToBegin();
int bufLength = 256;
char *buf = new char[bufLength];
ZeroMemory(buf, bufLength);
int nReadLength;
std::string strContent;
while((nReadLength = fileReader.Read(buf, bufLength)))
{
strContent.append(buf, nReadLength);
ZeroMemory(buf, nReadLength);
}
delete buf;
fileReader.Close();
convertGBToUTF8(strFoundFile, strContent.c_str());
}
}
}
}
finder.Close();
}
void convertGBToUTF8(CString strWritePath, const char* gb2312)
{
CFile fp;
fp.Open(strWritePath, CFile::modeCreate|CFile::modeWrite|CFile::typeBinary,NULL);
int len = MultiByteToWideChar(CP_ACP, 0, gb2312, -1, NULL, 0);
wchar_t* wstr = new wchar_t[len+1];
memset(wstr, 0, len+1);
MultiByteToWideChar(CP_ACP, 0, gb2312, -1, wstr, len);
len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, NULL, 0, NULL, NULL);
char* str = new char[len+1];
memset(str, 0, len+1);
len = WideCharToMultiByte(CP_UTF8, 0, wstr, -1, str, len, NULL, NULL);
if(wstr) delete[] wstr;
str[len] = '\n';
const unsigned char aryBOM[] = {0xEF, 0xBB, 0xBF};
fp.Write(aryBOM, sizeof(aryBOM));
fp.Write(str,len);
delete[] str;
fp.Close();
}
编码生成ChangeFileEncoding.exe后,将ChangeFileEncoding.exe放在cocos2d-x的主程序项目的根目录下里,例如项目的目录树如下:
Project
----------\cocos2dx
----------\CocosDenshion
----------\Box2D
----------\MainApplication
这里MainApplication就是主程序项目,将ChangeFileEncoding.exe放在此目录下,然后设置MainApplication的项目属性,在"生成事件" --"预生成事件" --"命令行"里填入“ChangeFileEncoding.exe ./Classes”即可。这样每次添加的文件虽然是gb2312编码,但每次在编译前会自动转换成utf8编码。
需要源代码的请留下邮箱。