Problem with timeGetTime()

This is a discussion on Problem with timeGetTime() within the Windows Programming forums, part of the Platform Specific Boards category; Dear all, I am working on a long Windows program. I included all of the code so that I donít ...

  1. #1
    Registered User
    Join Date
    Jul 2005
    Posts
    64

    Problem with timeGetTime()

    Dear all,

    I am working on a long Windows program. I included all of the code so that I donít miss anything.

    Code:
    /* Inverted Pendulum Project */
    
    #include <windows.h>
    #include <string.h>
    #include <stdio.h>
    #include "Clock2.h"
    
    #define SamplingTime  3000    //in milliseconds
    
    
    LRESULT CALLBACK WindowFunc(HWND, UINT, WPARAM, LPARAM);  //Callback function prototype
    
    //Variables used:
    
    char szWinName[] = "MyWin"; /* name of window class */
    
    char str[255] = ""; /* holds output string */
    char *clr = "                                                                               ";
    BOOL greenlight = false;
    UINT xref, x;
    
    
    Clock timer;
    
    //Main program:
    int WINAPI WinMain(HINSTANCE hThisInst, HINSTANCE hPrevInst, 
                       LPSTR lpszArgs, int nWinMode)
    {
      HWND hwnd;
      MSG msg;
      WNDCLASSEX wcl;
      
      /* Define a window class. */
      wcl.cbSize = sizeof(WNDCLASSEX); 
    
      wcl.hInstance = hThisInst; /* handle to this instance */
      wcl.lpszClassName = szWinName; /* window class name */
      wcl.lpfnWndProc = WindowFunc; /* window function */
      wcl.style = 0; /* default style */
    
      wcl.hIcon = LoadIcon(NULL, IDI_APPLICATION); /* standard icon */
      wcl.hIconSm = LoadIcon(NULL, IDI_WINLOGO); /* small icon */
      wcl.hCursor = LoadCursor(NULL, IDC_ARROW); /* cursor style */
    
      wcl.lpszMenuName = NULL; /* no main menu */
      wcl.cbClsExtra = 0; /* no extra */
      wcl.cbWndExtra = 0; /* information needed */
    
      /* Make the window white. */
      wcl.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); 
    
      /* Register the window class. */
      if(!RegisterClassEx(&wcl)) return 0;
    
      /* Now that a window class has been registered, a window
         can be created. */
      hwnd = CreateWindow(
        szWinName, /* name of window class */
        "Processing Mouse Messages", /* title */
        WS_OVERLAPPEDWINDOW, /* window style - normal */
        CW_USEDEFAULT, /* X coordinate - let Windows decide */
        CW_USEDEFAULT, /* Y coordinate - let Windows decide */
        CW_USEDEFAULT, /* width - let Windows decide */
        CW_USEDEFAULT, /* height - let Windows decide */
        HWND_DESKTOP, /* no parent window */
        NULL, /* no menu */
        hThisInst, /* handle of this instance of the program */
        NULL /* no additional arguments */
      );
    
      /* Display the window. */
      ShowWindow(hwnd, nWinMode);
      UpdateWindow(hwnd);
    
      /* Create the message loop. */
      while(GetMessage(&msg, NULL, 0, 0))
      {
        TranslateMessage(&msg); /* translate keyboard messages */
        DispatchMessage(&msg); /* return control to Windows 98 */
      }
      return msg.wParam;
    }
    
    /* This function is called by Windows 98 and is passed 
       messages from the message queue.
    */
    LRESULT CALLBACK WindowFunc(HWND hwnd, UINT message,
                                WPARAM wParam, LPARAM lParam)
    {
      HDC hdc;
        
      switch(message) {
    
      case WM_CHAR:
    	  hdc = GetDC(hwnd);
    	  switch(wParam)
    	  {
    	    case 'r': case 'R':			//Pressing 'r' or 'R' sets reference point, resets timer
    			greenlight = true;
    			timer.reset();
    			xref=x;
    			TextOut(hdc, 10, 400, clr, strlen(clr));
    			sprintf(str, "xref = %d", xref);
    			TextOut(hdc, 10, 400, str, strlen(str));
    			break;
    		case 's': case 'S':			//Pressing 's' or 'S' halts all operations
    			greenlight = false;
    			break;
    		default:
    			greenlight = greenlight;
    	  }
    	  ReleaseDC(hwnd, hdc);
    	  break;
    
    
        case WM_RBUTTONDOWN: // Clicking right mouse button clears "YES!" message
          hdc = GetDC(hwnd);
    	  TextOut(hdc, 100, 400, clr, strlen(clr));
          ReleaseDC(hwnd, hdc);
          break;
    
        
    	case WM_LBUTTONDOWN: // Clicking left mouse button closes the window (terminates operations)
          hdc = GetDC(hwnd);
    	  PostQuitMessage(0);
          ReleaseDC(hwnd, hdc);
          break;
    
    	
    	case WM_MOUSEMOVE:  // Processing pointer motion
    	  hdc = GetDC(hwnd);
    	  if (greenlight == 1)
    	  {
    		  TextOut(hdc, 10, 50, clr, strlen(clr));
    		  sprintf(str, "Time passed is %f seconds", (float)timer.time()/1000);
    		  TextOut(hdc, 10, 50, str, strlen(str));
    		  TextOut(hdc, 10, 10, clr, strlen(clr));
    		  sprintf(str, "Mouse coordinates are at (%d, %d)", LOWORD(lParam), HIWORD(lParam));
    		  TextOut(hdc, 10, 10, str, strlen(str));
    		  if (timer.sample(SamplingTime))
    		  {
    			  TextOut(hdc, 100, 400, clr, strlen(clr));
    			  sprintf(str, "YES!");
    			  TextOut(hdc, 100, 400, str, strlen(str));
    		  }
    	  }
    	  x=LOWORD(lParam);		//Set x-coordinate
    	  ReleaseDC(hwnd,hdc);
    	  break;
    
    
        case WM_DESTROY: // Terminate the program
    	  PostQuitMessage(0);
          break;
    
    
        default:
           /* Let Windows 98 process any messages not specified in
             the preceding switch statement. */
          return DefWindowProc(hwnd, message, wParam, lParam);
      }
      return 0;
    }
    Here is the Clock2.h header file:

    Code:
    MMRESULT begin = timeBeginPeriod(1);
    MMRESULT end = timeEndPeriod(2);
    
    class Clock
    {
    	private:
    		DWORD timestarted;		//time started (in seconds)
    		//DWORD sampler;
    	public:
    		Clock();
    		BOOL sample(const int&);
    		VOID reset();
    		VOID wait(const int&);		//wait a certain number of seconds
    		long time();					//return the time in seconds since last reset
    };
    
    Clock::Clock()
    {
    	//timestarted=(float)timeGetTime()/1000;
    	(*this).reset();
    }
    
    VOID Clock::reset()
    {
    	timestarted = timeGetTime();
    	//sampler = timeGetTime();
    	return;
    }
    
    long Clock::time()
    {
    	return timeGetTime()-timestarted;  //return time elapsed in seconds
    }
    
    VOID wait(const int &tsec)
    {
    	DWORD beginning = timeGetTime();
    	while(beginning + tsec > timeGetTime())
    	{ /* wait */}
    	return;
    }
    
    BOOL Clock::sample(const int &msec)
    {
    	int center=time()%msec;
    	if(center >= 0 && center <= 4)
    		return true;
    	else
    		return false;
    }
    The problem is the following: I want to get the mouse coordinates every, say, 3 seconds. That is the point of the ďsampleĒ member function in the Clock2.h header file. Just to test the response of the computer, I ordered a ďYES!Ē message to be displayed every time the sampling period elapses (so, in this example, every 3 seconds). This is not working unless I have an error range allowing at least 4 milliseconds to pass. Is there no way to do this with zero error (accumulation of error will render my program useless after a few seconds, because the actual time is less than 3 seconds). Thank you oh so dearly.

  2. #2
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You can't expect mouse input to work with that time resolution. The rate at which movement messages are sent by the mouse is limited, the rate at which your application processes mouse messages is limited. According to this page, the reporting rate of a mouse can be as slow as 50 times per second (every 20ms). This works because the time resolution of humans is also not that good. Simple reaction time for a human is about 160ms. This increases when a choice or recognition is required.

    As for getting better resolution, you might consider the GetMessageTime function (which will tell you when the mouse move message was posted) or the QueryPerformanceCounter function (which provides better resolution than timeGetTime).
    Last edited by anonytmouse; 01-15-2006 at 12:33 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help understanding a problem
    By dnguyen1022 in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2009, 05:21 PM
  2. Memory problem with Borland C 3.1
    By AZ1699 in forum C Programming
    Replies: 16
    Last Post: 11-16-2007, 11:22 AM
  3. Someone having same problem with Code Block?
    By ofayto in forum C++ Programming
    Replies: 1
    Last Post: 07-12-2007, 09:38 AM
  4. A question related to strcmp
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 07-07-2007, 03:51 PM
  5. WS_POPUP, continuation of old problem
    By blurrymadness in forum Windows Programming
    Replies: 1
    Last Post: 04-20-2007, 07:54 PM

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