-
timer class
Does anyone have any ideas on how to implement an effective "timer" class. As of now i'm using system calls and sleeping for a second, but it's not very accurate as you can imagine...The class objects basically need to behave like stopwatches...
i'm not looking for code, just an idea or two...Thanks for the input...
[E]
-
You can use GetTickCount(), which returns the number of milliseconds since windows started. It's located in windows.h.
If you want the program to run at a constant speed, you can do this:
Code:
DWORD LastTickCount;
DWORD CurrentTickCount;
DWORD TickDelay = 20; // 50 Fps --> 1000 / 50 = 20
while(ProgramIsRunning)
{
LastTickCount = GetTickCount();
DoSomething();
do
{
CurrentTickCount = GetTickCount();
}
while((CurrentTickCount - LastTickCount) < TickDelay);
}
<thought>Why is it called GetTickCount() when it actually returns milliseconds, not ticks???</though>
-
You can also use the stuff in ctime like time(), clock(), things like that. I don't think GetTickCount is portable, so your class would probably only work with windows.