Restructure the event loop. Inspired by Bevy.
@@ -116,13 +116,13 @@ pub fn main_loop<GS: GameState>(mut bterm: BTerm, mut gamestate: GS) -> BResult<
wc.window().set_inner_size(glutin::dpi::PhysicalSize::new(rr.0, rr.1));
}*/
- match event {
- Event::NewEvents(_) => {
- clear_input_state(&mut bterm);
- }
+ let my_window_id = wc.window().id();
+
+ match &event {
Event::MainEventsCleared => {
let execute_ms = now.elapsed().as_millis() as u64 - prev_ms as u64;
if execute_ms >= wait_time && wc.window().inner_size().width > 0 {
+ let start_render = std::time::Instant::now();
if queued_resize_event.is_some() {
if let Some(resize) = &queued_resize_event {
wc.resize(resize.physical_size);
@@ -141,21 +141,33 @@ pub fn main_loop<GS: GameState>(mut bterm: BTerm, mut gamestate: GS) -> BResult<
&now,
);
wc.swap_buffers().unwrap();
+ let render_elapsed = start_render.elapsed().as_millis() as u64;
- // I'm not convinced that this is doing anything
- /*if *control_flow != ControlFlow::Exit {
- let next_tick = now.elapsed().as_millis() as u64 - execute_ms;
- if next_tick >= wait_time {
- *control_flow = ControlFlow::WaitUntil(std::time::Instant::now() + std::time::Duration::from_millis(next_tick - wait_time));
+ if wait_time > render_elapsed {
+ if *control_flow != ControlFlow::Exit {
+ *control_flow = ControlFlow::WaitUntil(
+ std::time::Instant::now() + std::time::Duration::from_millis(wait_time - render_elapsed)
+ );
+ } else {
+ if *control_flow != ControlFlow::Exit {
+ *control_flow = TICK_TYPE;
}
- }*/
}
}
- Event::RedrawRequested { .. } => {
- // Do nothing
+
+ // Moved from new events, which doesn't make sense
+ clear_input_state(&mut bterm);
+ }
}
- Event::LoopDestroyed => (),
- Event::WindowEvent { ref event, .. } => match event {
+ Event::WindowEvent{event, window_id} => {
+ // Fast return for other windows
+ if *window_id != my_window_id {
+ //println!("Dropped event from other window");
+ return;
+ }
+
+ // Handle Window Events
+ match event {
WindowEvent::Moved(physical_position) => {
bterm.on_event(BEvent::Moved {
new_position: Point::new(physical_position.x, physical_position.y),
@@ -243,9 +255,10 @@ pub fn main_loop<GS: GameState>(mut bterm: BTerm, mut gamestate: GS) -> BResult<
bterm.alt = modifiers.alt();
bterm.control = modifiers.ctrl();
}
- _ => (),
- },
- _ => (),
+ _ => {}
+ }
+ }
+ _ => {}
}
});
}
