Skip to content

Use nanosleep for I_SleepDuration on *nix

Hanicef requested to merge Hanicef/SRB2:use-unix-nanosleep into next

most unix systems use something called a "high-resolution timer" which is a timer that ticks at high frequency (as the name implies). this timer is the same timer that is used by the wall clock, and as such, is able to achieve the same accuracy as the wall time, which is often on microsecond accuracy. this makes it an ideal solution for frame capping, since the CPU is actually sleeping while we're capping. on my system, when capping the game to 35 fps, SRB2's CPU usage goes down to around 20% during gameplay, in contrast to the previous solution which normally used more than 85% CPU.

the only catch is that it's not supported on Windows, and i also haven't been able to test that Mac OS is using the same technique for nanosleep (if someone can verify, please do!). because of that, Mac OS (for now, at least) and Windows will remain using the old frame capping logic.

as for the accuracy, well... img

...and for reference, the old solution... img2

...it's pretty damn accurate. not perfect, but you aren't going to notice such a small difference at 300 fps anyway.

Merge request reports