#include "video_GL_glfw.h"
#include <iostream>
		
bool OpenGL_Particles::_set_particle(int particle_num, float x, float y, int r, int g, int b, float a)
{
	if(particle_num>=_count_)return false;

	if(_use_point_sprite_)
	{
		_particles_[particle_num*2]=_base_x_+x;
		_particles_[particle_num*2+1]=_base_y_+y;	
		
		_colors_[particle_num*4]=(float)r/255.;
		_colors_[particle_num*4+1]=(float)g/255.;
		_colors_[particle_num*4+2]=(float)b/255.;
		_colors_[particle_num*4+3]=a;
	}
	else
	{
		_particles_[particle_num*8]=_base_x_+x;
		_particles_[particle_num*8+1]=_base_y_+y;
		
		_particles_[particle_num*8+2]=_base_x_+x;
		_particles_[particle_num*8+3]=_base_y_+y+_fsize_;
		
		_particles_[particle_num*8+4]=_base_x_+x+_fsize_;
		_particles_[particle_num*8+5]=_base_y_+y+_fsize_;
		
		_particles_[particle_num*8+6]=_base_x_+x+_fsize_;
		_particles_[particle_num*8+7]=_base_y_+y;
		
		_texture_[particle_num*8]=0;
		_texture_[particle_num*8+1]=0;
		
		_texture_[particle_num*8+2]=0;
		_texture_[particle_num*8+3]=1;
		
		_texture_[particle_num*8+4]=1;
		_texture_[particle_num*8+5]=1;
		
		_texture_[particle_num*8+6]=1;
		_texture_[particle_num*8+7]=0;
	
		_colors_[particle_num*16]=(float)r/255.;
		_colors_[particle_num*16+1]=(float)g/255.;
		_colors_[particle_num*16+2]=(float)b/255.;
		_colors_[particle_num*16+3]=a;
	
		_colors_[particle_num*16+4]=(float)r/255.;
		_colors_[particle_num*16+5]=(float)g/255.;
		_colors_[particle_num*16+6]=(float)b/255.;
		_colors_[particle_num*16+7]=a;
		
		_colors_[particle_num*16+8]=(float)r/255.;
		_colors_[particle_num*16+9]=(float)g/255.;
		_colors_[particle_num*16+10]=(float)b/255.;
		_colors_[particle_num*16+11]=a;
	
		_colors_[particle_num*16+12]=(float)r/255.;
		_colors_[particle_num*16+13]=(float)g/255.;
		_colors_[particle_num*16+14]=(float)b/255.;
		_colors_[particle_num*16+15]=a;
	}
	
	return true;
}

bool OpenGL_Particles::_get_particle(int particle_num, float * x, float * y, int * r, int * g, int * b, float * a)
{
	if(particle_num>=_count_)return false;
	if(_use_point_sprite_)
	{
		*x=_particles_[particle_num*2];
		*y=_particles_[particle_num*2+1];
		
		*r=(int)(_colors_[particle_num*4]*255.);
		*g=(int)(_colors_[particle_num*4+1]*255.);
		*b=(int)(_colors_[particle_num*4+2]*255.);
		*a=_colors_[particle_num*4+3];
	}
	else
	{
		*x=_particles_[particle_num*8];
		*y=_particles_[particle_num*8+1];
		
		*r=(int)(_colors_[particle_num*16]*255.);
		*g=(int)(_colors_[particle_num*16+1]*255.);
		*b=(int)(_colors_[particle_num*16+2]*255.);
		*a=_colors_[particle_num*16+3];
	}
	return true;	
}

OpenGL_Particles::~OpenGL_Particles()
{
	if(_particles_)
	{
		delete[] _particles_;
	}
	if(_colors_)
	{
		delete[] _colors_;
	}
}
void OpenGL_Particles::_set_particle_size(int size)
{
	_size_=size;
	if(!_use_point_sprite_)
	{
		_size_=size;
		_fsize_=(float)_size_/((float)_vid_->_get_height()/2.);
	}
}

