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.   
  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

  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. }  

運行效果圖:



      點擊超鏈接:


                     


      控制檯顯示:

                       

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章