cocos2d-x 显示中文字符

解决显示中文乱码问题。

1,、cocos2d-x 显示中文字符
首先包含iconv.h头文件,c++->常规->附加包含目录:cocos2dx\platform\third_party\win32\iconv

2、新建tool.h,内容如下

#ifndef _TOOLS_H_
#define  _TOOLS_H_


#include "cocos2d.h"


#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
#include "iconv.h"
#endif


#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
#pragma comment(lib,"libiconv.lib")
#endif


float heronsformula(float x1,float y1,float x2,float y2,float x3,float y3);


bool triangleContainPoint(float x1,float y1,float x2,float y2,float x3,float y3,float px,float py);


#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
//字符转换,使cocos2d-x在win32平台支持中文显示
int GBKToUTF8(std::string &gbkStr,const char* toCode,const char* formCode);
#endif


#endif //_TOOLS_H_


新建tool.cpp,内容如下


#include "tools.h"


float heronsformula(float x1,float y1,float x2,float y2,float x3,float y3)
{
float a=sqrt(pow(x1-x2,2)+pow(y1-y2,2));
float b=sqrt(pow(x2-x3,2)+pow(y2-y3,2));
float c=sqrt(pow(x3-x1,2)+pow(y3-y1,2));
float s=(a+b+c)/2;


return sqrt(s*(s-a)*(s-b)*(s-c));
}


bool triangleContainPoint(float x1,float y1,float x2,float y2,float x3,float y3,float px,float py)
{
float s1=heronsformula(x1,y1,x2,y2,px,py);
float s2=heronsformula(x2,y2,x3,y3,px,py);
float s3=heronsformula(x3,y3,x1,y1,px,py);
float s=heronsformula(x1,y1,x2,y2,x3,y3);


return abs(s-(s1+s2+s3))<0.001f;
}


#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
//字符转换,使cocos2d-x在win32平台支持中文显示
int GBKToUTF8(std::string &gbkStr,const char* toCode,const char* formCode)
{
iconv_t iconvH;
iconvH = iconv_open(formCode,toCode);
if(iconvH == 0)
{
return -1;
}


const char* strChar = gbkStr.c_str();
const char** pin = &strChar;


size_t strLength = gbkStr.length();
char* outbuf = (char*)malloc(strLength*4);
char* pBuff = outbuf;
memset(outbuf,0,strLength*4);
size_t outLength = strLength*4;
if(-1 == iconv(iconvH,pin,&strLength,&outbuf,&outLength))
{
iconv_close(iconvH);
return -1;
}


gbkStr = pBuff;
iconv_close(iconvH);
return 0;
}
#endif

3、具体使用实例

std::string menuItemStr = "单人游戏";
#if (CC_TARGET_PLATFORM == CC_PLATFORM_WIN32)
GBKToUTF8(menuItemStr,"gb2312","utf-8");
#endif

CCLabelTTF *labelSingleGame = CCLabelTTF::create(menuItemStr.c_str(), "Arial", 17); 
labelSingleGame->setPosition(ccp(origin.x + 800*scalex, origin.y + 550*scaley));


搞定....

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值