better looking snow
This commit is contained in:
parent
f53aef4f67
commit
5c0dd5558e
25
src/main.rs
25
src/main.rs
|
@ -9,7 +9,7 @@ use sdl2::render::{WindowCanvas, BlendMode};
|
||||||
use sdl2::image::ImageRWops;
|
use sdl2::image::ImageRWops;
|
||||||
use sdl2::surface::Surface;
|
use sdl2::surface::Surface;
|
||||||
use sdl2::pixels::{Color, PixelFormatEnum};
|
use sdl2::pixels::{Color, PixelFormatEnum};
|
||||||
use sdl2::rect::Rect;
|
use sdl2::rect::Point;
|
||||||
|
|
||||||
use rand::{SeedableRng, Rng};
|
use rand::{SeedableRng, Rng};
|
||||||
use rand_xoshiro::Xoshiro128Plus;
|
use rand_xoshiro::Xoshiro128Plus;
|
||||||
|
@ -44,7 +44,7 @@ struct Zretro {
|
||||||
font: Surface<'static>,
|
font: Surface<'static>,
|
||||||
rng: Xoshiro128Plus,
|
rng: Xoshiro128Plus,
|
||||||
ui_bg: Option<Surface<'static>>,
|
ui_bg: Option<Surface<'static>>,
|
||||||
snowflakes: Vec<Rect>,
|
snowflakes: Vec<Point>,
|
||||||
}
|
}
|
||||||
|
|
||||||
const FONT_PNG: &'static [u8] = include_bytes!("lifont.png");
|
const FONT_PNG: &'static [u8] = include_bytes!("lifont.png");
|
||||||
|
@ -101,10 +101,10 @@ impl Zretro {
|
||||||
let mut ui_bg = Surface::new(geometry.base_width, geometry.base_height, PixelFormatEnum::ABGR8888)?;
|
let mut ui_bg = Surface::new(geometry.base_width, geometry.base_height, PixelFormatEnum::ABGR8888)?;
|
||||||
ui_bg.fill_rect(None, Color::RGBA(0, 0, 128, 128))?;
|
ui_bg.fill_rect(None, Color::RGBA(0, 0, 128, 128))?;
|
||||||
let snowflakes = (0..100).into_iter()
|
let snowflakes = (0..100).into_iter()
|
||||||
.map(|_| Rect::new(
|
.map(|_| Point::new(
|
||||||
rng.gen_range(0..ui_bg.width() as i32),
|
rng.gen_range(0..ui_bg.width() as i32),
|
||||||
rng.gen_range(0..ui_bg.height() as i32),
|
rng.gen_range(0..ui_bg.height() as i32),
|
||||||
2, 2))
|
))
|
||||||
.collect();
|
.collect();
|
||||||
|
|
||||||
Ok(Zretro {
|
Ok(Zretro {
|
||||||
|
@ -122,15 +122,22 @@ impl Zretro {
|
||||||
ui_bg.fill_rect(None, Color::RGBA(0, 0, 128, 128))?;
|
ui_bg.fill_rect(None, Color::RGBA(0, 0, 128, 128))?;
|
||||||
let w = ui_bg.width() as i32;
|
let w = ui_bg.width() as i32;
|
||||||
let h = ui_bg.height() as i32;
|
let h = ui_bg.height() as i32;
|
||||||
for flake in &mut self.snowflakes {
|
let halflen = self.snowflakes.len() / 2;
|
||||||
let x = flake.x() + self.rng.gen_range(-1..=1);
|
for flake in &mut self.snowflakes[..halflen] {
|
||||||
|
let x = flake.x() + self.rng.gen_range(0..=1);
|
||||||
let y = flake.y() + 1;
|
let y = flake.y() + 1;
|
||||||
flake.set_x(x % w);
|
*flake = Point::new(x % w, y % h);
|
||||||
flake.set_y(y % h);
|
}
|
||||||
|
for flake in &mut self.snowflakes[halflen..] {
|
||||||
|
let x = flake.x();
|
||||||
|
let y = flake.y() + self.rng.gen_range(0..=1);
|
||||||
|
*flake = Point::new(x, y % h);
|
||||||
}
|
}
|
||||||
let mut ui_canvas = ui_bg.into_canvas()?;
|
let mut ui_canvas = ui_bg.into_canvas()?;
|
||||||
ui_canvas.set_draw_color(Color::RGBA(255, 255, 255, 160));
|
ui_canvas.set_draw_color(Color::RGBA(255, 255, 255, 160));
|
||||||
ui_canvas.draw_rects(&self.snowflakes[..])?;
|
ui_canvas.draw_points(&self.snowflakes[..halflen])?;
|
||||||
|
ui_canvas.set_draw_color(Color::RGBA(255, 255, 255, 128));
|
||||||
|
ui_canvas.draw_points(&self.snowflakes[halflen..])?;
|
||||||
self.ui_bg = Some(ui_canvas.into_surface());
|
self.ui_bg = Some(ui_canvas.into_surface());
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue