Thread: FAQ: Timers in C++

  1. #1
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331

    FAQ: Timers in C++

    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')
    {

    .....

    }

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    In windows.h, there is GetTickCount(), returning the number of milliseconds since windows started. You could use that.
    Code:
    DWORD StartTime = GetTickCount()
    DWORD TimeFrame = 1000 * 60;
    
    while((GetTickCount() - StartTime) < TimeFrame)
    {
       ...
    }
    MagosX.com

    Give a man a fish and you feed him for a day.
    Teach a man to fish and you feed him for a lifetime.

  3. #3
    Redundantly Redundant RoD's Avatar
    Join Date
    Sep 2002
    Location
    Missouri
    Posts
    6,331
    Thanks, that did it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Wiki FAQ
    By dwks in forum A Brief History of Cprogramming.com
    Replies: 192
    Last Post: 04-29-2008, 01:17 PM
  2. FAQ Check/Lock
    By RoD in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 10-15-2002, 11:21 AM
  3. Timers
    By Mox in forum Windows Programming
    Replies: 2
    Last Post: 11-09-2001, 04:34 AM