Skip to content

Commit

Permalink
qcommon: let the framerate flirt with the limits
Browse files Browse the repository at this point in the history
  • Loading branch information
illwieckz committed May 27, 2024
1 parent 7b5abf5 commit e8779b8
Showing 1 changed file with 22 additions and 8 deletions.
30 changes: 22 additions & 8 deletions src/engine/qcommon/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -862,25 +862,39 @@ void Com_Frame()
}

Com_EventLoop();

// It must be called at least once.
IN_Frame();

com_frameTime = Sys::Milliseconds();

if ( lastTime > com_frameTime )
{
lastTime = com_frameTime; // possible on first frame
}
// lastTime can be greater than com_frameTime on first frame.
lastTime = std::min( lastTime, com_frameTime );

msec = com_frameTime - lastTime;

IN_Frame(); // must be called at least once
// For framerates up to 250fps, sleep until 1ms is remaining
// use extra margin of 2ms when looking for an higher framerate.
int margin = minMsec > 3 ? 1 : 2;

while ( msec < minMsec )
{
//give cycles back to the OS
Sys::SleepFor(std::chrono::milliseconds(std::min(minMsec - msec, 50)));
IN_Frame();
// Never sleep more than 50ms.
// Never sleep when there is only “margin” left or less remaining.
int sleep = std::min( std::max( minMsec - msec - margin, 0 ), 50 );

if ( sleep )
{
// Give cycles back to the OS.
Sys::SleepFor( std::chrono::milliseconds( sleep ) );
}

Com_EventLoop();

IN_Frame();

com_frameTime = Sys::Milliseconds();

msec = com_frameTime - lastTime;
}

Expand Down

0 comments on commit e8779b8

Please sign in to comment.