cocos2d-x 文字特效freetype2

前端之家收集整理的这篇文章主要介绍了cocos2d-x 文字特效freetype2前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

最近做聊天系统,遇到棘手的问题,就是字体要支持多颜色、换行、表情(图片)、超链接!我们不会从OpenGL底层来做这个工作,因为那样工作量非常大,不现实,考虑在已有的cocos2d-x接口上进行处理,来组合出我们需要富文本。因Android IOS 似乎都支持freetype2,所以就优先考虑了。


1.下载准备:


freetype2:http://download.savannah.gnu.org/releases/freetype/

扩展库:https://github.com/happykevins/cocos2dx-ext


2.搭建环境


2.1 配置freetype2


2.2.创建工程,添加文件,如下:

工程根目录:




class文件夹:




vs2010工程目录:



3. 代码


HelloWorldScene.h

  1. #ifndef__HELLOWORLD_SCENE_H__
  2. #define__HELLOWORLD_SCENE_H__
  3. #include"cocos2d.h"
  4. #include"cocos-ext.h"
  5. #include<renren-ext.h>
  6. USING_NS_CC;
  7. USING_NS_CC_EXT;
  8. classHelloWorld:publiccocos2d::CCLayer
  9. {
  10. public:
  11. //Here'sadifference.Method'init'incocos2d-xreturnsbool,insteadofreturning'id'incocos2d-iphone
  12. virtualboolinit();
  13. //there'sno'id'incpp,sowerecommendreturningtheclassinstancepointer
  14. staticcocos2d::CCScene*scene();
  15. //aselectorcallback
  16. voidmenuCloseCallback(CCObject*pSender);
  17. //implementthe"staticnode()"methodmanually
  18. CREATE_FUNC(HelloWorld);
  19. boolccTouchBegan(CCTouch*pTouch,CCEvent*pEvent);
  20. voidccTouchMoved(CCTouch*pTouch,CCEvent*pEvent);
  21. //HTMLevents
  22. voidonHTMLClicked(
  23. IRichNode*root,IRichElement*ele,int_id);
  24. voidonHTMLMoved(
  25. int_id,
  26. constCCPoint&location,constCCPoint&delta);
  27. };
  28. #endif//__HELLOWORLD_SCENE_H__
HelloWorldScene.cpp

@H_916_301@
    staticCCHTMLLabel*s_htmlLabel=NULL;
  1. std::stringtt;
  2. //on"init"youneedtoinitializeyourinstance
  3. boolHelloWorld::init()
  4. //////////////////////////////
  5. //1.superinitfirst
  6. if(!CCLayer::init())
  7. returnfalse;
  8. }
  9. CCSizevisibleSize=CCDirector::sharedDirector()->getVisibleSize();
  10. CCPointorigin=CCDirector::sharedDirector()->getVisibleOrigin();
  11. /////////////////////////////
  12. //2.addamenuitemwith"X"image,whichisclickedtoquittheprogram
  13. //youmaymodifyit.
  14. //adda"close"icontoexittheprogress.it'sanautoreleaSEObject
  15. CCMenuItemImage*pCloseItem=CCMenuItemImage::create(
  16. "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",
  17. 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));
  18. pCloseItem->setPosition(ccp(origin.x+visibleSize.width-pCloseItem->getContentSize().width/2,
  19. origin.y+pCloseItem->getContentSize().height/2));
  20. //createmenu,it'sanautoreleaSEObject
  21. CCMenu*pMenu=CCMenu::create(pCloseItem,NULL);
  22. pMenu->setPosition(CCPointZero);
  23. this->addChild(pMenu,1);
  24. usingnamespacedfont;
  25. CCLayerColor*l=CCLayerColor::create(ccc4(0xb0,0xb0,0xff));
  26. l->setContentSize(this->getContentSize());
  27. this->addChild(l);
  28. //控件文字样式(尺寸、对齐方式、字体等等)
  29. //font1
  30. FontCatalog*font_catalog=NULL;
  31. font_catalog=FontFactory::instance()->create_font(
  32. "font1","simhei.ttf",0xffffffff,32,e_plain,0.0f,0);
  33. //font2
  34. font_catalog=FontFactory::instance()->create_font(
  35. "font2","simkai.ttf",24,e_shadow,1.0f,0xff000000,0);
  36. font_catalog->add_hackfont("htmltest/MarkerFelt.ttf",latin_charset(),-1);
  37. //font3
  38. "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);
  39. CCSizevsize=CCDirector::sharedDirector()->getVisibleSize();
  40. CCString*str_utf8=CCString::createWithContentsOfFile("html.htm");
  41. CCHTMLLabel*htmllabel=CCHTMLLabel::createWithString(str_utf8->getCString(),248); line-height:18px; margin:0px!important; padding:0px 3px 0px 10px!important"> CCSize(vsize.width*0.8f,vsize.height),"default");
  42. htmllabel->setAnchorPoint(ccp(0.5f,0.5f));
  43. htmllabel->setPosition(ccp(vsize.width*0.5f,vsize.height*0.5f));
  44. addChild(htmllabel);
  45. s_htmlLabel=htmllabel;
  46. //创建超链接
  47. htmllabel->registerListener( FontFactory::instance()->dump_textures();
  48. true;
  49. }
  50. voidHelloWorld::onHTMLClicked(
  51. IRichNode*root,87); background-color:inherit; font-weight:bold">int_id)
  52. {
  53. CCLog("[OnClicked]id=%d",_id);
  54. if(!s_htmlLabel)
  55. return;
  56. elseif(_id==1002)//close
  57. s_htmlLabel->setVisible(false);
  58. if(_id==2000)//reload
  59. s_htmlLabel->setString(str_utf8->getCString());
  60. voidHelloWorld::onHTMLMoved(
  61. constCCPoint&delta)
  62. CCLog("[OnMoved]id=%d",_id);
  63. if(!s_htmlLabel)
  64. return;
  65. if(_id==1001)
  66. s_htmlLabel->setPosition(ccpAdd(delta,s_htmlLabel->getPosition()));
  67. boolHelloWorld::ccTouchBegan(CCTouch*pTouch,CCEvent*pEvent)
  68. voidHelloWorld::ccTouchMoved(CCTouch*pTouch,CCEvent*pEvent)
  69. }

运行效果图:



点击超链接



控制台显示

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