cocos2dx阴影层的实现

效果图

cocos2dx阴影层的实现

 

//ShadowLayer.h

cocos2dx阴影层的实现
class ShadowLayer : public CCLayer
{
protected:
    ShadowLayer()
    :m_pRender(NULL)
    ,m_pShadow(NULL)
    {}
    ~ShadowLayer()
    {
        CC_SAFE_RELEASE(m_pRender);
        CC_SAFE_RELEASE(m_pShadow);
    }

public:
    static ShadowLayer* create(CCNode* pRender,const CCPoint& ptOffset=ccp(-5.0f,-5.0f),const ccColor4F& col = ccc4f(0,0,0,0.5f) );

    virtual bool init();
    virtual void update(float delta)override;
    virtual void setOffset(const CCPoint& ptOffset);
    virtual void setShadowColor(const ccColor4F& col);
    CC_SYNTHESIZE_RETAIN(CCNode*,m_pRender,Render);
    
private:
    CCRenderTexture*    m_pRT;
    CCRenderTexture*    m_pShadow;
};
cocos2dx阴影层的实现

 

//ShadowLayer.h

cocos2dx阴影层的实现
ShadowLayer* ShadowLayer::create(CCNode* pRender,const CCPoint& ptOffset/*t=ccp(-5.0f,-5.0f)*/,const ccColor4F& col/* = ccc4f(0,0,0,0.5f)*/ )
{
    ShadowLayer *pRet = new ShadowLayer();
    if (pRet && pRet->init())
    {
        pRet->autorelease();
        pRet->setRender(pRender);
        pRet->setShadowColor(col);
        pRet->setOffset(ptOffset);
        return pRet;
    }
    else
    {
        delete pRet;
        pRet = NULL;
        return NULL;
    }
}
bool ShadowLayer::init()
{
    if (CCLayer::init())
    {
        CCSize sz = getContentSize();
        m_pRT = CCRenderTexture::create(sz.width,sz.height);
        m_pRT->setPosition(sz.width/2,sz.height/2);
        
        addChild(m_pRT);

        m_pShadow = CCRenderTexture::create(sz.width,sz.height);
        m_pShadow->setPosition(ccp(sz.width/2,sz.height/2));

        ccBlendFunc tBlendFunc = {GL_DST_ALPHA, GL_NONE};
        //ccBlendFunc tBlendFunc = {GL_ONE, GL_NONE};
        m_pShadow->getSprite()->setBlendFunc(tBlendFunc);
        m_pShadow->retain();

        scheduleUpdate();
    }    

    return true;
}

void ShadowLayer::update(float delta)
{
    m_pRT->clear(0,0,0,0);

    m_pRT->begin();

    if (m_pRender)
    {
        m_pRender->visit();
    }
    m_pShadow->visit();
    
    m_pRT->end();
}

void ShadowLayer::setShadowColor(const ccColor4F& col)
{
    m_pShadow->beginWithClear(col.r,col.g,col.b,col.a);
    m_pShadow->end();
}

void ShadowLayer::setOffset( const CCPoint& ptOffset )
{
    setPosition(ptOffset);
}
cocos2dx阴影层的实现

cocos2dx阴影层的实现

上一篇:Redis学习


下一篇:Java多线程同步代码块