From 2646964b9e5bafbacafea27ff0968a44858efb74 Mon Sep 17 00:00:00 2001 From: Daniel Collin Date: Wed, 16 Sep 2020 08:32:32 +0200 Subject: [PATCH] Add char_callback example --- examples/char_callback.rs | 65 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 65 insertions(+) create mode 100644 examples/char_callback.rs diff --git a/examples/char_callback.rs b/examples/char_callback.rs new file mode 100644 index 0000000..83a82bb --- /dev/null +++ b/examples/char_callback.rs @@ -0,0 +1,65 @@ +use minifb::{Key, Window, WindowOptions}; +use std::cell::RefCell; +use std::rc::Rc; + +const WIDTH: usize = 640; +const HEIGHT: usize = 360; + +type KeyVec = Rc>>; + +struct Input { +keys: KeyVec, +} + +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 = vec![0; WIDTH * HEIGHT]; + + let mut window = Window::new( + "Test - ESC to exit", + WIDTH, + HEIGHT, + WindowOptions::default(), + ) + .unwrap_or_else(|e| { + panic!("{}", e); + }); + + 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 + window + .update_with_buffer(&buffer, WIDTH, HEIGHT) + .unwrap(); + + let mut keys = keys_data.borrow_mut(); + + for t in keys.iter() { + println!("keys {}", t); + } + + keys.clear(); + } +}