c++connectionpingframe-rategettickcount

FPS game ping implementation


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)


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);