Skip to content

Instantly share code, notes, and snippets.

@audinue
Last active July 12, 2022 14:36
Show Gist options
  • Save audinue/aea2da9ef0369f720c028f1780065141 to your computer and use it in GitHub Desktop.
Save audinue/aea2da9ef0369f720c028f1780065141 to your computer and use it in GitHub Desktop.
Common game loop's sleep
#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);
}
#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