timer class

This is a discussion on timer class within the C++ Programming forums, part of the General Programming Boards category; Does anyone have any ideas on how to implement an effective "timer" class. As of now i'm using system calls ...

  1. #1
    Registered User
    Join Date
    Jun 2002
    Posts
    25

    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]

  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    Location
    Sweden
    Posts
    3,145
    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>
    Last edited by Magos; 09-11-2002 at 11:28 AM.
    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
    Registered User Dr. Bebop's Avatar
    Join Date
    Sep 2002
    Posts
    96
    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.
    Processing error: Stupidity detected.
    ------------------------------
    Dr. Bebop
    Windows XP Professional Ed.
    Microsoft Visual Studio 6

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. class composition constructor question...
    By andrea72 in forum C++ Programming
    Replies: 3
    Last Post: 04-03-2008, 06:11 PM
  2. deriving classes
    By l2u in forum C++ Programming
    Replies: 12
    Last Post: 01-15-2007, 05:01 PM
  3. Mmk, I give up, lets try your way. (Resource Management)
    By Shamino in forum Game Programming
    Replies: 31
    Last Post: 01-18-2006, 09:54 AM
  4. allegro timer in a class
    By MadHatter in forum Game Programming
    Replies: 7
    Last Post: 12-06-2002, 10:06 PM
  5. Abstract class problem
    By VanJay011379 in forum C++ Programming
    Replies: 9
    Last Post: 07-31-2002, 02:30 PM

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