Skip to content

Commit

Permalink
Switch from .run_forever() to .poll_events()
Browse files Browse the repository at this point in the history
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 rust-windowing/winit#276 and
rust-windowing/winit#231
  • Loading branch information
jturner314 committed Feb 17, 2018
1 parent eb3f2c8 commit 7af125f
Showing 1 changed file with 27 additions and 27 deletions.
54 changes: 27 additions & 27 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,43 +148,43 @@ A: Sized + ApplicationBase<gfx_device_gl::Resources, gfx_device_gl::CommandBuffe
});

let mut harness = Harness::new();
events_loop.run_forever(move |event| {
use glutin::ControlFlow;

if let winit::Event::WindowEvent { event, .. } = event {
match event {
winit::WindowEvent::Closed => 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);
window.swap_buffers().unwrap();
device.cleanup();

harness.bump();
ControlFlow::Continue
});
}
}


Expand Down

0 comments on commit 7af125f

Please sign in to comment.