Cocos2d-x   发布时间:2022-05-03  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了cocos2d-x freetype2大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

cocos2d-x学习日志(16) --富文本

http://blog.csdn.net/rexuefengye/article/details/21120705

最近做聊天系统,遇到棘手的问题,就是字体要支 持多颜色、换行、表情(图片)、超链接 !我们不会从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_675_227@//aSELEctorcallBACk
  16. void@H_827_119@menuCloseCallBACk(CCObject*pSender);
  17. @H_675_227@//implementthe"staticnode()"methodmanually
  18. CREATE_FUNC(HelloWorld);
  19. boolccTouchBegan(CCTouch*pTouch,CCEvent*pEvent);
  20. voidccTouchMoved(CCTouch*pTouch,CCEvent*pEvent);
  21. @H_675_227@//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

    staticCCHTMLLabel*s_htmlLabel=NULL;
  1. std::stringtt;
  2. @H_675_227@//on"init"youneedtoinitializeyourinstance
  3. boolHelloWorld::init()
  4. @H_675_227@////////////////////////////// @H_675_227@//1.superinitfirst
  5. if(!CCLayer::init())
  6. returnfalse;
  7. }
  8. CCSizevisibleSize=CCDirector::sharedDirector()->getVisibleSize();
  9. CCPointorigin=CCDirector::sharedDirector()->getVisibLeorigin();
  10. @H_675_227@///////////////////////////// @H_675_227@//2.addamenuitemwith"X"image,whichisclickedtoquittheprogram
  11. //youmaymodifyit.
  12. @H_675_227@//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"> @H_279_116@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_675_227@//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)"> 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(:rgb(0,&HelloWorld::onHTMLClicked,&HelloWorld::onHTMLMoved);
  46. FontFactory::instance()->dump_textures();
  47. true;
  48. }
  49. voidHelloWorld::onHTMLClicked(
  50. IRichNode*root,87); font-weight:bold; BACkground-color:inherit">int_id)
  51. {
  52. CCLog("[OnClicked]id=%d",_id);
  53. if(!s_htmlLabel)
  54. return;
  55. elseif(_id==1002)//close
  56. s_htmlLabel->setVisible(false);
  57. if(_id==2000)//reload
  58. s_htmlLabel->setString(str_utf8->getCString());
  59. voidHelloWorld::onHTMLMoved(
  60. constCCPoint&delta)
  61. CCLog("[OnMoved]id=%d",_id);
  62. if(!s_htmlLabel)
  63. return;
  64. if(_id==1001)
  65. s_htmlLabel->setPosition(ccpAdd(delta,s_htmlLabel->getPosition()));
  66. boolHelloWorld::ccTouchBegan(CCTouch*pTouch,CCEvent*pEvent)
  67. voidHelloWorld::ccTouchMoved(CCTouch*pTouch,CCEvent*pEvent)
  68. }

运行效果图:



点击超链接:



控制台显示:

大佬总结

以上是大佬教程为你收集整理的cocos2d-x freetype2全部内容,希望文章能够帮你解决cocos2d-x freetype2所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
猜你在找的Cocos2d-x相关文章