My Timing System

This is a discussion on My Timing System within the Windows Programming forums, part of the Platform Specific Boards category; This is my timing system that uses the Win32 API's GetTickCount for timing. At times, it seems like it isn't ...

  1. #1
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532

    My Timing System

    This is my timing system that uses the Win32 API's GetTickCount for timing. At times, it seems like it isn't counting the seconds correctly. Here is my simple timing system.
    Code:
        int iTickCount;
        static int iPrevTick=0;
        while(GetMessage(&msg,NULL,0,0))
         {
                                        TranslateMessage(&msg);
                                        DispatchMessage(&msg);
                                        iTickCount=GetTickCount();
                                        if(iTickCount>=iPrevTick)
                                        {
                                                                iPrevTick=iTickCount+1000;
                                                                cge->AddSecond();
                                        }
          }
    cge is my instance of a class I wrote, and the AddSecond member function increments a variable that holds how many seconds has passed. Then I have it display the value of that variable, that holds the elapsed seconds, when you click on the window. This number doesn't look right sometimes, it looks like it isn't catching all the seconds and I was wondering if I am doing something wrong? Thanks.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  2. #2
    Registered User Tonto's Avatar
    Join Date
    Jun 2005
    Location
    New York
    Posts
    1,465
    I think (?) you should probably use PeekMessage(..) here instead of GetMessage(..)

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,597
    GetTickCount
    The GetTickCount function retrieves the number of milliseconds that have elapsed since the system was started. It is limited to the resolution of the system timer. To obtain the system timer resolution, use the GetSystemTimeAdjustment function.

    DWORD GetTickCount(VOID);
    Parameters
    This function has no parameters.

    Return Values
    The return value is the number of milliseconds that have elapsed since the system was started.

    Remarks
    The elapsed time is stored as a DWORD value. Therefore, the time will wrap around to zero if the system is run continuously for 49.7 days.

    If you need a higher resolution timer, use a multimedia timer or a high-resolution timer.

    Windows NT/2000: To obtain the time elapsed since the computer was started, retrieve the System Up Time counter in the performance data in the registry key HKEY_PERFORMANCE_DATA. The value returned is an 8-byte value. For more information, see Performance Monitoring.

    Example
    The following example demonstrates how to handle timer wrap around.

    DWORD dwStart = GetTickCount();

    // Stop if this has taken too long
    if( GetTickCount() - dwStart >= TIMELIMIT )
    Cancel();

    Where TIMELIMIT is the time interval of interest to the application.

    Requirements
    Windows NT/2000: Requires Windows NT 3.1 or later.
    Windows 95/98: Requires Windows 95 or later.
    Header: Declared in Winbase.h; include Windows.h.
    Library: Use Kernel32.lib.

    See Also
    Time Overview, Time Functions
    You may want to check the timer resolution. Adding 1000 to the tick count is probably not going to be accurate at all times.

  4. #4
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    As Tonto pointed out, you need to use PeekMessage(). Currently, if your application doesn't receive any message for 5 seconds, then your timer code won't get executed for 5 seconds.

    Something like this:
    Code:
    if(PeekMessage(...))
       TranslateMessage(...);
       DispatchMessage(...);
    else
       //timer code here
    end if
    Make sure and pass the PM_REMOVE flag to PeekMessage().

  5. #5
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Alright thanks guys. I'll use PeekMessage instead and I'll look into that too Bubba.

    EDIT:
    Setting up my main message loop like you suggested, bithub, fixed the problem. That was the reason the timer was "lagging" behind because the lack of messages. I now have it set up like this:
    Code:
    while(cge->IsRunning())
             {    
                                        if(PeekMessage(&msg,NULL,0,0,PM_REMOVE))
                                        {
                                             TranslateMessage(&msg);
                                             DispatchMessage(&msg);
                                        }
                                        cge->RenderAll();
                                        SwapBuffers(cge->GetGEDC());
                                        iTickCount=GetTickCount();
                                        if(iTickCount>=iPrevTick)
                                        {
                                                                iPrevTick=iTickCount+1000;
                                                                cge->AddSecond();
                                        } 
             }
    I don't really need to explain my variables and classes because I'm just showing you how it's set-up, but cge is my main game engine class.
    Last edited by jmd15; 01-02-2006 at 12:16 AM.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Using system icons
    By @nthony in forum Windows Programming
    Replies: 1
    Last Post: 01-13-2007, 07:56 PM
  2. Open GL - glTranslate + Game Timing
    By Tonto in forum Game Programming
    Replies: 9
    Last Post: 10-24-2006, 04:20 AM
  3. Linux database system needed
    By BobS0327 in forum Tech Board
    Replies: 7
    Last Post: 06-11-2006, 04:56 PM
  4. measuring system resources used by a function
    By Aran in forum C Programming
    Replies: 1
    Last Post: 03-13-2006, 05:35 PM
  5. BIOS system and memory allocation problem
    By beely in forum Tech Board
    Replies: 9
    Last Post: 11-25-2003, 07:12 AM

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