rust_minifb/examples/mouse.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

69 lines
2.1 KiB
Rust

extern crate minifb;
use minifb::{Key, MouseButton, MouseMode, Scale, Window, WindowOptions};
const WIDTH: usize = 640;
const HEIGHT: usize = 360;
fn main() {
let mut buffer: Vec<u32> = vec![0; WIDTH * HEIGHT];
let mut window = match Window::new(
"Mouse Draw - Press ESC to exit",
WIDTH,
HEIGHT,
WindowOptions {
scale: Scale::X2,
..WindowOptions::default()
},
) {
Ok(win) => win,
Err(err) => {
println!("Unable to create window {}", err);
return;
}
};
let (mut width, mut height) = (WIDTH, HEIGHT);
while window.is_open() && !window.is_key_down(Key::Escape) {
{
let (new_width, new_height) = window.get_size();
if new_width != width || new_height != height {
// Div by / 2 here as we use 2x scaling for the buffer
// copy valid bits of old buffer to new buffer
let mut new_buffer = vec![0; (new_width / 2) * (new_height / 2)];
for y in 0..(height / 2).min(new_height / 2) {
for x in 0..(width / 2).min(new_width / 2) {
new_buffer[y * (new_width / 2) + x] = buffer[y * (width / 2) + x];
}
}
buffer = new_buffer;
width = new_width;
height = new_height;
}
}
window.get_mouse_pos(MouseMode::Discard).map(|(x, y)| {
let screen_pos = ((y as usize) * (width / 2)) + x as usize;
if window.get_mouse_down(MouseButton::Left) {
buffer[screen_pos] = 0x00ffffff;
}
if window.get_mouse_down(MouseButton::Right) {
buffer[screen_pos] = 0;
}
});
window.get_scroll_wheel().map(|scroll| {
println!("Scrolling {} - {}", scroll.0, scroll.1);
});
// We unwrap here as we want this code to exit if it fails
window
.update_with_buffer(&buffer, width / 2, height / 2)
.unwrap();
}
}