Skip to content

Commit

Permalink
Merge #1822
Browse files Browse the repository at this point in the history
1822: Switch from .run_forever() to .poll_events() in gfx_app r=kvark a=jturner314

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

PR checklist:
- [ ] `make` succeeds (on *nix)
  I don't see a Makefile. `make` says `make: *** No targets specified and no makefile found.  Stop.`
- [ ] `make reftests` succeeds
  I don't see a Makefile. I did run `cargo test`, though, and that succeeded (although it ran only 3 tests).
- [X] tested examples with the following backends:
  When I run `glxinfo`, I see `OpenGL core profile version string: 3.3 (Core Profile) Mesa 17.3.3`. I tested all of the examples. They all worked correctly, except `terrain_tessellated` (which failed to build even before this commit) and `triangle` (which still has the `.run_forever()` issue since it doesn't use `gfx_app`).
  • Loading branch information
bors[bot] committed Feb 17, 2018
2 parents eb3f2c8 + f0b2a50 commit f625df5
Showing 1 changed file with 29 additions and 27 deletions.
56 changes: 29 additions & 27 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,43 +148,45 @@ 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 f625df5

Please sign in to comment.