-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
5630d16
commit 3fc0471
Showing
1 changed file
with
79 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} |