cocos2dx 2.x实现闪电效果(贴画版)

 

cocos2dx 2.x实现闪电效果(非画线版)

在网上搜索到一个直接用opengl画线实现的版本,但放在游戏中效果不太搭,要求用贴图的。我这个版本用的也是画线版的算法。

闪动的时候效果还可以,每段衔接的地方还是不太完美,各位有其他的建议的请联系我  blue-1986@hotmail.com

 

转载请注明出处 http://www.cnblogs.com/mrblue/p/4315091.html

 

效果图

//头文件

#ifndef __LIGINTNING_H__
#define __LIGINTNING_H__

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

class Lightning : public cocos2d::CCNode
{
public:
	Lightning();
	~Lightning();

public:
    static Lightning* create(const std::string& strTextureFile, float fLength);
	virtual bool init(const std::string& strTextureFile, float fLength);

protected:
	virtual void draw()/*override*/;
	virtual void update(float fDelta)/*override*/;

public:
	void setTexture(cocos2d::CCTexture2D *texture);
	void setLength(float fLength){ m_fLength = fLength; }

protected:
	void calculateVertex();
	void addLightningSegment(float x1, float y1, float x2, float y2, float displace);

protected:
	std::vector<cocos2d::CCPoint>	m_vecLightningTrackPoint;
	std::vector<float>				m_vecVertics;
	std::vector<float>				m_vecCoordinates;
	cocos2d::CCTexture2D*			m_pTexture;
	float							m_fLength;
};

#endif // __HELLOWORLD_SCENE_H__

  

//cpp

#include "Lightning.h"

USING_NS_CC;

using namespace std;


Lightning::Lightning()
    :m_pTexture(NULL)
{
    m_vecLightningTrackPoint.reserve(64);
    m_vecVertics.reserve(256);
    m_vecCoordinates.reserve(256);
}

Lightning::~Lightning()
{
    CC_SAFE_RELEASE(m_pTexture);
}

Lightning* Lightning::create( const std::string& strTextureFile, float fLength )
{
    Lightning * pRet = new Lightning();
    if (pRet && pRet->init(strTextureFile,fLength))
    {
        pRet->autorelease();
    }
    else
    {
        CC_SAFE_DELETE(pRet);
    }

    return pRet;
}

bool Lightning::init(const string& strFile, float fLength)
{
    CCNode::init();

    setShaderProgram(CCShaderCache::sharedShaderCache()->programForKey(kCCShader_PositionTexture));
    setLength(fLength);
    setTexture(CCTextureCache::sharedTextureCache()->addImage(strFile.c_str()));
    
    
    scheduleUpdate();
        
    return true;
}

void Lightning::draw()
{
    if(m_vecVertics.empty()||m_vecCoordinates.empty()) return;

    CC_NODE_DRAW_SETUP();
    ccGLEnableVertexAttribs( kCCVertexAttribFlag_Position | kCCVertexAttribFlag_TexCoords );
        
    ccGLBlendFunc( CC_BLEND_SRC,CC_BLEND_DST );
    ccGLBindTexture2D( m_pTexture->getName() );

    glVertexAttribPointer(kCCVertexAttrib_Position, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*)(&m_vecVertics[0]));
    glVertexAttribPointer(kCCVertexAttrib_TexCoords, 2, GL_FLOAT, GL_FALSE, 0, (GLvoid*)(&m_vecCoordinates[0]));

    glDrawArrays(GL_TRIANGLE_STRIP, 0, m_vecVertics.size()/2);
}

void Lightning::addLightningSegment( float x1, float y1, float x2, float y2, float displace )
{
    if (displace <=m_pTexture->getPixelsWide()/2 )
    {
        m_vecLightningTrackPoint.push_back(ccp(x2,y2));
    }
    else
    {
        float mid_x = (x2+x1)/2;
        float mid_y = (y2+y1)/2;
        mid_x += (CCRANDOM_0_1() - 0.5) * displace;
        mid_y += (CCRANDOM_0_1() - 0.5) * displace;
        addLightningSegment(x1, y1, mid_x, mid_y, displace/2);
        addLightningSegment(mid_x, mid_y,x2, y2, displace/2);
    }
}

void Lightning::update(float delta)
{
    calculateVertex();
}

void Lightning::calculateVertex()
{
    m_vecLightningTrackPoint.clear();
    m_vecLightningTrackPoint.push_back(CCPointZero);
    addLightningSegment(0,0,m_fLength,0,m_fLength/3);

    m_vecVertics.clear();
    m_vecCoordinates.clear();

    for (size_t i=0; i<m_vecLightningTrackPoint.size(); i++)
    {
        const auto& pt = m_vecLightningTrackPoint[i];

        m_vecVertics.push_back(pt.x);
        m_vecVertics.push_back(pt.y-m_pTexture->getPixelsHigh()/2);
        m_vecVertics.push_back(pt.x);
        m_vecVertics.push_back(pt.y+m_pTexture->getPixelsHigh()/2);

        m_vecCoordinates.push_back(i);
        m_vecCoordinates.push_back(0);
        m_vecCoordinates.push_back(i);
        m_vecCoordinates.push_back(1);
    }
}

void Lightning::setTexture( cocos2d::CCTexture2D *texture )
{
    CC_SAFE_RETAIN(texture);
    CC_SAFE_RELEASE(m_pTexture);
    m_pTexture = texture;
  ccTexParams params = {GL_LINEAR,GL_LINEAR,GL_REPEAT,GL_REPEAT};
   m_pTexture->setTexParameters(&params);
}

 

代码下载

 

转载于:https://www.cnblogs.com/mrblue/p/4315091.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值