cocos2d-x 中文显示

前端之家收集整理的这篇文章主要介绍了cocos2d-x 中文显示前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
#include "ChineseDictionary.h"
#include "iconv/iconv.h"
#pragma comment(lib,"libiconv.lib")

CCScene * ChineseDictionary::scene()
{
	CCScene * s = CCScene::create();
	CCLayer * layer = ChineseDictionary::create();
	s->addChild(layer);
	return s;
}
int ChineseDictionary::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,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;
}
bool ChineseDictionary::init()
{
	bool bRet = false;
	do 
	{
		CC_BREAK_IF(!CCLayer::init());
		addChineseTTF();
		bRet = true;
	} while (0);
	return bRet;
}
void ChineseDictionary::addChineseTTF()
{
	std::string keyStr = "啊啊啊";
	GBKToUTF8(keyStr);
	CCSize winSize = CCDirector::sharedDirector()->getWinSize();
	CCLabelTTF * chTTF = CCLabelTTF::create();
	chTTF->setString(keyStr.c_str());
	chTTF->setFontName("Arial");
	chTTF->setFontSize(50);
	addChild(chTTF);
	chTTF->setPosition(ccp(winSize.width/2,winSize.height/2));
}

猜你在找的Cocos2d-x相关文章