Question

I'm working on a FPS (first person shooter) game at the moment, I want to show player's ping in the game (Connection delay). But what is the best method to do this? First I thought to use GetTickCount64, but Get Tick Count is not precise:

"The resolution of the GetTickCount64 function is limited to the resolution of the system timer, which is typically in the range of 10 milliseconds to 16 milliseconds."

I had an idea to use time.h to look how many Tick Counts there are in 1 second. But I think that that is not the best solution.

Can someone help me with this?

Edit: I'm making a Windows game. ( Thanks unwind and Lefteris for mentioning that I forgot to note this down)

Was it helpful?

Solution

If you are looking for a Windows solution try QueryPerformanceCounter.

Following code was posted there by BobJoy1. You will have to divide the difference between two calls by the CPU frequency like so:

LARGE_INTEGER start;
::QueryPerformanceCounter(&start);
// do something
LARGE_INTEGER stop;
::QueryPerformanceCounter(&stop);

LARGE_INTEGER proc_freq;
::QueryPerformanceFrequency(&proc_freq);
double frequency = proc_freq.QuadPart;
double seconds_elapsed = ((stop.QuadPart - start.QuadPart) / frequency);

OTHER TIPS

I think the best approach is to simply perform an ICMP echo against the server host. That way, the ping you display will match what players can measure for themselves, which is nice. Also, you only should expect millisecond resolution for something like ping, in my experience. I know Linux does it better, but you didn't mention a platform and for games Windows is pretty popular.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top