2019-11-27 18:03:33 +11:00
|
|
|
use minifb::{Key, MouseButton, MouseMode, Scale, Window, WindowOptions};
|
2016-01-30 06:16:00 +11:00
|
|
|
|
|
|
|
const WIDTH: usize = 640;
|
|
|
|
const HEIGHT: usize = 360;
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let mut buffer: Vec<u32> = vec![0; WIDTH * HEIGHT];
|
|
|
|
|
2019-11-27 18:03:33 +11:00
|
|
|
let mut window = match Window::new(
|
|
|
|
"Mouse Draw - Press ESC to exit",
|
|
|
|
WIDTH,
|
|
|
|
HEIGHT,
|
|
|
|
WindowOptions {
|
|
|
|
scale: Scale::X2,
|
|
|
|
..WindowOptions::default()
|
|
|
|
},
|
|
|
|
) {
|
2016-01-30 06:16:00 +11:00
|
|
|
Ok(win) => win,
|
|
|
|
Err(err) => {
|
|
|
|
println!("Unable to create window {}", err);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2019-04-01 19:19:23 +11:00
|
|
|
let (mut width, mut height) = (WIDTH, HEIGHT);
|
|
|
|
|
2016-01-30 06:16:00 +11:00
|
|
|
while window.is_open() && !window.is_key_down(Key::Escape) {
|
2019-04-01 19:19:23 +11:00
|
|
|
{
|
|
|
|
let (new_width, new_height) = window.get_size();
|
|
|
|
if new_width != width || new_height != height {
|
2019-12-16 18:24:48 +11:00
|
|
|
// Div by / 2 here as we use 2x scaling for the buffer
|
2019-04-01 19:19:23 +11:00
|
|
|
// copy valid bits of old buffer to new buffer
|
2019-12-16 18:24:48 +11:00
|
|
|
let mut new_buffer = vec![0; (new_width / 2) * (new_height / 2)];
|
2019-04-01 19:19:23 +11:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-07 02:34:01 +11:00
|
|
|
if let Some((x, y)) = window.get_mouse_pos(MouseMode::Discard) {
|
2019-12-16 18:24:48 +11:00
|
|
|
let screen_pos = ((y as usize) * (width / 2)) + x as usize;
|
2016-01-30 06:16:00 +11:00
|
|
|
|
|
|
|
if window.get_mouse_down(MouseButton::Left) {
|
|
|
|
buffer[screen_pos] = 0x00ffffff;
|
|
|
|
}
|
|
|
|
|
|
|
|
if window.get_mouse_down(MouseButton::Right) {
|
|
|
|
buffer[screen_pos] = 0;
|
|
|
|
}
|
2021-12-07 02:34:01 +11:00
|
|
|
}
|
2016-01-30 06:16:00 +11:00
|
|
|
|
2021-12-07 02:34:01 +11:00
|
|
|
if let Some(scroll) = window.get_scroll_wheel() {
|
2016-01-30 06:16:00 +11:00
|
|
|
println!("Scrolling {} - {}", scroll.0, scroll.1);
|
2021-12-07 02:34:01 +11:00
|
|
|
}
|
2016-01-30 06:16:00 +11:00
|
|
|
|
2017-08-11 20:41:24 +10:00
|
|
|
// We unwrap here as we want this code to exit if it fails
|
2019-12-16 18:24:48 +11:00
|
|
|
window
|
|
|
|
.update_with_buffer(&buffer, width / 2, height / 2)
|
|
|
|
.unwrap();
|
2016-01-30 06:16:00 +11:00
|
|
|
}
|
|
|
|
}
|