Problems getting timing right

Started by
5 comments, last by DividedByZero 12 years, 1 month ago
Hi guys,

I have made a couter that is part of a class, that I am trying to get right.

So, far I am able to accurately assess the frames per second in a scene. But, I am having problems calculating the time since the last frame, so that movement and animations run at the same speed on any computer.

But I am finding, different PC's are giving different movement speeds although I want them to be the same.

int Engine::counterFramerate()
{
// get curent PC frequecy (incase of overclocking during test)
LARGE_INTEGER li;
start=current;
QueryPerformanceCounter(&li);
current=(double(li.QuadPart))/1000;
nCounter++;
total=total+current-start;
tslf=(current-start);
if((total)>=1000)
{
nFramesPerSecond=nCounter;
nCounter=0;
QueryPerformanceCounter(&li);
PCCounter=double(li.QuadPart);
start=PCCounter;
total=0;
}
return nFramesPerSecond;
}

double Engine::counterTimeSinceLastFrame()
{
return tslf;
}


counterFramerate() returns FPS and counterTimeSinceLastFrame() should return the time lapsed since the last frame but doesn't seem to be doing so correctly.

Can anyone see where I might have gone wrong?

Thanks in advance smile.png
Advertisement
You need to find the frequency with queryperformancefrequency and divide the results you get from queryperformancecounter by the frequency.
I count the time needed for rendering 10 frames and use fixed steps for physics. With 200-700 physical steps per second, you do not need to interpolate from fixed physics to dynamic framerate. You can use vertical synchronization if rendering is faster than the monitor.
Thanks [color="#284b72"]turch, I'll give that a go. I must admit I missed that all together.

[color="#284b72"]Dawoodoz, yes the rendering is somewhat faster than the monitor. I am currently getting ~8000 FPS.
I am now dividing by [size="2"]QueryPerformanceFrequency(), which is now qiving me more reasonable numbers to play with.

[size="2"]Although, the result between frames is often the same. Does this mean the framerate is too fast for QueryPerformanceCounter() to see a difference?

[size="2"]I thought this was what [size="2"]QueryPerformanceCounter() was for, to give a higher degree of accuracy than GetTickCount().
The accuracy of the timer can be bad for short durations because the the CPU do many things at the same time and in the wrong order to get more speed. Sometimes, I get a negative duration when trying to measure how long it takes to do something.
I have gone back to the good old GetTickCount() and locking my framerate to 100 FPS.

I have since read lots of nasty things about the performance counters. Including docs published by Microsoft about its downfalls.

Now my app seems to run pretty close on systems with different specs.

This topic is closed to new replies.

Advertisement