FAQ: Timers in C++

This is a discussion on FAQ: Timers in C++ within the FAQ Board forums, part of the General Programming Boards category; Ok i know this has been adressed before, and i even knew the answer not long ago, but when i ...

  1. #1
    RoD
    RoD is offline
    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
    RoD
    RoD is offline
    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, 03:34 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21