Skip to content

Instantly share code, notes, and snippets.

@heeen
Created January 17, 2020 09:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save heeen/9e36b28f4251a84c5f1a1411044231a2 to your computer and use it in GitHub Desktop.
Save heeen/9e36b28f4251a84c5f1a1411044231a2 to your computer and use it in GitHub Desktop.
use winit::{
event::{Event, WindowEvent},
event_loop::{ControlFlow},
};
use std::cell::RefCell;
pub struct ExampleBase {
pub window: winit::window::Window,
pub event_loop: RefCell<winit::event_loop::EventLoop<()>>,
pub test: i32,
}
impl ExampleBase {
pub fn render_loop<F: Fn()>(&self, f: F) {
// FIXME 1: cannot move out of dereference of `std::cell::RefMut<'_, winit::event_loop::EventLoop<()>>`
self.event_loop.borrow_mut().run(move |event, _, control_flow| {
//FIXME 2: the parameter type `F` may not live long enough
f();
*control_flow = ControlFlow::Wait;
match event {
Event::WindowEvent { event, .. } => {
match event {
WindowEvent::CloseRequested => {
*control_flow = ControlFlow::Exit;
}
_ => (),
}
}
_ => (),
}
});
}
pub fn new() -> Self {
let event_loop = winit::event_loop::EventLoop::new();
let window = winit::window::WindowBuilder::new()
.with_title("Example")
.with_inner_size(winit::dpi::LogicalSize::new( f64::from(800), f64::from(600),))
.build(&event_loop)
.unwrap();
let test = 42;
ExampleBase {
event_loop: RefCell::new(event_loop),
window,
test
}
}
}
fn main() {
let base = ExampleBase::new();
base.render_loop(|| {
println!("test: {:?}", base.test);
})
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment