Archived

This topic is now archived and is closed to further replies.

How to get "the change in time"??

This topic is 5588 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
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...

Share this post


Link to post
Share on other sites
So timeGetTime() is the function most people use when they program games??? Are there any other functions that are more acturate???

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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;
}
}

Share this post


Link to post
Share on other sites