cocos2dx中使用中文

方法一 使用 xml

在vs中创建xml

<?xml version="1.0" encoding="UTF-8"?>

<plist version="1.0">

<dict>

	
<key>startGame</key>
	
<string>开始游戏</string>

	
<key>japanese</key>
	
<string>地方</string>
</dict>
</plist>

代码中使用

Dictionary* dic = Dictionary::createWithContentsOfFile("chineseString.xml");
String* strchinese   =   (String*)dic->objectForKey("startGame");

LabelBMFont* bmFont = LabelBMFont::create(strchinese->getCString(),"font_1.fnt");
addChild(bmFont,0,1000);
bmFont->setPosition(Point(200,500));
bmFont->setWidth(20);


方法二 使用类库

首先创建一类库  名为G2UTool

#ifndef __TOOLS_H__  
#define __TOOLS_H__  
  
#include <string>  
#include "iconv/iconv.h"
using namespace std;


class G2UTool
{
public:

	static int GBKToUTF8(std::string &gbkStr);  
};
#endif  

#include "G2UTool.h"


int G2UTool::GBKToUTF8(std::string &gbkStr)  
{  
	iconv_t iconvH;  

	iconvH				= iconv_open("utf-8","gb2312");  
	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;  
} 

然后再cocos2dx中调用

std::string str2 = "开始游戏";
	G2UTool::GBKToUTF8(str2);

	LabelBMFont* bmFont = LabelBMFont::create(str2.c_str(),"font_1.fnt");
	addChild(bmFont,0,1000);
	bmFont->setPosition(Point(200,500));
	bmFont->setWidth(20);




  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值