-
Since the I want to limit the CPU usage of my real-time rendering program. To prevent the event loop from always being triggered, I have to use Did I miss something? Here is the code: match event {
Event::WindowEvent { window_id, ref event } if window_id == state.window().id() => {
match event {
// ...
WindowEvent::RedrawRequested => {
state.window().request_redraw(); // ! emit
let elapsed_time = Instant::now().duration_since(start_time).as_millis() as u64;
let wait_millis = match 1000 / TARGET_FPS >= elapsed_time {
true => 1000 / TARGET_FPS - elapsed_time,
false => 0,
};
let new_inst = start_time + std::time::Duration::from_millis(wait_millis);
target.set_control_flow = ControlFlow::WaitUntil(new_inst); // ! not working
render();
}
_ => {}
}
}
_ => {}
} |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment
-
To do what you want I'd suggest to register some In the end you'll render at the target frame rate, since you start rendering at the start of each timestamp. You may still not render at the start of each timestamp, however it'll mean that you can't target the frame rate you have desired. The schedule for the next timestamp must be done after the |
Beta Was this translation helpful? Give feedback.
WaitUntil
works exactly as it should, it waits until the deadline or a new event, which is redraw requested.To do what you want I'd suggest to register some
Instant
, then compute the future timestamps based on the target frame-rate you have, then you doWaitUntil
and in a new events you compare the current time with the next timeout, and if's later you schedule the redraw via theredraw_requested
. In theRedrawRequsted
itself you don't ask for redraw, since that's not what you want, however you set theWaitUntil
to thenext
timestamp.In the end you'll render at the target frame rate, since you start rendering at the start of each timestamp. You may still not render at the start of each …