http://blog.csdn.net/rexuefengye/article/details/21120705
1.下载准备:
freetype2:http://download.savannah.gnu.org/releases/freetype/
扩展库:https://github.com/happykevins/cocos2dx-ext
2.搭建环境
2.1 配置freetype2
工程根目录:
class文件夹:
vs2010工程目录:
3. 代码:
HelloWorldScene.h
- #ifndef__HELLOWORLD_SCENE_H__
- #define__HELLOWORLD_SCENE_H__
- #include"cocos2d.h"
- #include"cocos-ext.h"
- #include<renren-ext.h>
- USING_NS_CC;
- USING_NS_CC_EXT;
- classHelloWorld:publiccocos2d::CCLayer
- {
- public:
- //Here'sadifference.Method'init'incocos2d-xreturnsbool,insteadofreturning'id'incocos2d-iphone
- virtualboolinit();
- //there'sno'id'incpp,sowerecommendreturningtheclassinstancepointer
- staticcocos2d::CCScene*scene();
- //aselectorcallback
- voidmenuCloseCallback(CCObject*pSender);
- //implementthe"staticnode()"methodmanually
- CREATE_FUNC(HelloWorld);
- boolccTouchBegan(CCTouch*pTouch,CCEvent*pEvent);
- voidccTouchMoved(CCTouch*pTouch,CCEvent*pEvent);
- //HTMLevents
- voidonHTMLClicked(
- IRichNode*root,IRichElement*ele,int_id);
- voidonHTMLMoved(
- int_id,
- constCCPoint&location,constCCPoint&delta);
- };
- #endif//__HELLOWORLD_SCENE_H__
- staticCCHTMLLabel*s_htmlLabel=NULL;
- std::stringtt;
- //on"init"youneedtoinitializeyourinstance
- boolHelloWorld::init()
- //////////////////////////////
- //1.superinitfirst
- if(!CCLayer::init())
- returnfalse;
- }
- CCSizevisibleSize=CCDirector::sharedDirector()->getVisibleSize();
- CCPointorigin=CCDirector::sharedDirector()->getVisibleOrigin();
- /////////////////////////////
- //2.addamenuitemwith"X"image,whichisclickedtoquittheprogram
- //youmaymodifyit.
- //adda"close"icontoexittheprogress.it'sanautoreleaSEObject
- CCMenuItemImage*pCloseItem=CCMenuItemImage::create(
- "CloseNormal.png",108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> "CloseSelected.png",
- this,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> menu_selector(HelloWorld::menuCloseCallback));
- pCloseItem->setPosition(ccp(origin.x+visibleSize.width-pCloseItem->getContentSize().width/2,
- origin.y+pCloseItem->getContentSize().height/2));
- //createmenu,it'sanautoreleaSEObject
- CCMenu*pMenu=CCMenu::create(pCloseItem,NULL);
- pMenu->setPosition(CCPointZero);
- this->addChild(pMenu,1);
- usingnamespacedfont;
- CCLayerColor*l=CCLayerColor::create(ccc4(0xb0,0xb0,0xff));
- l->setContentSize(this->getContentSize());
- this->addChild(l);
- //控件文字样式(尺寸、对齐方式、字体等等)
- //font1
- FontCatalog*font_catalog=NULL;
- font_catalog=FontFactory::instance()->create_font(
- "font1","simhei.ttf",0xffffffff,32,e_plain,0.0f,0);
- //font2
- font_catalog=FontFactory::instance()->create_font(
- "font2","simkai.ttf",24,e_shadow,1.0f,0xff000000,0);
- font_catalog->add_hackfont("htmltest/MarkerFelt.ttf",latin_charset(),-1);
- //font3
- "font3","simli.ttf",20,e_border,108); list-style:decimal-leading-zero outside; color:inherit; line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> font_catalog->add_hackfont("simhei.ttf",5);
- CCSizevsize=CCDirector::sharedDirector()->getVisibleSize();
- CCString*str_utf8=CCString::createWithContentsOfFile("html.htm");
- CCHTMLLabel*htmllabel=CCHTMLLabel::createWithString(str_utf8->getCString(),248)"> CCSize(vsize.width*0.8f,vsize.height),"default");
- htmllabel->setAnchorPoint(ccp(0.5f,0.5f));
- htmllabel->setPosition(ccp(vsize.width*0.5f,vsize.height*0.5f));
- addChild(htmllabel);
- s_htmlLabel=htmllabel;
- //创建超链接
- htmllabel->registerListener( FontFactory::instance()->dump_textures();
- true;
- }
- voidHelloWorld::onHTMLClicked(
- IRichNode*root,87); font-weight:bold; background-color:inherit">int_id)
- {
- CCLog("[OnClicked]id=%d",_id);
- if(!s_htmlLabel)
- return;
- elseif(_id==1002)//close
- s_htmlLabel->setVisible(false);
- if(_id==2000)//reload
- s_htmlLabel->setString(str_utf8->getCString());
- voidHelloWorld::onHTMLMoved(
- constCCPoint&delta)
- CCLog("[OnMoved]id=%d",_id);
- if(!s_htmlLabel)
- return;
- if(_id==1001)
- s_htmlLabel->setPosition(ccpAdd(delta,s_htmlLabel->getPosition()));
- boolHelloWorld::ccTouchBegan(CCTouch*pTouch,CCEvent*pEvent)
- voidHelloWorld::ccTouchMoved(CCTouch*pTouch,CCEvent*pEvent)
- }