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

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. #ifndef __HELLOWORLD_SCENE_H__  
  2. #define __HELLOWORLD_SCENE_H__  
  3.   
  4. #include "cocos2d.h"  
  5. #include "cocos-ext.h"  
  6. #include <renren-ext.h>  
  7. USING_NS_CC;  
  8. USING_NS_CC_EXT;  
  9.   
  10. class HelloWorld : public cocos2d::CCLayer  
  11. {  
  12. public:  
  13.     // Here's a difference. Method 'init' in cocos2d-x returns bool, instead of returning 'id' in cocos2d-iphone  
  14.     virtual bool init();    
  15.   
  16.     // there's no 'id' in cpp, so we recommend returning the class instance pointer  
  17.     static cocos2d::CCScene* scene();  
  18.       
  19.     // a selector callback  
  20.     void menuCloseCallback(CCObject* pSender);  
  21.       
  22.     // implement the "static node()" method manually  
  23.     CREATE_FUNC(HelloWorld);  
  24.   
  25.     bool ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent);  
  26.     void ccTouchMoved(CCTouch* pTouch, CCEvent* pEvent);  
  27.   
  28.     // HTML events  
  29.     void onHTMLClicked(  
  30.         IRichNode* root, IRichElement* ele, int _id);  
  31.     void onHTMLMoved(  
  32.         IRichNode* root, IRichElement* ele, int _id,  
  33.         const CCPoint& location, const CCPoint& delta);  
  34. };  
  35.   
  36. #endif // __HELLOWORLD_SCENE_H__  
HelloWorldScene.cpp

[cpp]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. static CCHTMLLabel* s_htmlLabel = NULL;  
  2. std::string tt;  
  3. // on "init" you need to initialize your instance  
  4. bool HelloWorld::init()  
  5. {  
  6.     //  
  7.     // 1. super init first  
  8.     if ( !CCLayer::init() )  
  9.     {  
  10.         return false;  
  11.     }  
  12.       
  13.     CCSize visibleSize = CCDirector::sharedDirector()->getVisibleSize();  
  14.     CCPoint origin = CCDirector::sharedDirector()->getVisibleOrigin();  
  15.   
  16.     /  
  17.     // 2. add a menu item with "X" image, which is clicked to quit the program  
  18.     //    you may modify it.  
  19.   
  20.     // add a "close" icon to exit the progress. it's an autorelease object  
  21.     CCMenuItemImage *pCloseItem = CCMenuItemImage::create(  
  22.                                         "CloseNormal.png",  
  23.                                         "CloseSelected.png",  
  24.                                         this,  
  25.                                         menu_selector(HelloWorld::menuCloseCallback));  
  26.       
  27.     pCloseItem->setPosition(ccp(origin.x + visibleSize.width - pCloseItem->getContentSize().width/2 ,  
  28.                                 origin.y + pCloseItem->getContentSize().height/2));  
  29.   
  30.     // create menu, it's an autorelease object  
  31.     CCMenu* pMenu = CCMenu::create(pCloseItem, NULL);  
  32.     pMenu->setPosition(CCPointZero);  
  33.     this->addChild(pMenu, 1);  
  34.   
  35.     using namespace dfont;  
  36.   
  37.     CCLayerColor* l = CCLayerColor::create(ccc4(0xb0, 0xb0, 0xb0, 0xff));  
  38.     l->setContentSize(this->getContentSize());  
  39.     this->addChild(l);  
  40.   
  41.     //控件文字样式(尺寸、对齐方式、字体等等)  
  42.     // font1  
  43.     FontCatalog* font_catalog = NULL;  
  44.     font_catalog = FontFactory::instance()->create_font(  
  45.         "font1""simhei.ttf", 0xffffffff, 32, e_plain, 0.0f, 0xffffffff, 0);  
  46.     // font2  
  47.     font_catalog = FontFactory::instance()->create_font(  
  48.         "font2""simkai.ttf", 0xffffffff, 24, e_shadow, 1.0f, 0xff000000, 0);  
  49.     font_catalog->add_hackfont("htmltest/Marker Felt.ttf", latin_charset(), -1);  
  50.     // font3  
  51.     font_catalog = FontFactory::instance()->create_font(  
  52.         "font3""simli.ttf", 0xffffffff, 20, e_border, 1.0f, 0xff000000, 0);  
  53.     font_catalog->add_hackfont("simhei.ttf", latin_charset(), 5);  
  54.   
  55.   
  56.     CCSize vsize = CCDirector::sharedDirector()->getVisibleSize();  
  57.     CCString* str_utf8 = CCString::createWithContentsOfFile("html.htm");  
  58.   
  59.     CCHTMLLabel* htmllabel = CCHTMLLabel::createWithString(str_utf8->getCString(),   
  60.         CCSize(vsize.width*0.8f, vsize.height), "default");  
  61.     htmllabel->setAnchorPoint(ccp(0.5f,0.5f));  
  62.     htmllabel->setPosition(ccp(vsize.width*0.5f, vsize.height*0.5f));  
  63.     addChild(htmllabel);  
  64.   
  65.     s_htmlLabel = htmllabel;  
  66.     //创建超链接  
  67.     htmllabel->registerListener(this, &HelloWorld::onHTMLClicked, &HelloWorld::onHTMLMoved );  
  68.   
  69.     FontFactory::instance()->dump_textures();  
  70.   
  71.     return true;  
  72. }  
  73.   
  74. void HelloWorld::onHTMLClicked(  
  75.     IRichNode* root, IRichElement* ele, int _id)  
  76. {  
  77.     CCLog("[On Clicked] id=%d", _id);  
  78.   
  79.     if ( !s_htmlLabel )  
  80.     {  
  81.         return;  
  82.     }  
  83.     else if ( _id == 1002 ) // close  
  84.     {  
  85.         s_htmlLabel->setVisible(false);  
  86.     }  
  87.     else if ( _id == 2000 ) //reload  
  88.     {  
  89.         CCString* str_utf8 = CCString::createWithContentsOfFile("html.htm");  
  90.         s_htmlLabel->setString(str_utf8->getCString());     
  91.     }  
  92. }  
  93.   
  94. void HelloWorld::onHTMLMoved(  
  95.     IRichNode* root, IRichElement* ele, int _id,  
  96.     const CCPoint& location, const CCPoint& delta)  
  97. {  
  98.     CCLog("[On Moved] id=%d", _id);  
  99.   
  100.     if ( !s_htmlLabel )  
  101.     {  
  102.         return;  
  103.     }  
  104.     else if ( _id == 1001 )  
  105.     {  
  106.         s_htmlLabel->setPosition(ccpAdd(delta, s_htmlLabel->getPosition()));  
  107.     }  
  108. }  
  109.   
  110.   
  111. bool HelloWorld::ccTouchBegan(CCTouch *pTouch, CCEvent *pEvent)  
  112. {  
  113.     return true;  
  114. }  
  115.   
  116. void HelloWorld::ccTouchMoved(CCTouch *pTouch, CCEvent *pEvent)  
  117. {  
  118.   
  119. }  

运行效果图:



      点击超链接:


                     


      控制台显示:

                       

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值