在mac上面用cocos2dx写程序然后移植到PC能运行的代码,在移植到安卓上面去~~
1.代码文件不能直接拿过来,应为文件的编码方式不同,在mac上面代码文件是utf-8编码格式,但是在pc上面,代码格式一般为ANSI编码格式,如果不装换格式,会出现各种莫名其妙的问题。解决方案,用记事本打开,然后保存为需要的编码格式,但是里面的汉字会乱码,这里就涉及到注释的问题了,所以以后注释要走向英文的注释途径~~
2.mac上面的资源都是要自己加入到工程里面, 也就没有路径可言,但是资源移植到pc上面来的时候,就要重新设计路径,包括头文件的路径!今天在运行DEBUG的时候成功了,但是在运行RELEASE的时候,工程属性设置有误,原来工程属性配置里面有有一个 《所有配置》 的设置,可以同事设置DEBUG和RELEASE的共有属性,其余的可以在单独的配置里面设置~~
3.工程里面要涉及到不同平台的通讯,手机端接收到pc端传来的数据,可能要从gb2312转换到utf-8格式,可以用到以下函数。
class utf
{
public:
static int msf_xchr_gb_to_utf8 (unsigned char *src, unsigned int *srcLen,unsigned char *dst,unsigned int *dstLen)
{
unsigned int w;
unsigned int sidx = 0, didx = 0;
unsigned int dn;
unsigned char mask;
/* Variables need for macro GBTOUNICODE defined in big5.h */
unsigned char c; /* Big5: MSB of character to convert */
unsigned char c1; /* Big5: LSB of character to convert */
unsigned char ku; /* Big5: Number of charset table */
unsigned char ten; /* Big5: Part of charset table */
/**************/
/* Conversion */
/**************/
for (;;) {
if(src[sidx]<0xA1){
/* Check for string end */
if (sidx + 1 > *srcLen) break;
/* One character encoding */
w=src[sidx];
}
else {
/* Check for string end */
if (sidx + 2 > *srcLen) break;
/* Two characters encoding */
c=src[sidx++];
c1=src[sidx];
w = (unsigned int)(GBTOUNICODE(c,c1,ku,ten));
}
/* Determine how many UTF8-bytes are needed for this character,
and which bits should be set in the first byte. */
if (w <0x80) {
dn = 1;
mask = 0;
}
else if (w <0x800) {
dn = 2;
mask = 0xc0;
}
else {
dn = 3;
mask = 0xe0;
}
/* Is there room for this in the destination vector? */
if (didx + dn > *dstLen)
break;
/* Only write to destination vector if dst != NULL */
if (dst != NULL) {
switch (dn) {
case 3:
dst[didx + 2] = (unsigned char) ((w & 0x3f) | 0x80);
w >>= 6;
case 2:
dst[didx + 1] = (unsigned char) ((w & 0x3f) | 0x80);
w >>= 6;
case 1:
dst[didx] = (unsigned char) (w | mask);
}
}
sidx += 1;
didx += dn;
}
*srcLen = sidx;
*dstLen = didx;
return 0;
}
static int msf_xchr_utf8_to_gb (const char *src, long *srcLen, char *dst, long *dstLen)
{
int sidx = 0, didx = 0;
int sn;
unsigned int w;
int hc;
const unsigned short *tmt_iso8859_tab = NULL;
/*****************************************/
/* Get Unicode character in UTF-8 format */
/*****************************************/
while (sidx < *srcLen) {
/* Read one Unicode character in UTF-8 format: */
sn = msf_charset_utf8_bytes_per_character[(unsigned char)*src >> 4];
if (sidx + sn > *srcLen)
break;
w = 0;
switch (sn) {
case 0:
return -1;
case 4: w += (unsigned char)*src++; w <<= 6;
case 3: w += (unsigned char)*src++; w <<= 6;
case 2: w += (unsigned char)*src++; w <<= 6;
case 1: w += (unsigned char)*src++;
}
w -= msf_charset_offsets_from_utf8[sn];
/**************/
/* Conversion */
/**************/
/* TO DO convert from UTF8 to GB2312 */
hc = '?'; /* This convertion is not supported at the moment */
if (dst != NULL) {
dst[didx] = (char)(hc);
}
sidx += sn;
didx += 1;
}
*srcLen = sidx;
*dstLen = didx;
return 0;
}
static int UTFGB_CONV(const char* flag1,const char* flag2,char* src,int srcLen,char* dst)
{
char *in = src;
char *out = dst;
size_t src_len = strlen((const char*)src);
size_t dst_len = sizeof(dst);
//size_t* outLen;
iconv_t cd = iconv_open(flag1, flag2);
//iconv(cd, &src, (size_t *)&srcLen, (char*)des,&outLen);
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
iconv(cd,&in,&src_len, &out, &dst_len); /* 执行转换 */
#endif
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
iconv(cd, (const char**)&in,&src_len, &out, &dst_len); /* 执行转换 */
#endif
dst = out;
iconv_close(cd); /* 执行清理 */
return dst_len;
}
//#ifndef ICONV_CONST
//# define ICONV_CONST const
//#endif
/*!
对字符串进行语言编码转换
param from 原始编码,比如"GB2312",的按照iconv支持的写
param to 转换的目的编码
param save 转换后的数据保存到这个指针里,需要在外部分配内存
param savelen 存储转换后数据的内存大小
param src 原始需要转换的字符串
param srclen 原始字符串长度
*/
static int utf_gb_convert(const char *from, const char *to, char* save, int savelen, char *src, int srclen)
{
iconv_t cd;
char *inbuf = src;
char *outbuf = save;
size_t outbufsize = savelen;
int status = 0;
size_t savesize = 0;
size_t inbufsize = srclen;
//const char* inptr = inbuf;
char* inptr = inbuf;
size_t insize = inbufsize;
char* outptr = outbuf;
size_t outsize = outbufsize;
cd = iconv_open(to, from);
iconv(cd,NULL,NULL,NULL,NULL);
if (inbufsize == 0) {
status = -1;
goto done;
}
while (insize > 0)
{
#if (CC_TARGET_PLATFORM == CC_PLATFORM_ANDROID)
size_t res = iconv(cd,&inptr,(size_t *)&insize,&outptr,(size_t *)&outsize);
#endif
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
size_t res = iconv(cd,(const char**)&inptr,(size_t *)&insize,&outptr,(size_t *)&outsize);
#endif
if (outptr != outbuf)
{
int saved_errno = errno;
int outsize = outptr - outbuf;
strncpy(save+savesize, outbuf, outsize);
errno = saved_errno;
}
if (res == (size_t)(-1)) {
if (errno == EILSEQ) {
int one = 1;
iconvctl(cd,ICONV_SET_DISCARD_ILSEQ,&one);
status = -3;
} else if (errno == EINVAL) {
if (inbufsize == 0) {
status = -4;
goto done;
} else {
break;
}
} else if (errno == E2BIG) {
status = -5;
goto done;
} else {
status = -6;
goto done;
}
}
}
status = strlen(save);
done:
iconv_close(cd);
return status;
}
};