rust_minifb/examples/noise.rs
Daniel Collin 8b3c2e9b37
Merge dev -> master (#119)
* Deprecated update_with_buffer and added a temporary (update_with_buffer_size) for now. This will later be removed and the update_with_buffer is requiring the size to bu suplied

* Reparation for 0.14 release

* Missed one case

* Minor cleanup

* Switch to C scalar for Unix + rename

Reason is so we can always use optimized scalar even in debug.
Also removed _size so only update_with_buffer(..) takes width, height of the input buffer

* Implemented AspectRatio aware scale on nix

* Implemented image center

* Added UpperLeft center mode for unix

* Moving macOS over to sized update

* Fixed resize not working on macOS

* WIP on macOS

* More WIP on macOS version

* Bunch of macOS updates and fixes

* Fixed broken bg color on macOS

* Windows fixes WIP

* Remove some spamming

* More windows fixes

* Windows fixes for cursor and warnings

* Some cleanup

* rustfmt pass

* Fixed typo

* Added support for limiting update rate

* Added update rate to Windows

* Added update rate to macOS

* Misc fixes

* Fixed resources and maintance badge

* Updated readme

* Updated changelog

* Added rate limit
2019-12-16 08:24:48 +01:00

66 lines
1.6 KiB
Rust

extern crate minifb;
use minifb::{Key, ScaleMode, Window, WindowOptions};
const WIDTH: usize = 640 / 2;
const HEIGHT: usize = 360 / 2;
fn main() {
let mut noise;
let mut carry;
let mut seed = 0xbeefu32;
let mut window = Window::new(
"Noise Test - Press ESC to exit",
WIDTH,
HEIGHT,
WindowOptions {
resize: true,
scale_mode: ScaleMode::UpperLeft,
..WindowOptions::default()
},
)
.expect("Unable to create window");
// Limit to max ~60 fps update rate
window.limit_update_rate(Some(std::time::Duration::from_micros(16600)));
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().0, window.get_size().1);
if new_size != size {
size = new_size;
buffer.resize(size.0 * size.1, 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!"),
_ => (),
}
}
});
window
.update_with_buffer(&buffer, new_size.0, new_size.1)
.unwrap();
}
}