1
0
Fork 0
mirror of https://github.com/doukutsu-rs/doukutsu-rs synced 2025-07-16 17:02:54 +00:00

mitigations for windows' broken system timer

This commit is contained in:
Khang 2021-01-03 19:18:38 -05:00
parent 8b8c543526
commit 807b095b15

View file

@ -141,7 +141,15 @@ impl Game {
let state_ref = unsafe { &mut *self.state.get() };
if state_ref.timing_mode != TimingMode::FrameSynchronized {
let n1 = (self.start_time.elapsed().as_nanos() - self.last_tick) as f64;
// Even with the non-monotonic Instant mitigation at the start of the event loop,
// there's still a theoretical chance of it not working.
// This check here should trigger if that happens and makes sure there's no panic from an underflow.
let mut elapsed = self.start_time.elapsed().as_nanos();
if elapsed < self.last_tick {
warn!("Elapsed time was less than last tick! elapsed: {}, last tick: {}", elapsed, self.last_tick);
elapsed = self.last_tick;
}
let n1 = (elapsed - self.last_tick) as f64;
let n2 = (self.next_tick - self.last_tick) as f64;
state_ref.frame_time = n1 / n2;
}
@ -331,6 +339,12 @@ pub fn init() -> GameResult {
context = Some(init_ctx(&event_loop, resource_dir.clone())?);
event_loop.run(move |event, target, flow| {
#[cfg(target_os = "windows")]
{
// Windows' system clock implementation isn't monotonic when the process gets switched to another core.
// Rust has mitigations for this, but apparently aren't very effective unless Instant is called very often.
let _ = Instant::now();
}
if let Some(ctx) = &mut context {
ctx.process_event(&event);