use minifb::{Window, WindowOptions};
use std::{cell::RefCell, panic, rc::Rc};
use wasm_bindgen::prelude::*;
const WIDTH: usize = 1280;
const HEIGHT: usize = 720;
fn window() -> web_sys::Window {
web_sys::window().expect("no global `window` exists")
}
fn request_animation_frame(f: &Closure<dyn FnMut()>) {
window()
.request_animation_frame(f.as_ref().unchecked_ref())
.expect("should register `requestAnimationFrame` OK");
}
#[wasm_bindgen(start)]
fn main() {
panic::set_hook(Box::new(console_error_panic_hook::hook));
let container = "minifb-container";
let mut window = Window::new(container, WIDTH, HEIGHT, WindowOptions::default())
.expect("Unable to create the window");
let mut buffer = vec![0; WIDTH * HEIGHT];
let f = Rc::new(RefCell::new(None));
let g = f.clone();
window.update_with_buffer(&buffer, WIDTH, HEIGHT).unwrap();
*g.borrow_mut() = Some(Closure::wrap(Box::new(move || {
for pixel in buffer.iter_mut() {
*pixel = pixel.wrapping_add(1);
}
let _ = window.update_with_buffer(&buffer, WIDTH, HEIGHT);
request_animation_frame(f.borrow().as_ref().unwrap());
}) as Box<dyn FnMut() + 'static>));
request_animation_frame(g.borrow().as_ref().unwrap());
}