lexsdl 0.3.0

A wrapper for SDL2 to abstract away annoying parts
//! Still work in progress.

use lexlib::Vec2;
use sdl2_sys::*;
use crate::*;

pub struct Particle {
	pos: Vec2,
	pub direction: f32,
	pub spread: f32,
	velocity: i32,
	quantity: u32,
	radius: u16,
	particles: Vec<Vec2>,
	particlesRad: Vec<f32>,
}

impl Particle {
	pub fn new(pos: Vec2, quantity: u32,direction: f32) -> Self {
		let mut tmp = Self {
			pos,
			direction,
			spread: 0.,
			velocity: 5,
			quantity,
			radius: 220,
			particles: Vec::with_capacity(quantity.try_into().unwrap()),
			particlesRad: Vec::with_capacity(quantity.try_into().unwrap()),
		};
		
		for i in 0..quantity {
			tmp.particles.push(Vec2::new(tmp.pos.x + i as i32,tmp.pos.y + i as i32));
			tmp.particlesRad.push(i as f32);
		}
		
		tmp
	}
	
	// using C's rand().
	pub fn update(&mut self){
		for i in 0..self.quantity as usize{
			unsafe {
				self.particlesRad[i] += ((rand() % 10) + 1) as f32;
				
				self.particles[i].x = (rand() % 10 + 1) + (self.particlesRad[i] * (self.direction * std::f32::consts::PI / 180.).sin()) as i32 + self.pos.x;
				self.particles[i].y = (rand() % 10 + 1) - (self.particlesRad[i] * (self.direction * 3.14 / 180.).cos()) as i32 + self.pos.y;
				
				self.particles[i].y = (self.particlesRad[i] * ((rand() as f32 % self.spread + self.direction) * std::f32::consts::PI / 180.).cos()) as i32 + self.pos.y;
				self.particles[i].x = (self.particlesRad[i] * ((rand() as f32 % self.spread + self.direction) * std::f32::consts::PI / 180.).sin()) as i32 + self.pos.x;
				
				if self.particlesRad[i] > self.radius.into()
					{self.particlesRad[i] = 0.;}
			}
		}
	}
	
	pub fn render(&mut self){
		for i in 0..self.quantity {
			unsafe { SDL_RenderDrawPoint(LEXSDLDATA.renderer, self.particles[i as usize].x,self.particles[i as usize].y); }
		}
		
		let y = self.radius as f32 * (self.direction * std::f32::consts::PI / 180.).cos() + self.pos.y as f32;
		let x = self.radius as f32 * (self.direction * std::f32::consts::PI / 180.).sin() + self.pos.x as f32;
		unsafe { SDL_RenderDrawLine(LEXSDLDATA.renderer, self.pos.x, self.pos.y,x as i32,y as i32); }
		
		self.update();
	}
}