8
void CalculateFrameRate()
{    
    static float framesPerSecond    = 0.0f;       // This will store our fps
    static float lastTime   = 0.0f;       // This will hold the time from the last frame
    float currentTime = GetTickCount() * 0.001f;    
    ++framesPerSecond;
    if( currentTime - lastTime > 1.0f )
    {
        lastTime = currentTime;
        if(SHOW_FPS == 1) fprintf(stderr, "\nCurrent Frames Per Second: %d\n\n", (int)framesPerSecond);
        framesPerSecond = 0;
    }
}

Should I call this function in void play(void) or void display(void)?

Or it does not make any difference?

Ciro Santilli OurBigBook.com
  • 347,512
  • 102
  • 1,199
  • 985
Mzq
  • 1,796
  • 4
  • 30
  • 65

4 Answers4

7

You should put it in the display loop. Here's an article that explains some intricacies of game loops that you should read.

Philipp Zettl
  • 177
  • 1
  • 3
  • 17
DShook
  • 14,833
  • 9
  • 45
  • 55
1

If you have any kind of synchronization routine I suggest you place the call just after that, ie prior the big calculations. Otherwise the timing calculations can be shaky and give different values each loop...and a note, it's better to have a steady FPS than a fluctuating FPS just to maximize it. The fluctuation even so subtle makes the viewer/player aware that it's all a game and the immersion is lost.

epatel
  • 45,805
  • 17
  • 110
  • 144
0
void CalculateFrameRate()
{
    static float framesPerSecond = 0.0f;
    static int fps;
    static float lastTime = 0.0f;
    float currentTime = GetTickCount() * 0.001f;
    ++framesPerSecond;
    glPrint("Current Frames Per Second: %d\n\n", fps);
    if (currentTime - lastTime > 1.0f)
    {
        lastTime = currentTime;
        fps = (int)framesPerSecond;
        framesPerSecond = 0;
    }
}
0

Call this in every frame:

int _fpsCount = 0;
int fps = 0; // this will store the final fps for the last second

time_point<steady_clock> lastTime = steady_clock::now();

void CalculateFrameRate() {
    auto currentTime = steady_clock::now();

    const auto elapsedTime = duration_cast<nanoseconds>(currentTime - lastTime).count();
    ++_fpsCount;

    if (elapsedTime > 1000000000) {
        lastTime = currentTime;
        fps = _fpsCount;
        _fpsCount = 0;
        
        fmt::print("{} fps\n", fps); // print out fps in every second (or you can use it elsewhere)
    }
}

You can access the fps value from the fps variable, or you can print it only once a second as it's in the code currently.

Ambrus Tóth
  • 552
  • 6
  • 14