Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to calculate FPS in OpenGL?

Tags:

c

opengl

playback

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?

like image 260
Mzq Avatar asked Apr 11 '11 21:04

Mzq


2 Answers

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

like image 107
DShook Avatar answered Nov 10 '22 19:11

DShook


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.

like image 44
epatel Avatar answered Nov 10 '22 21:11

epatel