#include "StdAfx.h"
#include "Explosion.h"

CExplosion::CExplosion(CDirect3D *d3d) : 
  C3DObject(d3d->g_pd3dDevice),
  frame(1),
  lastFrame(0),
  isFinished(0)
{
  this->d3d = d3d;

  D3DXCreateTextureFromFileEx( d3d->g_pd3dDevice, "explode.bmp",
						D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, d3d->GetTextureFormat(),
                        D3DPOOL_MANAGED, D3DX_FILTER_TRIANGLE|D3DX_FILTER_MIRROR, 
                        D3DX_FILTER_TRIANGLE|D3DX_FILTER_MIRROR, D3DCOLOR_XRGB(255,255,255), NULL, NULL, &g_pExplode );
 
 vertex_count = 4;
 faces_count = 2;
 
 device->CreateVertexBuffer(
 	vertex_count * sizeof(Vertex),
	D3DUSAGE_WRITEONLY,
	Vertex::FVF,
	D3DPOOL_MANAGED,
	&VB,
	0);
 device->CreateIndexBuffer(
	3*faces_count * sizeof(WORD),
	D3DUSAGE_WRITEONLY,
	D3DFMT_INDEX16,
	D3DPOOL_MANAGED,
	&IB,
	0);
 
 Vertex* vertices;
 VB->Lock(0, 0, (void**)&vertices, 0);
	vertices[0].position = D3DXVECTOR3(0.0,0.0,0.0);
	vertices[1].position = D3DXVECTOR3(1.0,0.0,0.0);
	vertices[2].position = D3DXVECTOR3(1.0,1.0,0.0);
	vertices[3].position = D3DXVECTOR3(0.0,1.0,0.0);

	vertices[0].u = 0.0;
	vertices[0].v = (frame-1) * 1.0 / 16;

	vertices[1].u = 1.0;
	vertices[1].v = (frame-1) * 1.0 / 16;

	vertices[2].u = 1.0;
	vertices[2].v = frame * 1.0 / 16;

	vertices[3].u = 0.0;
	vertices[3].v = frame * 1.0 / 16;

	vertices[0].color = D3DCOLOR_XRGB(255,255,255);
	vertices[1].color = D3DCOLOR_XRGB(255,255,255);
	vertices[2].color = D3DCOLOR_XRGB(255,255,255);
	vertices[3].color = D3DCOLOR_XRGB(255,255,255);
 VB->Unlock();

 WORD* indices = 0;
 IB->Lock(0, 0, (void**)&indices, 0);
	indices[0] = 0;
	indices[1] = 1;
	indices[2] = 2;

	indices[3] = 0;
	indices[4] = 2;
	indices[5] = 3;
 IB->Unlock();
}

CExplosion::~CExplosion(void)
{
}

void CExplosion::Render()
{ 
 if( d3d->g_pd3dDevice && !isFinished)
 {
   d3d->g_pd3dDevice->SetRenderState( D3DRS_FILLMODE, D3DFILL_SOLID);

   d3d->g_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, TRUE );
   d3d->g_pd3dDevice->SetRenderState( D3DRS_SRCBLEND,         D3DBLEND_SRCALPHA );
   d3d->g_pd3dDevice->SetRenderState( D3DRS_DESTBLEND,        D3DBLEND_INVSRCALPHA );
   d3d->g_pd3dDevice->SetRenderState( D3DRS_ALPHATESTENABLE,  TRUE );
   d3d->g_pd3dDevice->SetRenderState( D3DRS_ALPHAREF,         0x01 );
   d3d->g_pd3dDevice->SetRenderState( D3DRS_ALPHAFUNC, D3DCMP_GREATEREQUAL );

   d3d->g_pd3dDevice->SetTexture( 0, g_pExplode );
   
   d3d->g_pd3dDevice->SetStreamSource(0, VB, 0, sizeof(Vertex));
   d3d->g_pd3dDevice->SetIndices(IB);
   d3d->g_pd3dDevice->SetFVF(Vertex::FVF);
   d3d->g_pd3dDevice->DrawIndexedPrimitive(D3DPT_TRIANGLELIST,0, 0, vertex_count, 0, faces_count);

   d3d->g_pd3dDevice->SetTexture(0, NULL);
   d3d->g_pd3dDevice->SetRenderState( D3DRS_ALPHABLENDENABLE, FALSE );

 }
}

void CExplosion::Explode(float x, float y, float z, float size)
{
	isFinished = 0;
	lastFrame = 0;
	frame = 1;

   Vertex* vertices;
   VB->Lock(0, 0, (void**)&vertices, 0);
	vertices[0].position = D3DXVECTOR3(0.0+x,0.0+y,0.0+z);
	vertices[1].position = D3DXVECTOR3(1.0*size+x,0.0+y,0.0+z);
	vertices[2].position = D3DXVECTOR3(1.0*size+x,1.0*size+y,0.0+z);
	vertices[3].position = D3DXVECTOR3(0.0+x,1.0*size+y,0.0+z);
   VB->Unlock();

}

void CExplosion::MoveFrame()
{
	if (isFinished)
	{
		return;
	}
	if (lastFrame == 0)
	{
		lastFrame = GetTickCount(); 
		return;
	}
	if (GetTickCount() - lastFrame > 1000 / 25)
	{
        frame = frame + 1;
		if (frame == 16) 
		{
			isFinished = 1;
			return;
		}
		Vertex* vertices;
		VB->Lock(0, 0, (void**)&vertices, 0);
			vertices[0].u = 0.0;
			vertices[0].v = (frame-1) * 1.0 / 16;
			vertices[1].u = 1.0;
			vertices[1].v = (frame-1) * 1.0 / 16;
			vertices[2].u = 1.0;
			vertices[2].v = frame * 1.0 / 16;
			vertices[3].u = 0.0;
			vertices[3].v = frame * 1.0 / 16;
		VB->Unlock();
		lastFrame = GetTickCount();
	}
}

int CExplosion::IsFinished()
{
	return isFinished;
}