1. 程式人生 > >cocos2dx中CCLabelTTF的描邊和陰影

cocos2dx中CCLabelTTF的描邊和陰影

遊戲中經常會用到文字描邊和陰影,當cocos2dx中並沒有給我們提供,下面是我參考:點選開啟連結(http://blog.csdn.net/song_hui_xiang/article/details/17375279)和點選開啟連結(http://fengmm521.blog.163.com/blog/static/2509135820138943638777/)自己弄了一個,基本上是一模一樣,不喜勿噴!程式碼如下:

///////////////////////////////////////////////////////////////////////
// 名稱空間:窄描邊 ///////////////////////////////////////////////////////////////////////

思路:多個CCLabelTTF重疊在一起。

標頭檔案

//
//  CLabel.h
//  XXX
//
//  Created by user on 14-3-4.
//
//

#ifndef __XXX__CLabel__
#define __XXX__CLabel__

#include <iostream>
#include "cocos2d.h"

USING_NS_CC;

namespace CLabel{
    /*使用示例
     CCSize size = CCDirector::sharedDirector()->getWinSize();
     
     //建立個背景
     CCLayerColor* whiteLayer = CCLayerColor::create(ccc4(0, 205, 205, 255), size.width, size.height);
     this->addChild(whiteLayer);
     
     //建立描邊
     CCLabelTTF* outline = CLabel::textAddOutline("描邊 Outline", "Arial", 40, ccWHITE, 1);
     outline->setPosition(ccp(size.width*0.5, size.height*0.7));
     this->addChild(outline);
     
     //建立陰影
     CCLabelTTF* shadow = CLabel::textAddShadow("陰影 Shadow", "Arial", 40, ccWHITE, 2, 200);
     shadow->setPosition(ccp(size.width*0.5, size.height*0.5));
     this->addChild(shadow);
     
     //建立描邊加陰影
     CCLabelTTF* outlineShadow = CLabel::textAddOutlineAndShadow("描邊加陰影 OutlineShadow", "Arial", 40, ccWHITE, 1, 4, 200);
     outlineShadow->setPosition(ccp(size.width*0.5, size.height*0.3));
     this->addChild(outlineShadow);
     */
    
    //**************************************************************************
    // 給文字新增描邊
    //**************************************************************************
    CCLabelTTF* textAddOutline(const char* string,          // 顯示的文字
                               const char* fontName,        // 字型名稱
                               float fontSize,              // 字型大小
                               const ccColor3B &color3,     // 字型顏色
                               float lineWidth);            // 描邊寬度
    
    //**************************************************************************
    // 新增陰影
    CCLabelTTF* textAddShadow(const char* string,           // 顯示的文字
                              const char* fontName,         // 字型名稱
                              float fontSize,               // 字型大小
                              const ccColor3B &color3,      // 字型顏色
                              float shadowSize,             // 陰影寬度
                              float shadowOpacity);         // 陰影透明度
    //**************************************************************************
    
    //**************************************************************************
    // 既新增描邊又新增陰影
    //**************************************************************************
    CCLabelTTF* textAddOutlineAndShadow(const char* string,     // 顯示的文字
                                        const char* fontName,   // 字型名稱
                                        float fontSize,         // 字型大小
                                        const ccColor3B &color3,// 陰影顏色
                                        float lineWidth,        // 字型寬度
                                        float shadowSize,       // 陰影寬度
                                        float shadowOpacity);   // 陰影透明度
};

#endif /* defined(__XXX__CLabel__) */

// 原始檔
//
//  CLabel.cpp
//  XXX
//
//  Created by user on 14-3-4.
//
//

#include "CLabel.h"

namespace CLabel{
    /*
     製作文字描邊效果是很簡單的,我們寫好一段文字之後,也就是創建出一個CCLabelTTF,稱之為正文CCLabelTTF。然後再創建出4個CCLabelTTF,顏色為黑色,大小同正文CCLabelTTF相同,
     稱之為描邊CCLabelTTF。說到這大家可能已經明白了,沒錯,就是把4個描邊CCLabelTTF放於正文CCLabelTTF的下面,分別於左右上下與正文CCLabelTTF錯開,這樣描邊效果就實現啦。。
     
     *string     文字
     *fontName   文字字型型別
     *fontSize   文字大小
     *color3     文字顏色
     *lineWidth  所描邊的寬度
     */
    CCLabelTTF* textAddOutline(const char* string, const char* fontName, float fontSize,const ccColor3B &color3,float lineWidth)
    {
        //描邊CCLabelTTF 左
        CCLabelTTF* left = CCLabelTTF::create(string, fontName, fontSize);
        left->setColor(ccBLACK);
        
        //描邊CCLabelTTF 右
        CCLabelTTF* right = CCLabelTTF::create(string, fontName, fontSize);
        right->setColor(ccBLACK);
        right->setPosition(ccp(left->getContentSize().width*0.5+lineWidth*2,left->getContentSize().height*0.5));
        left->addChild(right);
        
        //描邊CCLabelTTF 上
        CCLabelTTF* up = CCLabelTTF::create(string, fontName, fontSize);
        up->setColor(ccBLACK);
        up->setPosition(ccp(left->getContentSize().width*0.5+lineWidth, left->getContentSize().height*0.5+lineWidth));
        left->addChild(up);
        
        //描邊CCLabelTTF 下
        CCLabelTTF* down = CCLabelTTF::create(string, fontName, fontSize);
        down->setColor(ccBLACK);
        down->setPosition(ccp(left->getContentSize().width*0.5+lineWidth, left->getContentSize().height*0.5-lineWidth));
        left->addChild(down);
        
        //正文CCLabelTTF
        CCLabelTTF* center = CCLabelTTF::create(string, fontName, fontSize);
        center->setColor(color3);
        center->setPosition(ccp(left->getContentSize().width*0.5+lineWidth, left->getContentSize().height*0.5));
        left->addChild(center);
        
        return left;
    }


    /*
     給文字新增陰影,一看就懂的。。。
     *string         文字
     *fontName       文字字型型別
     *fontSize       文字大小
     *color3         文字顏色
     *shadowSize     陰影大小
     *shadowOpacity  陰影透明度
     
     */
    CCLabelTTF* textAddShadow(const char* string, const char* fontName, float fontSize,const ccColor3B &color3,float shadowSize,float shadowOpacity)
    {
        CCLabelTTF* shadow = CCLabelTTF::create(string, fontName, fontSize);
        shadow->setColor(ccBLACK);
        shadow->setOpacity(shadowOpacity);
        
        CCLabelTTF* center = CCLabelTTF::create(string, fontName, fontSize);
        center->setColor(color3);
        center->setPosition(ccp(shadow->getContentSize().width*0.5-shadowSize, shadow->getContentSize().height*0.5+shadowSize));
        shadow->addChild(center);
        
        return shadow;
    }


    //既新增描邊又新增陰影
    CCLabelTTF* textAddOutlineAndShadow(const char* string, const char* fontName, float fontSize,const ccColor3B &color3,float lineWidth,float shadowSize,float shadowOpacity)
    {
        CCLabelTTF* shadow = CCLabelTTF::create(string, fontName, fontSize);
        shadow->setColor(ccBLACK);
        shadow->setOpacity(shadowOpacity);
        
        CCLabelTTF* left = CCLabelTTF::create(string, fontName, fontSize);
        left->setColor(ccBLACK);
        left->setPosition(ccp(shadow->getContentSize().width*0.5-shadowSize, shadow->getContentSize().height*0.5+shadowSize));
        shadow->addChild(left);
        
        CCLabelTTF* right = CCLabelTTF::create(string, fontName, fontSize);
        right->setColor(ccBLACK);
        right->setPosition(ccp(left->getContentSize().width*0.5+lineWidth*2,left->getContentSize().height*0.5));
        left->addChild(right);
        
        CCLabelTTF* up = CCLabelTTF::create(string, fontName, fontSize);
        up->setColor(ccBLACK);
        up->setPosition(ccp(left->getContentSize().width*0.5+lineWidth, left->getContentSize().height*0.5+lineWidth));
        left->addChild(up);
        
        CCLabelTTF* down = CCLabelTTF::create(string, fontName, fontSize);
        down->setColor(ccBLACK);
        down->setPosition(ccp(left->getContentSize().width*0.5+lineWidth, left->getContentSize().height*0.5-lineWidth));
        left->addChild(down);
        
        CCLabelTTF* center = CCLabelTTF::create(string, fontName, fontSize);
        center->setColor(color3);
        center->setPosition(ccp(left->getContentSize().width*0.5+lineWidth, left->getContentSize().height*0.5));
        left->addChild(center);
        
        return shadow;
    }
}

使用時,加上名稱空間就可以拉,下面是使用示例:
     CCSize size = CCDirector::sharedDirector()->getWinSize();
     
     //建立描邊
     CCLabelTTF* outline = CLabel::textAddOutline("描邊 Outline", "Arial", 40, ccWHITE, 1);
     outline->setPosition(ccp(size.width*0.5, size.height*0.7));
     this->addChild(outline);
     
     //建立陰影
     CCLabelTTF* shadow = CLabel::textAddShadow("陰影 Shadow", "Arial", 40, ccWHITE, 2, 200);
     shadow->setPosition(ccp(size.width*0.5, size.height*0.5));
     this->addChild(shadow);
     
     //建立描邊加陰影
     CCLabelTTF* outlineShadow = CLabel::textAddOutlineAndShadow("描邊加陰影 OutlineShadow", "Arial", 40, ccWHITE, 1, 4, 200);
     outlineShadow->setPosition(ccp(size.width*0.5, size.height*0.3));
     this->addChild(outlineShadow);

// 效果圖


///////////////////////////////////////////////////////////////////////
// 名稱空間:寬描邊 /////////////////////////////////////////////////////////////////////// //標頭檔案
class CCStrokeLabel : public CCNode {
public:
    bool init();
    static CCStrokeLabel* create(CCLabelTTF* labelTTF,ccColor3B fullColor,ccColor3B StrokeColor,float strokeSize);
    float getStrokeSize();
    void setStrokeSize(float strokeSize);
    
private:
    CCSprite* m_sprite;
    CCLabelTTF* m_label;
    ccColor3B m_fullColor;
    ccColor3B m_StrokeColor;
    float m_strokeSize;
};

//原始檔
CCStrokeLabel* CCStrokeLabel::create(CCLabelTTF* labelTTF,ccColor3B fullColor,ccColor3B StrokeColor,float strokeSize)
{
    CCStrokeLabel* tmp = new CCStrokeLabel;
    tmp->autorelease();
    tmp->m_label = labelTTF;
    tmp->m_fullColor = fullColor;
    tmp->m_StrokeColor = StrokeColor;
    tmp->m_strokeSize = strokeSize;
    tmp->init();
    
    return tmp;
}

bool CCStrokeLabel::init()
{
    float strokeSize = getStrokeSize();
    CCSize textureSize = m_label->getContentSize();
    textureSize.width += 2 * strokeSize;
    textureSize.height += 2 * strokeSize;
    
    glGetError();
    CCRenderTexture *rt = CCRenderTexture::create(
                                                  textureSize.width, textureSize.height);
    if(!rt)
    {
        CCLOG("CCStrokeLabel::init:rt == NULL");
        addChild(m_label);
        return false;
    }
    
    m_label->setColor(m_fullColor);
    
    ccBlendFunc originalBlend = m_label->getBlendFunc();
    ccBlendFunc func = { GL_SRC_ALPHA, GL_ONE};
    m_label->setBlendFunc(func);
    
    m_label->setAnchorPoint(ccp(0.5, 0.5));
    
    rt->begin();
    for(int i = 0; i < 360; i += 15)
    {
        float r = CC_DEGREES_TO_RADIANS(i);
        m_label->setPosition(ccp(
                                 textureSize.width * 0.5f + sin(r) * strokeSize,
                                 textureSize.height * 0.5f + cos(r) * strokeSize));
        m_label->visit();
    }
    m_label->setColor(m_StrokeColor);
    m_label->setBlendFunc(originalBlend);
    m_label->setPosition(ccp(textureSize.width * 0.5f, textureSize.height * 0.5f));
    m_label->visit();
    rt->end();
    
    CCTexture2D *texture = rt->getSprite()->getTexture();
    texture->setAliasTexParameters();
    m_sprite = CCSprite::createWithTexture(rt->getSprite()->getTexture());
    setContentSize(m_sprite->getContentSize());
    m_sprite->setAnchorPoint(ccp(0, 0));
    m_sprite->setPosition(ccp(0, 0));
    ((CCSprite *)m_sprite)->setFlipY(true);
    addChild(m_sprite);
    
    return true;
}

float CCStrokeLabel::getStrokeSize()
{
    return m_strokeSize;
}

void CCStrokeLabel::setStrokeSize(float strokeSize)
{
    m_strokeSize = strokeSize;
}

// 使用示例
    CCLabelTTF* ttf = CCLabelTTF::create("ttflable","Arial",50);
    CCStrokeLabel* ttfStroke = CCStrokeLabel::create(ttf,ccc3(255,0,0),ccc3(0,0,255),2.0f);
    ttfStroke->setPosition(ccp(250,250));
    this->addChild(ttfStroke,2);
    
    CCLabelTTF* ttfx = CCLabelTTF::create("ttf label test white","Arial",50);
    CCStrokeLabel* ttfStrokeWhite = CCStrokeLabel::create(ttfx,ccc3(0,255,0),ccc3(255,255,255),4.0f);
    ttfStrokeWhite->setPosition(ccp(255,500));
    this->addChild(ttfStrokeWhite,2);

// 效果圖