⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 explosion.cpp

📁 极限赛车CarGameDemo DirectX9
💻 CPP
字号:
#include "StdAfx.h"
#include "../common/myd3d.h"
#include "explosion.h"
using namespace d3d;

extern IDirect3DDevice9* g_device;
extern HWND g_hwnd;
Explosion::Explosion()
{
	
}

Explosion::~Explosion()
{

}

void Explosion::Init( ExplosionType explosionType, LPCSTR pFilename )
{
	if( explosionType == CAR)
	{
		_time = 5.0f;
	}
	else
	{
		_time = 1.0f;
	}
	_explosionType = explosionType;
	_usedNum	   = explosionType;
	_isExp		   = false;
	_expTime	   = 0.0f;
	_pTex		   = 0;
	_pVertexBuffer = 0;
	HRESULT hr = 0;

	// 载入点精灵纹理
	D3DXCreateTextureFromFile( g_device, pFilename, &_pTex );

	g_device->SetSamplerState(0, D3DSAMP_MINFILTER, D3DTEXF_LINEAR);
	g_device->SetSamplerState(0, D3DSAMP_MAGFILTER, D3DTEXF_LINEAR);

	// 为点精灵创建顶点缓存
    g_device->CreateVertexBuffer( 2048 * sizeof(Vertex2), 
                                  D3DUSAGE_DYNAMIC | D3DUSAGE_WRITEONLY | D3DUSAGE_POINTS, 
								  Vertex2::FVF_Flags, D3DPOOL_DEFAULT, 
								  &_pVertexBuffer, 0 );
 
	//设备检查

	float fMaxPointSize = 0.0f;
	bool  bDeviceSupportsPSIZE = false;
	
    D3DCAPS9 d3dCaps;
    g_device->GetDeviceCaps( &d3dCaps );

    fMaxPointSize = d3dCaps.MaxPointSize;

    if( d3dCaps.FVFCaps & D3DFVFCAPS_PSIZE )
        bDeviceSupportsPSIZE = true;
    else
        bDeviceSupportsPSIZE = false;
	
	//设置随机属性
	for( int i = 0; i < _usedNum; ++i )
    {
		/*_particles[i]._pos = D3DXVECTOR3(0.0f,0.0f,0.0f);*/
		_particles[i]._vel = getRandomVector() * getRandomMinMax( 0.5f, 5.0f );

		_particles[i]._color = D3DCOLOR_COLORVALUE( getRandomMinMax( 0.0f, 1.0f ), 
													   getRandomMinMax( 0.0f, 1.0f ), 
													   getRandomMinMax( 0.0f, 1.0f ), 
													   1.0f );
	}

}

void Explosion::Clean()
{
	d3d::Release<LPDIRECT3DTEXTURE9>( _pTex);
	d3d::Release<LPDIRECT3DVERTEXBUFFER9>( _pVertexBuffer);
}

void Explosion::Update(float timeDelta)
{
	if(_isExp)
	{
		//用于计算渲染了多长时间
		float fElpasedAppTime = (float)((timeGetTime() - _expTime) * 0.001);
		
		//当持续时间达到时重设置位置
		if( fElpasedAppTime >= _time )
		{
			_isExp = false;
		}
		//粒子更新位置
		for( int i = 0; i < _usedNum; ++i )
			_particles[i]._pos += _particles[i]._vel * timeDelta;
	}
}

void Explosion::Rend()
{
	if(_isExp)
	{

		g_device->SetRenderState(D3DRS_LIGHTING, false);			//关闭灯光

		g_device->SetRenderState( D3DRS_ZWRITEENABLE, FALSE );		//关闭写深度缓存
		g_device->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );	//开启alpha通道
		g_device->SetRenderState( D3DRS_DESTBLEND, D3DBLEND_ONE );	//设置混合状态
		
		//
		g_device->SetRenderState( D3DRS_POINTSPRITEENABLE, TRUE );    // 开启点精灵
		g_device->SetRenderState( D3DRS_POINTSCALEENABLE,  TRUE );    // 开启点精灵缩放
		g_device->SetRenderState( D3DRS_POINTSIZE,     FtoDw(1.0) );  // 点精灵尺寸
		g_device->SetRenderState( D3DRS_POINTSIZE_MIN, FtoDw(1.0f) ); 
		g_device->SetRenderState( D3DRS_POINTSCALE_A,  FtoDw(0.0f) ); // 根据视野距离控制点精灵尺寸
		g_device->SetRenderState( D3DRS_POINTSCALE_B,  FtoDw(0.0f) ); 
		g_device->SetRenderState( D3DRS_POINTSCALE_C,  FtoDw(1.0f) ); 

		//存放粒子到顶点缓存(使用Vertex2格式)
		Vertex2 *pPointVertices;

		_pVertexBuffer->Lock( 0, _usedNum * sizeof(Vertex2),
							   (void**)&pPointVertices, D3DLOCK_DISCARD );

		for( int i = 0; i < _usedNum; ++i )
		{
			pPointVertices->posit = _particles[i]._pos;
			pPointVertices->color = _particles[i]._color;
			pPointVertices++;
		}

		_pVertexBuffer->Unlock();
		
		//
		// 渲染点精灵...
		g_device->SetTexture( 0, _pTex);							//设置纹理
		/*g_device->SetMaterial(NULL);*/
		D3DXMATRIX normalMatrix;
		D3DXMatrixIdentity( &normalMatrix );
		g_device->SetTransform(D3DTS_WORLD, &normalMatrix);
		g_device->SetStreamSource( 0, _pVertexBuffer, 0, sizeof(Vertex2) );
		g_device->SetFVF( Vertex2::FVF_Flags );
		g_device->DrawPrimitive( D3DPT_POINTLIST, 0, _usedNum );

		//
		// 恢复渲染状态...
		//
		g_device->SetRenderState(D3DRS_LIGHTING,          true);
		g_device->SetRenderState( D3DRS_POINTSPRITEENABLE, FALSE );
		g_device->SetRenderState( D3DRS_POINTSCALEENABLE,  FALSE );

		g_device->SetRenderState( D3DRS_ZWRITEENABLE, TRUE );
		g_device->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE );
	}

}

void Explosion::setPos(const D3DXVECTOR3 &pos)
{
	for( int i = 0; i < _usedNum; ++i )
    {
		_particles[i]._pos = pos;
	}
}

void Explosion::getExpTime()
{
	_expTime = (float)timeGetTime();
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -