Skip to content

Commit

Permalink
HOW DARE YOU
Browse files Browse the repository at this point in the history
  • Loading branch information
timelessnesses committed Dec 30, 2023
1 parent 5630d16 commit 3fc0471
Showing 1 changed file with 79 additions and 0 deletions.
79 changes: 79 additions & 0 deletions src/snow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
use rand::{self, Rng};
use sdl2;

// const SNOWFLAKE_SIZE: int = 5;

#[derive(Clone, Copy)]
pub struct Snowflake {
x: i32,
y: i32,
size: i32,
window_width: i32,
window_height: i32,
speed: i32,
}

impl Snowflake {
pub fn new(w: i32, h: i32, size: i32) -> Self {
let mut rng = rand::thread_rng();
return Snowflake {
x: rng.gen_range(0..w),
y: 0,
speed: rng.gen_range(1..5),
window_height: h,
window_width: w,
size,
};
}

pub fn update(&mut self) {
self.y += self.speed;
let mut rng = rand::thread_rng();
if self.y > self.window_height {
self.y = 0;
self.x = rng.gen_range(0..self.window_width);
self.speed = rng.gen_range(1..3);
}
}

pub fn render(&self, surface: &mut sdl2::surface::Surface, revert: bool) {
surface
.fill_rect(
sdl2::rect::Rect::new(self.x, self.y, self.size as u32, self.size as u32),
{
if !revert {
sdl2::pixels::Color::WHITE
} else {
sdl2::pixels::Color::BLACK
}
},
)
.unwrap();
}
}

#[derive(Clone)]
pub struct SnowParticles {
snowflakes: Vec<Snowflake>,
}

impl SnowParticles {
pub fn new(limit: i32, surface: &mut sdl2::surface::Surface) -> Self {
let mut snowflakes: Vec<Snowflake> = Vec::new();
for _ in 0..limit {
snowflakes.push(Snowflake::new(
surface.width() as i32,
surface.height() as i32,
5,
))
}
return Self { snowflakes };
}

pub fn render(&mut self, surface: &mut sdl2::surface::Surface, revert: bool) {
for snow in self.snowflakes.iter_mut() {
snow.update();
snow.render(surface, revert)
}
}
}

0 comments on commit 3fc0471

Please sign in to comment.