cocos2d-x的三种label:CCLabelTTF,CCLabelAtlas,CClabelBMFont

cocos2d-x中关于字体的类主要有三种
   CCLabelAtlas
   CCLabelBMFont
   CCLabelTTF
  
   简单介绍
   CCLabelAtlas

   继承关系图


CCLabelAtlas is a subclass of CCAtlasNode.

It can be as a replacement of CCLabel since it is MUCH faster.

CCLabelAtlas versus CCLabel:

  • CCLabelAtlas is MUCH faster than CCLabel
  • CCLabelAtlas "characters" have a fixed height and width
  • CCLabelAtlas "characters" can be anything you want since they are taken from an image file

A more flexible class is CCLabelBMFont. It supports variable width characters and it also has a nice editor.


上面说明了以下几点:
1、CCLabelAtlas 比CCLabel(CCLabelTTF)快很多
2、CCLabelAtlas 中的每个字符必须有固定的高度和宽度
3、CCLabelAtlas 可以做你想要的,前提是你提供给他一张你想操作的字体的图片
4、CCLabelBMFont 比CCLabelAtlas 更灵活,他支持的字体宽可以参数化
5、增加一点,要用CCLabelAtlas创建字体你必须给出图片到字体的映射方式,也就是设置里面startCharMap的参数值,每种字体的映射方式不一样
  
CCLabelBMFont
继承关系图


CCLabelBMFont is a subclass of CCSpriteBatchNode.

Features:

  • Treats each character like a CCSprite. This means that each individual character can be:
  • rotated
  • scaled
  • translated
  • tinted
  • change the opacity
  • It can be used as part of a menu item.
  • anchorPoint can be used to align the "label"
  • Supports AngelCode text format

Limitations:

  • All inner characters are using an anchorPoint of (0.5f, 0.5f) and it is not recommend to change it because it might affect the rendering

CCLabelBMFont implements the protocol CCLabelProtocol, like CCLabel and CCLabelAtlasCCLabelBMFont has the flexibility of CCLabel, the speed ofCCLabelAtlas and all the features of CCSprite. If in doubt, use CCLabelBMFont instead of CCLabelAtlas / CCLabel.

继续说明
1、 首先CCLabelBMFont是CCSpriteBatchNode的一个子类(这个很强大)
2、 他能够把每一个字符当做一个CCSprite,这意味着每一个单独的字符都可以有自己的动作(什么旋转,放大,改变透明度)
3、 所有内部字符的的定位点(anchorpoint)为(0.5,0.5),也就是说每一个字符相对于坐标点的位置为该字符的中心点(而不是左上角,或则左下角),这里说了建议不要去修改他,因为这种改变可能会影响绘制
4、 CCLabelBMFont不仅具有CCLabel(CCLabelTTF)的灵活性,而且有CCLabelAtlas的速度和所有CCSprite的功能,如果你在考虑用CCLabelBMFont还是CCLabelAtlas时,那么就用CCLabelBMFont吧




CCLabelTTF
继承关系图



CCLabelTTF is a subclass of CCTextureNode that knows how to render text labels.

All features from CCTextureNode are valid in CCLabelTTF

CCLabelTTF objects are slow. Consider using CCLabelAtlas or CCLabelBMFont instead.

说明
CCLabelTTF很慢,考虑用CCLabelAtlas和CCLabelBMFont来替代


字体的创建方式
和cocos2d-x中大多数类型一样,字体也实现了静态工厂的模式(通过静态方法创建,采用CCPoolManager管理内存),当然你也可以采用非静态方法来创建
CCLabelAtlas:

CCLabelAtlas* CCLabelAtlas::create(const char *string, const char *charMapFile, unsigned int itemWidth, int unsigned itemHeight, unsigned int startCharMap)
{
    CCLabelAtlas *pRet = new CCLabelAtlas();
    if(pRet && pRet->initWithString(string, charMapFile, itemWidth, itemHeight, startCharMap))
    {
        pRet->autorelease();
        return pRet;
    }
    CC_SAFE_DELETE(pRet);
    return NULL;
}

