From f0b2a500e2e00a51d15327324fc65a2be3306bf4 Mon Sep 17 00:00:00 2001 From: Jim Turner Date: Fri, 16 Feb 2018 21:03:19 -0500 Subject: [PATCH] Switch from .run_forever() to .poll_events() This commit uses `.poll_events()` to allow processing multiple events per rendered frame. (Before, only one event was processed per frame.) This is necessary because it's easy for the user to generate events much faster than frames can be rendered (e.g. by moving the mouse). Additionally, with `.run_forever()`, rendering blocked on receiving events. (An event had to occur before a frame was rendered.) In order to see an animation such as the `particle` example, the user had to generate events fast enough to cause frames to be rendered (e.g. by moving the mouse over the window). See also https://github.com/tomaka/winit/issues/276 and https://github.com/tomaka/winit/issues/231 --- src/lib.rs | 56 ++++++++++++++++++++++++++++-------------------------- 1 file changed, 29 insertions(+), 27 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 8df02b7fba4..e6c744e6e8f 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -148,34 +148,37 @@ A: Sized + ApplicationBase return ControlFlow::Break, - winit::WindowEvent::KeyboardInput { - input: winit::KeyboardInput { - state: winit::ElementState::Pressed, - virtual_keycode: key, + let mut running = true; + while running { + events_loop.poll_events(|event| { + if let winit::Event::WindowEvent { event, .. } = event { + match event { + winit::WindowEvent::Closed => running = false, + winit::WindowEvent::KeyboardInput { + input: winit::KeyboardInput { + state: winit::ElementState::Pressed, + virtual_keycode: key, + .. + }, .. + } if key == A::get_exit_key() => running = false, + winit::WindowEvent::Resized(width, height) => { + if width != cur_width || height != cur_height { + window.resize(width, height); + cur_width = width; + cur_height = height; + let (new_color, new_depth) = gfx_window_glutin::new_views(&window); + app.on_resize(&mut factory, WindowTargets { + color: new_color, + depth: new_depth, + aspect_ratio: width as f32 / height as f32, + }); + } }, - .. - } if key == A::get_exit_key() => return ControlFlow::Break, - winit::WindowEvent::Resized(width, height) => if width != cur_width || height != cur_height { - window.resize(width, height); - cur_width = width; - cur_height = height; - let (new_color, new_depth) = gfx_window_glutin::new_views(&window); - app.on_resize(&mut factory, WindowTargets { - color: new_color, - depth: new_depth, - aspect_ratio: width as f32 / height as f32, - }); - }, - _ => app.on(event), + _ => app.on(event), + } } - } + }); // draw a frame app.render(&mut device); @@ -183,8 +186,7 @@ A: Sized + ApplicationBase