Last active
July 12, 2022 14:36
-
-
Save audinue/aea2da9ef0369f720c028f1780065141 to your computer and use it in GitHub Desktop.
Common game loop's sleep
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
void SleepSync(int targetFps) | |
{ | |
static DWORD previous = 0; | |
if (previous == 0) | |
previous = GetTickCount(); | |
LONGLONG current = GetTickCount(); | |
LONGLONG sleepTime = (1000 / targetFps) - (current - previous); | |
previous = current; | |
if (sleepTime > 0) | |
Sleep(sleepTime); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <windows.h> | |
double SleepSyncTime() | |
{ | |
double frequency; | |
double counter; | |
QueryPerformanceFrequency((LARGE_INTEGER*) &frequency); | |
QueryPerformanceCounter((LARGE_INTEGER*) &counter); | |
return counter / frequency; | |
} | |
void SleepSync(double targetFps) | |
{ | |
static double previous = 0.0; | |
if (previous == 0.0) | |
previous = SleepSyncTime(); | |
double current = SleepSyncTime(); | |
double sleepTime = (1.0 / targetFps) - (current - previous); | |
previous = current; | |
if (sleepTime > 0.0) | |
Sleep(sleepTime * 1000.0); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment