Skip to content

Commit

Permalink
simpler implementation
Browse files Browse the repository at this point in the history
  • Loading branch information
illwieckz committed May 16, 2024
1 parent 90571ed commit ea35ac4
Showing 1 changed file with 7 additions and 20 deletions.
27 changes: 7 additions & 20 deletions src/engine/qcommon/common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -861,16 +861,12 @@ void Com_Frame()
minMsec = 1;
}

int beforeProcessing = Sys::Milliseconds();

Com_EventLoop();

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

int afterProcessing = Sys::Milliseconds();

com_frameTime = afterProcessing;
com_frameTime = Sys::Milliseconds();

// lastTime can be greater than com_frameTime on first frame.
lastTime = std::min( lastTime, com_frameTime );
Expand All @@ -879,30 +875,21 @@ void Com_Frame()

while ( msec < minMsec )
{
int processingTime = afterProcessing - beforeProcessing;

/* Fill as much as input frames as possible within this timeframe to not
let the game process events in a way it would last longer than that. */
int jamTime = std::max( afterProcessing, 50 );
// Never sleep more than 50ms.
// Never sleep when there is 2ms or less remaining.
int sleep = std::max( std::min( minMsec - msec, 50 ) - 2, 0 );

int sleep = minMsec - msec;

// Let enough time to process events and jam frames after sleeping.
if ( sleep > jamTime * 2 )
if ( sleep )
{
// Give cycles back to the OS.
Sys::SleepFor( std::chrono::milliseconds( processingTime ) );
Sys::SleepFor( std::chrono::milliseconds( sleep ) );
}

beforeProcessing = Sys::Milliseconds();

Com_EventLoop();

IN_Frame();

afterProcessing = Sys::Milliseconds();

com_frameTime = afterProcessing;
com_frameTime = Sys::Milliseconds();

msec = com_frameTime - lastTime;
}
Expand Down

0 comments on commit ea35ac4

Please sign in to comment.