上面已经说了CCLabelAtlas创建的字体相对于你给的字体图片来说必须有固定的宽度和高度,你还应该给出固定的映射方式(否则创建的字体达不到你的要的效果)


setString函数:
我们可以通过初始化的方式设置label显示的字符串,如果你想改变label显示的字符串,那么你就需要用到setString函数
   CCLabelAtlas:

//CCLabelAtlas - CCLabelProtocol
void CCLabelAtlas::setString(const char *label)
{
    unsigned int len = strlen(label);
    if (len > m_pTextureAtlas->getTotalQuads())
    {
        m_pTextureAtlas->resizeCapacity(len);
    }
    m_sString.clear();
    m_sString = label;
    this->updateAtlasValues();

    CCSize s = CCSizeMake(len * m_uItemWidth, m_uItemHeight);

    this->setContentSize(s);

    m_uQuadsToDraw = len;
}

CCLabelBMFont:

//LabelBMFont - CCLabelProtocol protocol
void CCLabelBMFont::setString(const char *newString)
{
    this->setString(newString, false);
}

void CCLabelBMFont::setString(const char *newString, bool fromUpdate)
{
    CC_SAFE_DELETE_ARRAY(m_sString);
    m_sString = cc_utf16_from_utf8(newString);
    m_sInitialString = newString;

    updateString(fromUpdate);
}

void CCLabelBMFont::updateString(bool fromUpdate)
{
    if (m_pChildren && m_pChildren->count() != 0)
    {
        CCObject* child;
        CCARRAY_FOREACH(m_pChildren, child)
        {
            CCNode* pNode = (CCNode*) child;
            if (pNode)
            {
                pNode->setVisible(false);
            }
        }
    }
    this->createFontChars();

    if (!fromUpdate)
        updateLabel();
}

CCLabelTTF:

void CCLabelTTF::setString(const char *string)
{
    CCAssert(string != NULL, "Invalid string");
    
    if (m_string.compare(string))
    {
        m_string = string;
        
        this->updateTexture();
    }
}
// Helper
void CCLabelTTF::updateTexture()
{
    CCTexture2D *tex;
    
    // let system compute label's width or height when its value is 0
    // refer to cocos2d-x issue #1430
    tex = new CCTexture2D();
    tex->initWithString(m_string.c_str(),
                        CC_SIZE_POINTS_TO_PIXELS(m_tDimensions), 
                        m_hAlignment,
                        m_vAlignment,
                        m_pFontName->c_str(),
                        m_fFontSize * CC_CONTENT_SCALE_FACTOR());
	
    this->setTexture(tex);
    tex->release();
	
	CCRect rect = CCRectZero;
    rect.size = m_pobTexture->getContentSize();
    this->setTextureRect(rect);
}

 通过上面的源代码我们能大概描述出其创建的方式
 CCLabelAtlas:
 1、将字符串清空
 2、将设置的字符串赋值给先前清空的字符串


 CCLabelBMFont:
 1、将字符串清空
 2、将设置的字符串赋值给先前清空的字符串
 3、将每个节点设置为隐藏(CCLabelBMFont 中对待每个字符是以CCNode的方式,因为他是CCSpriteBathNode的派生类)
 4、显示新的字符(我们可以猜测这里的createFontChars的功能就是将新的字符赋值到每个节点中,后将节点显示出来)


 CCLabelTTF
 1、将字符串析构掉
 2、重新new一个字符串
 3、创建新的texture
 4、将texture设置进去


 从上面的setString函数调用开销可以看出一般我们在需要频繁改变显示的字符串的时候我们最好是采用CCLabelAtlas,比如说显示时钟的时候,其次是CCLabelBMFont,最后是CCLabelTTF
 
 具体的创建和使用上面三个类,我们可以通过Cocos2d-x中FontTest来进行测试


 最后需要说明的是上面的这些类全都继承自CCNode,CCNode有个接口函数runAction,也就是说我们可以将cocos2d-x中的所有动作和这些label结合起来显示,而CCLabelBMFont是以CCNode的方式对待每个字符,那么每个字符都可以创建出动作



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值