zoukankan      html  css  js  c++  java
  • cocos2dx阴影层的实现

    效果图

    //ShadowLayer.h

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

    //ShadowLayer.h

    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);
    }
  • 相关阅读:
    POJ3159 Candies —— 差分约束 spfa
    POJ1511 Invitation Cards —— 最短路spfa
    POJ1860 Currency Exchange —— spfa求正环
    POJ3259 Wormholes —— spfa求负环
    POJ3660 Cow Contest —— Floyd 传递闭包
    POJ3268 Silver Cow Party —— 最短路
    POJ1797 Heavy Transportation —— 最短路变形
    POJ2253 Frogger —— 最短路变形
    POJ1759 Garland —— 二分
    POJ3685 Matrix —— 二分
  • 原文地址:https://www.cnblogs.com/mrblue/p/3533938.html
Copyright © 2011-2022 走看看