rust_minifb/examples/noise.rs
2019-11-27 08:03:33 +01:00

69 lines
1.7 KiB
Rust

extern crate minifb;
use minifb::{Key, Scale, Window, WindowOptions};
const WIDTH: usize = 640;
const HEIGHT: usize = 360;
fn main() {
let mut noise;
let mut carry;
let mut seed = 0xbeefu32;
let mut window = match Window::new(
"Noise Test - Press ESC to exit",
WIDTH,
HEIGHT,
WindowOptions {
resize: true,
scale: Scale::X2,
..WindowOptions::default()
},
) {
Ok(win) => win,
Err(err) => {
println!("Unable to create window {}", err);
return;
}
};
let mut buffer: Vec<u32> = Vec::with_capacity(WIDTH * HEIGHT);
let mut size = (0, 0);
while window.is_open() && !window.is_key_down(Key::Escape) {
{
let new_size = window.get_size();
if new_size != size {
size = new_size;
buffer.resize(size.0 * size.1 / 2 / 2, 0);
}
}
for i in buffer.iter_mut() {
noise = seed;
noise >>= 3;
noise ^= seed;
carry = noise & 1;
noise >>= 1;
seed >>= 1;
seed |= carry << 30;
noise &= 0xFF;
*i = (noise << 16) | (noise << 8) | noise;
}
window.get_keys().map(|keys| {
for t in keys {
match t {
Key::W => println!("holding w!"),
Key::T => println!("holding t!"),
_ => (),
}
}
});
// We unwrap here as we want this code to exit if it fails
window.update_with_buffer(&buffer).unwrap();
}
}