2020-09-16 16:32:32 +10:00
|
|
|
use minifb::{Key, Window, WindowOptions};
|
|
|
|
use std::cell::RefCell;
|
|
|
|
use std::rc::Rc;
|
|
|
|
|
|
|
|
const WIDTH: usize = 640;
|
|
|
|
const HEIGHT: usize = 360;
|
|
|
|
|
|
|
|
type KeyVec = Rc<RefCell<Vec<u32>>>;
|
|
|
|
|
|
|
|
struct Input {
|
2020-09-16 16:40:27 +10:00
|
|
|
keys: KeyVec,
|
2020-09-16 16:32:32 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Input {
|
|
|
|
fn new(data: &KeyVec) -> Input {
|
|
|
|
Input { keys: data.clone() }
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl minifb::InputCallback for Input {
|
|
|
|
fn add_char(&mut self, uni_char: u32) {
|
|
|
|
self.keys.borrow_mut().push(uni_char);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() {
|
|
|
|
let mut buffer: Vec<u32> = vec![0; WIDTH * HEIGHT];
|
|
|
|
|
|
|
|
let mut window = Window::new(
|
2020-09-16 16:40:27 +10:00
|
|
|
"Test - ESC to exit",
|
|
|
|
WIDTH,
|
|
|
|
HEIGHT,
|
|
|
|
WindowOptions::default(),
|
|
|
|
)
|
|
|
|
.unwrap_or_else(|e| {
|
2021-11-15 18:45:44 +11:00
|
|
|
panic!("{:?}", e);
|
2020-09-16 16:40:27 +10:00
|
|
|
});
|
2020-09-16 16:32:32 +10:00
|
|
|
|
|
|
|
let keys_data = KeyVec::new(RefCell::new(Vec::new()));
|
|
|
|
|
|
|
|
let input = Box::new(Input::new(&keys_data));
|
|
|
|
|
|
|
|
// Limit to max ~60 fps update rate
|
|
|
|
window.limit_update_rate(Some(std::time::Duration::from_micros(16600)));
|
|
|
|
window.set_input_callback(input);
|
|
|
|
|
|
|
|
while window.is_open() && !window.is_key_down(Key::Escape) {
|
|
|
|
for i in buffer.iter_mut() {
|
|
|
|
*i = 0; // write something more funny here!
|
|
|
|
}
|
|
|
|
|
|
|
|
// We unwrap here as we want this code to exit if it fails. Real applications may want to handle this in a different way
|
2020-09-16 16:40:27 +10:00
|
|
|
window.update_with_buffer(&buffer, WIDTH, HEIGHT).unwrap();
|
2020-09-16 16:32:32 +10:00
|
|
|
|
|
|
|
let mut keys = keys_data.borrow_mut();
|
|
|
|
|
|
|
|
for t in keys.iter() {
|
2021-11-15 18:45:44 +11:00
|
|
|
println!("Code point: {}, Character: {:?}", *t, char::from_u32(*t));
|
2020-09-16 16:32:32 +10:00
|
|
|
}
|
|
|
|
|
|
|
|
keys.clear();
|
|
|
|
}
|
|
|
|
}
|