Skip to content

Commit

Permalink
fix: fix clock timing on web. closes #234
Browse files Browse the repository at this point in the history
  • Loading branch information
Luke Petherbridge committed May 17, 2024
1 parent 7428811 commit 57d323d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 2 deletions.
3 changes: 2 additions & 1 deletion tetanes/src/nes/emulation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -853,7 +853,8 @@ impl State {
}
thread::park_timeout(self.target_frame_duration - park_epsilon);
return;
} else if !self.rewinding && self.should_park() {
}
if !self.rewinding && self.should_park() {
thread::park_timeout(self.audio.queued_time().saturating_sub(self.audio.latency));
return;
}
Expand Down
17 changes: 16 additions & 1 deletion tetanes/src/nes/event.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use tetanes_core::{
use tracing::{error, trace};
use winit::{
event::{ElementState, Event, WindowEvent},
event_loop::{ControlFlow, EventLoopProxy, EventLoopWindowTarget},
event_loop::{ControlFlow, DeviceEvents, EventLoopProxy, EventLoopWindowTarget},
keyboard::PhysicalKey,
window::WindowId,
};
Expand Down Expand Up @@ -224,6 +224,9 @@ impl Nes {
event_loop.exit();
return;
}
// Disable device events to save some cpu as they're mostly duplicated in
// WindowEvents
event_loop.listen_device_events(DeviceEvents::Never);
if let State::Running(state) = &mut self.state {
if let Some(window) = state
.renderer
Expand Down Expand Up @@ -322,6 +325,18 @@ impl Running {
}

self.emulation.clock_frame();

// Event loop timing isn't great on web, so try to clock as fast as possible by
// requesting a redraw every time which will wake up the event loop, and then
// trigger AboutToWait again.
#[cfg(target_arch = "wasm32")]
if let Some(window) = self
.renderer
.root_window_id()
.and_then(|window_id| self.renderer.window(window_id))
{
window.request_redraw();
}
}
Event::WindowEvent {
window_id, event, ..
Expand Down

0 comments on commit 57d323d

Please sign in to comment.