Timers in C++

This is a discussion on Timers in C++ within the C++ Programming 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

    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.

  4. #4
    pronounced 'fib' FillYourBrain's Avatar
    Join Date
    Aug 2002
    Posts
    2,297
    of course if you're app is a windows app you don't want to do that. Hard loops are not nice for windows apps. SetTimer, KillTimer, WM_TIMER is the stuff you want to look at if that is the case. Of course if you're using a dos app I think there is a non-windowsAPI way of doing what GetTickCount does so that you don't have to include windows.h
    "You are stupid! You are stupid! Oh, and don't forget, you are STUPID!" - Dexter

  5. #5
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    You could probably use time(NULL) too, though it's accuracy is seconds rather than milliseconds.
    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.

  6. #6
    CALVIN
    Guest

    timer

    gettimeofday() gives you the time in microseconds on UNIX or Linux.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Threads and Timers
    By scioner in forum C Programming
    Replies: 8
    Last Post: 03-22-2008, 07:56 AM
  2. Enumerating timers and hotkeys
    By maxorator in forum Windows Programming
    Replies: 3
    Last Post: 01-01-2007, 10:47 AM
  3. switch cases and timers
    By soranz in forum C++ Programming
    Replies: 5
    Last Post: 10-02-2005, 06:43 PM
  4. Timers, Timers, Timers!
    By Stan100 in forum Game Programming
    Replies: 9
    Last Post: 01-24-2003, 03:45 PM
  5. 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