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

运行效果图:



点击超链接



控制台显示

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