This is very difficult to search for because of that other Rust.
let frame_length = std::time::Duration::new(0, 4_166_166);
let mut timer = std::time::Instant::now();
while window.is_open() && !window.is_key_down(Key::Escape) {
let elapsed_time = timer.elapsed();
if elapsed_time >= frame_length {
canvas.clear();
let our_input = Input::from_window(&window);
game.update([our_input; 8]);
// TODO: let frame_ready = elapsed_time >= some_runtime_determined_refresh_rate;
// if frame_ready {
graphics::draw(&game, &mut canvas);
// }
window.update_with_buffer(&canvas.buffer).unwrap();
timer = std::time::Instant::now();
}
}
I have a game loop I'm trying to make framerate-independent by running the updating logic at a high rate, but rendering only when the monitor is ready. Unfortunately, monitor manufacturers keep making up new refresh rates with wildly-non-integral multiples of each other, so if I want my game to have a deterministic, fixed timestep and to render smoothly, I believe I need to check the refresh rate at runtime and check whether the loop should render on this step.
If there are other solutions to the problem, such as using VSYNC from Rust without it taking over game logic loop updating frequency, that's cool too.
As an aside, I use a tight loop without sleeping here because it seems to have less judder.