How to get "the change in time"??

Started by
4 comments, last by Yau 21 years, 7 months ago
At the moment I''m workin'' on a cool mecha simulation game. I would like to get the change in time since my last animation frame, what is the best function to do this?? At them moment I''m using the function timeGetTime(), which retrieves number of clock cycles that have pasted since the last time the function was called. I''m not sure if this is the best way, is there a much more acturate method??
:)
Advertisement
Yes, timeGetTime() is a very good function to use for simulating time. It doesn''t return the time since it was last called, it returns the number of msec that have elapsed since Windows started.

Anyway, have a DWORD variable keep track of the last time you changed frames. Do soemthing like this:

DWORD time = timeGetTime();

if ( time - mechLastFrameChange() > 100 )
{
mechChangeFrame();
mechSetLastFrameChange( time );
}

You can fiddle with the number in the if statement (100) to whatever looks good.

That should do it...
So timeGetTime() is the function most people use when they program games??? Are there any other functions that are more acturate???

:)
I think the method most games use is the QueryPerformanceCounter() functions, they''re much more accurate. This will only work however if your processor has a performance counter, so to support all computers you should have a fallback method.

Another thing to note is that apparently you sometimes get the performance counter skipping forward, I''ve never experienced this but I know that there is a workaround method by using to different timers, this is documented somewhere on the web by Microsoft by I can''t remember where.
You could use QueryPerformanceFrequency and QueryPerformanceCounter for a high-resolution timer.

  class HighResolutionTimerException{public:   HighResolutionTimerException()    : mdwLastError(::GetLastError())   {   }   DWORD GetLastError() const { return mdwLastError; }private:   DWORD mdwLastError;};double GetHighResolutionFrequency(){   LARGE_INTEGER  liPerformanceFrequency;   if (!QueryPerformanceFrequency(&liPerformanceFrequency))   {      throw HighResolutionTimerException();   }   else   {      return (double) liPerformanceFrequency.QuadPart;   }}double GetHighResolutionTime(){   static double  dPerformancePeriod = 1 / GetHighResolutionFrequency();   LARGE_INTEGER  liPerformanceCount;   if (!QueryPerformanceCounter(&liPerformanceCount))   {      throw HighResolutionTimerException();   }   else   {      return (double) liPerformanceCount.QuadPart * dPerformancePeriod;   }}  
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]
Microsoft Knowledge Base Article - Q274323 - PRB: Performance Counter Value May Unexpectedly Leap Forward
Arguing on the internet is like running in the Special Olympics: Even if you win, you're still retarded.[How To Ask Questions|STL Programmer's Guide|Bjarne FAQ|C++ FAQ Lite|C++ Reference|MSDN]

This topic is closed to new replies.

Advertisement