I've been developing a small framework for OpenGL and WinApi for some research purposes. My biggest problem right now is the game loop. Simplified I did something like this: Main thread:
HANDLE hUpdate = (HANDLE)_beginthreadex(0, 0, updateThread, 0, 0, 0);
while (!done)
{
if (PeekMessage(&msg, hWnd, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
{
done = TRUE;
}
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
else
{
Draw();
}
}
Update thread:
unsigned int __stdcall updateThread(void *params)
{
QueryPerformanceCounter(&nextTick);
QueryPerformanceCounter(&lastUpdate);
while (!done)
{
LARGE_INTEGER currentTick;
QueryPerformanceCounter(¤tTick);
while (currentTick.QuadPart > nextTick.QuadPart)
{
double frameTime = ((currentTick.QuadPart - lastUpdate.QuadPart) * 1.0) / clock.QuadPart;
QueryPerformanceCounter(&lastUpdate);
Update(frameTime);
nextTick.QuadPart += TIME_STEP * clock.QuadPart;
QueryPerformanceCounter(¤tTick);
}
}
return 0;
}
This way i got fixed time step while draw will be called as much as it can (kinda what I was looking for). The BIG problem is that everything is operating using busy wait, and it just swallows CPU and still no real game logic has been written. Can anyone tell me, or redirect me to some good, efficient implementation of game loop? While I was researching I came across information that Sleep() is bad for game loop.