Thread: timer class

  1. #1
    Registered User
    Join Date
    Jun 2002

    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...


  2. #2
    Confused Magos's Avatar
    Join Date
    Sep 2001
    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:
    DWORD LastTickCount;
    DWORD CurrentTickCount;
    DWORD TickDelay = 20;  // 50 Fps --> 1000 / 50 = 20
       LastTickCount = GetTickCount();
          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 10:28 AM.

    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
    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, 05: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, 01:30 PM