2

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)

genpfault
  • 51,148
  • 11
  • 85
  • 139
Dagob
  • 695
  • 1
  • 12
  • 23

2 Answers2

6

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);
kossmoboleat
  • 1,841
  • 1
  • 19
  • 36
  • Note that QueryPerformanceFrequency only needs to be called once. It does not change throughout the program, but might be different on different computers. – Macke Feb 07 '12 at 14:59
  • +1 for QueryPerformanceCounter if you are in Windows. Will definitely prove useful. – Lefteris Feb 07 '12 at 15:02
0

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.

unwind
  • 391,730
  • 64
  • 469
  • 606
  • Its a windows game what i'm making. I already have a data stream of information to the client. So I think it is beather to add this to that. That way it will take less bandwidth. – Dagob Feb 07 '12 at 14:53