#include "particle.h"
#include <windows.h>
#include <iostream>

extern vec3_t v_right,v_up,v_forward,g_CamAngles;

Particle::Particle(HSPRITE sprite)
{
	m_flStartTime = gEngfuncs.GetClientTime();
	position = vec3_t(0,0,0);	
	color = vec3_t(1,1,1);
	m_flLifeTime = 0;
	m_flMaxLifeTime = -1;
	m_flSize = 1.0;
	m_fEnabled = true;
	m_iFrame = 0;
	m_flRotation=0;
	m_flRotationSpeed=0;
	m_iRenderMode = kRenderTransAdd;
	m_hsprSprite = sprite;
	SetThink(&Particle::defaultThink);
	SetDraw(&Particle::defaultDraw);
}

Particle::Particle(vec3_t _position, HSPRITE sprite)
{
	m_flStartTime = gEngfuncs.GetClientTime();
	position = _position;
	color = vec3_t(1,1,1);
	startColor = vec3_t(1,1,1);
	m_flLifeTime = 0;
	m_flMaxLifeTime = -1;
	m_flSize = 1.0;
	m_hsprSprite = sprite;
	m_iFrame = 0;
	m_flRotation=0;
	m_flRotationSpeed=0;
	m_iRenderMode = kRenderTransAdd;
	m_fEnabled = true;
	SetThink(&Particle::defaultThink);
	SetDraw(&Particle::defaultDraw);
}
//-------------------------------------------------------------------------
Particle::~Particle()
{	
}
//-------------------------------------------------------------------------
void Particle::doThink(float time)
{	(*this.*think)(time-m_flStartTime);  		
}
//-------------------------------------------------------------------------
void Particle::doDraw()
{	if(m_fEnabled)
		(*this.*draw)();  			
}
//-------------------------------------------------------------------------
void Particle::defaultThink(float time)
{
	m_flLifeTime=time;
	m_flRotation=time*m_flRotationSpeed;
}
//-------------------------------------------------------------------------
void Particle::DrawWorldSprite( int RenderMode, TRICULLSTYLE tricullstyle, float a,int frame)
{
	
	vec3_t temporg;

	
	vec3_t vecRight = v_right;
	vec3_t vecUp = v_up;
	vec4_t qt;

	AxisAngleToQuaternion(v_forward,m_flRotation,qt);
	
	float rotMatrix[3][4];
	QuaternionMatrix( qt, rotMatrix );
	rotMatrix[0][3] = 0;
	rotMatrix[1][3] = 0;
	rotMatrix[2][3] = 0;
	//rotMatrix[3][3] = 1;
	VectorTransform(v_up, rotMatrix, vecUp);
	VectorTransform(v_right, rotMatrix, vecRight);

	float flSize = m_flSize * 0.5;
	if ( !gEngfuncs.pTriAPI->SpriteTexture( (struct model_s *)gEngfuncs.GetSpritePointer( m_hsprSprite ), frame ))
	{
		return;
	}
	//ConsolePrint("Drawing particle\n");
	gEngfuncs.pTriAPI->RenderMode( RenderMode );
	gEngfuncs.pTriAPI->CullFace( tricullstyle );
	gEngfuncs.pTriAPI->Begin( TRI_QUADS );
	
	//gEngfuncs.pTriAPI->Color4f( color.x, color.y, color.z, 1.0 );
	
	gEngfuncs.pTriAPI->TexCoord2f( 0, 0 );
	temporg = position + (vecRight * flSize) + (vecUp * flSize);
	gEngfuncs.pTriAPI->Vertex3f( temporg.x, temporg.y, temporg.z );
	
	gEngfuncs.pTriAPI->TexCoord2f( 0, 1 );	
	temporg = position + (vecRight * flSize) + (vecUp * -flSize);
	gEngfuncs.pTriAPI->Vertex3f( temporg.x, temporg.y, temporg.z );

	gEngfuncs.pTriAPI->TexCoord2f( 1, 1 );	
	temporg = position + (vecRight * -flSize) + (vecUp * -flSize);
	gEngfuncs.pTriAPI->Vertex3f( temporg.x, temporg.y, temporg.z );
	
	gEngfuncs.pTriAPI->TexCoord2f( 1, 0 );
	temporg = position + (vecRight * -flSize) + (vecUp * flSize);
	gEngfuncs.pTriAPI->Vertex3f( temporg.x, temporg.y, temporg.z );
	
	gEngfuncs.pTriAPI->End();
}
void Particle::defaultDraw()
{
	DrawWorldSprite( m_iRenderMode, TRI_FRONT, 1.0,m_iFrame);		
}
