天天看点

cocos2d-x显示中文,方法(2)

       在上一篇《cocos2d-x显示中文,方法(1)》中,我们使用官方TestCpp里面的方法,通过xml文件实现了显示中文。那么还有其他的方法吗?当然有了,那就是使用第三方类库。

      coco2d-x自带了libiconv.lib类库来支持中文,那么我们就来学习一下怎么使用它,这里参考了老G前辈的方法。

      新建一个项目,命名为”LabelTest2“。还记得在上一篇中,我们是按照子龙山人前辈的方法配置的环境,其中的《cocos2d-x建工程时避免copy文件夹和库》想必大家都已经看过了,我们会在VC++目录的包含目录和库目录下添加一些文件夹,那么我们就要在这里把iconv.h头文件所在的目录也包含进去

cocos2d-x显示中文,方法(2)

然后我们在编辑框内添加上一条E:\cocos2d-xSources\cocos2dx\platform\third_party\win32\iconv                    

cocos2d-x显示中文,方法(2)

如若不添加进去,则会出现以下错误提示:

cocos2d-x显示中文,方法(2)

接下来就要写代码了,我们新建tools.h和tool.cpp两个文件,两个文件的内容如下: tools.h文件

#ifndef __TOOLS_H__
#define __TOOLS_H__

#include "cocos2d.h"
#include "iconv.h"

int GBKToUTF8(std::string &gbkStr,const char* toCode,const char* formCode);

#endif
           

tools.cpp文件

#include "tools.h"

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;
}
           

然后我们就要在HelloWorld.cpp文件中使用这个方法了,还是和上一篇一样,我们简单的显示几个汉字

bool HelloWorld::init()
{
    bool bRet = false;
    do 
    {
        //
        // super init first
        //

        CC_BREAK_IF(! CCLayer::init());

        //
        // add your codes below...
        //

        // 1. Add a menu item with "X" image, which is clicked to quit the program.

        // Create a "close" menu item with close icon, it's an auto release object.
        CCMenuItemImage *pCloseItem = CCMenuItemImage::create(
            "CloseNormal.png",
            "CloseSelected.png",
            this,
            menu_selector(HelloWorld::menuCloseCallback));
        CC_BREAK_IF(! pCloseItem);

        // Place the menu item bottom-right conner.
        pCloseItem->setPosition(ccp(CCDirector::sharedDirector()->getWinSize().width - 20, 20));

        // Create a menu with the "close" menu item, it's an auto release object.
        CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);
        pMenu->setPosition(CCPointZero);
        CC_BREAK_IF(! pMenu);

        // Add the menu to HelloWorld layer as a child layer.
        this->addChild(pMenu, 1);
      
        CCSize size = CCDirector::sharedDirector()->getWinSize();

	std::string hello="你好";
	GBKToUTF8(hello,"gb2312","utf-8");

	CCLabelTTF* label=CCLabelTTF::create(hello.c_str(),"Arial",20);
	label->setPosition(ccp(size.width/2,size.height*0.5f));

        this->addChild(label,2);
    
        // 3. Add add a splash screen, show the cocos2d splash image.
        CCSprite* pSprite = CCSprite::create("HelloWorld.png");
        CC_BREAK_IF(! pSprite);

        // Place the sprite on the center of the screen
        pSprite->setPosition(ccp(size.width/2, size.height/2));

        // Add the sprite to HelloWorld layer as a child layer.
        this->addChild(pSprite, 0);

        bRet = true;
    } while (0);

    return bRet;
}
           

运行,这时会报错,如下面所示,(不报的就不用管了)。

cocos2d-x显示中文,方法(2)

这是因为我们还得设置一个地方。右击项目-->属性-->链接器-->附加依赖项,编辑,添加进一条“libiconv.lib”即可。                          

cocos2d-x显示中文,方法(2)

好了,我们再次运行,会看到界面中间显示“你好”,(不大清楚,请仔细观察)。                       

cocos2d-x显示中文,方法(2)

最后,感谢子龙山人和老G前辈做出的无私奉献,祝愿大家新的一年工作顺利,身体健康,阖家欢乐!    

继续阅读