Skip to content

Instantly share code, notes, and snippets.

@AlexanderDzhoganov
Last active April 4, 2019 14:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlexanderDzhoganov/e6b7b961678799906faf05beb85a9219 to your computer and use it in GitHub Desktop.
Save AlexanderDzhoganov/e6b7b961678799906faf05beb85a9219 to your computer and use it in GitHub Desktop.
bool netplay_manager::rollback(unsigned long long before_frame)
{
if (m_debug)
{
NETPLAY_LOG("rollback at %llu to %llu", m_frame_count, before_frame);
}
auto sys_start_time = system_time();
netplay_assert(before_frame <= m_frame_count); // impossible to go to the future
// store the current time, we'll advance the simulation to it later
auto current_frame = m_frame_count;
// ... some more stuff happens here but it's irrelevant to the problem ...
// let the emulator know we need to catch up
// this should disable sound and video updates during the simulation loop below
m_catching_up = true;
// run the emulator loop
// this will replay any new inputs that we have received since
while (m_frame_count < current_frame)
{
auto start_time = machine().time();
while (machine().time() < start_time + netplay_frametime)
{
machine().scheduler().timeslice();
}
m_frame_count++;
m_machine_time += netplay_frametime;
}
m_catching_up = false;
if (m_debug)
{
auto duration = system_time() - sys_start_time;
NETPLAY_LOG("rollback complete at time %s and frame %llu (took %dms)",
m_machine_time.as_string(4), m_frame_count, (int)(duration.as_double() * 1000.0));
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment