- 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);
- //创建显示文本
- CCLabelTTF * label=CCLabelTTF::create(FontToUTF8("这是“微软雅黑”字体类型的HelloWorld"),"微软雅黑",32);
- //获取屏幕大小
- CCSize size=CCDirector::sharedDirector()->getWinSize();
- //设置文本大小和颜色
- label->setPosition(ccp(size.width/2,size.height/2));
- label->setColor(ccGREEN);
- //添加进布景
- addChild(label,1);
- bRet = true;
- } while (0);
- return bRet;
- }
我们还要在HelloWorld.h文件里声明用到的字符编码转换方法FontToUTF8,添加如下代码:
- char* FontToUTF8(const char* font);
- char* HelloWorld::FontToUTF8(const char* font)
- {
- int len=MultiByteToWideChar(CP_ACP,0,font,-1,NULL,0);
- wchar_t* wstr=new wchar_t[len+1];
- memset(wstr,0,len+1);
- MultiByteToWideChar(CP_ACP,0,font,-1,wstr,len);
- len=WideCharToMultiByte(CP_UTF8,0,wstr,-1,NULL,0,NULL,NULL);
- char* str=new char[len+1];
- memset(str,0,len+1);
- WideCharToMultiByte(CP_UTF8,0,wstr,-1,str,len,NULL,NULL);
- if(wstr)delete[] wstr;
- return str;
- }