Advertisement

C++ and the system clock

Started by January 19, 2002 02:26 AM
1 comment, last by Holocron 22 years, 8 months ago
How do I do timing in C++? I assume there are some built in functions of the C++ Library that allow a function to follow an algarithim(sp) over a specified time. Any suggestions?
There is the ANSI time() function, but it only has a resolution of seconds. See also asctime(), gmtime() and localtime().

High-resolution timing is platform-specific. Under Win32, you can use GetTickCount(), timeGetTime(), QueryPerformanceCounter() and QueryPerformanceFrequency(), _ftime() and _utime() (not all of those are timing functions in the conventional sense). You can look them up online at MSDN Online.

[ GDNet Start Here | GDNet FAQ | MS RTFM | STL | Google ]
Thanks to Kylotan for the idea!
Advertisement
          #include <time.h>  // include time.hclass Time{	float timediff;					// time between previous and current frame	bool  hi_res_timer_available;	                // does the hardware support hi-res timing	LARGE_INTEGER hi_timefreq, hi_timecurrent, hi_timeprevious;                 // hi-res timer	DWORD					   lo_timecurrent, lo_timeprevious; // low-res timerpublic:		float GetTime() {	return timediff; }	bool HiClock() { return hi_res_timer_available;	}	// True if hi-res timer is in use	void Init()	{		if ( QueryPerformanceFrequency(&hi_timefreq) )	// hi-res timer is accessible, set it up!		{			hi_res_timer_available = true;				// tell UpdateTime to use hi-res variables			QueryPerformanceCounter(&hi_timeprevious);	// UpdateTime relies on having a good prev value		}		else		{			hi_res_timer_available = false;				// tell UpdateTime to use lo-res variables			lo_timeprevious = clock();				// UpdateTime relies on having a good prev value		}	}	void UpdateTime()	{		if (true == hi_res_timer_available)				// do we update the hi or lo-res timer		{			QueryPerformanceCounter(&hi_timecurrent);      // get the new hi-res time// Calculate the time elappsed since the last frame was drawn, and the current one			timediff= (float)(hi_timecurrent.QuadPart-hi_timeprevious.QuadPart)/ hi_timefreq.QuadPart;			hi_timeprevious = hi_timecurrent;			// set up the timer for the next update		}		else		{			lo_timecurrent	= clock();	 						timediff		= (float)(lo_timecurrent-lo_timeprevious) / CLOCKS_PER_SEC;					lo_timeprevious	= lo_timecurrent;		}			}};        


This is a time class i wrote for my game. It could be better but it does the job. Just call Init, and it will check if you have a hi-resolution windows timer available. If not, it will default to a lo-resolution windows timer. Make sure to call UpdateTime at the beginning of your main message loop to make sure you don't have old values in the class. Call GetTime whenever you need the amount of time since the last frame. If you are running at 50fps, then GetTime on average will return .020 . (50frames * .020 seconds = 1 second). With this class, i have 2-3 unused variables that take up space, but otherwise its a clean way of handling time that i like. Hope that helped

- Kevin "BaShildy" King
Game Programmer: DigiPen
www.mpogd.com

Edited by - BaShildy on January 19, 2002 4:10:08 AM

- Kevin "BaShildy" KingGame Programmer: DigiPenwww.mpogd.com

This topic is closed to new replies.

Advertisement