mirror of
https://github.com/italicsjenga/rust_minifb.git
synced 2024-12-23 19:31:30 +11:00
Add char_callback example
This commit is contained in:
parent
65e5be1dcf
commit
2646964b9e
65
examples/char_callback.rs
Normal file
65
examples/char_callback.rs
Normal file
|
@ -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<RefCell<Vec<u32>>>;
|
||||||
|
|
||||||
|
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<u32> = 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();
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue