FAQ: Timers in C++ [Archive] - C Board

PDA

View Full Version : FAQ: Timers in C++


RoD
11-18-2002, 02:04 PM
Ok i know this has been adressed before, and i even knew the answer not long ago, but when i went to the FAQ for a reference it wasn't in there, and since i need an answer and its an often asked question, i figured why not post, ask, then submit it into the FAQ for future readers, so here it is:

In C++ i want to set a time limit on my game. Not with decrements in for loops or anything, but with a timer, i think the functions i am looking for are in ctime.h or something like that. Thanks!


say int timer = 600(seconds)

while (timer >=1 || do_this != 'Q')
{

.....

}

Magos
11-18-2002, 02:16 PM
In windows.h, there is GetTickCount(), returning the number of milliseconds since windows started. You could use that.

DWORD StartTime = GetTickCount()
DWORD TimeFrame = 1000 * 60;

while((GetTickCount() - StartTime) < TimeFrame)
{
...
}

RoD
11-18-2002, 02:36 PM
Thanks, that did it.