How to calculate FPS in OpenGL?
Asked Answered
C

5

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?

Cambium answered 11/4, 2011 at 21:30 Comment(1)
What is play(void)? Note that things are a bit more complicated since GPUs are unpredictable: #8780436Earthlight
K
8

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

Kindle answered 11/4, 2011 at 21:34 Comment(0)
B
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.

Boigie answered 11/4, 2011 at 21:37 Comment(0)
P
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;
    }
}
Petie answered 1/1, 2020 at 20:28 Comment(0)
A
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.

Amieeamiel answered 4/6, 2022 at 7:42 Comment(0)
B
0

I think you should just be able to divide the delta time by 1. Since the delta time is the time elapsed since the last frame or the time this frame took to render, dividing this by 1 should get how many times you can multiply that frame time to get one second. How many frames you get in one second is the framerate.

float lastTime = 0.0f;
void Application::Update() 
{ 
    // ...

    // Frame info
    float currentTime = glfwGetTime();
    float deltaTime = currentTime - lastTime;
    lastTime = currentTime;
    std::cout << "FPS: " << (1 / deltaTime) << "/" << deltaTime << std::endl;
{ 
Blouson answered 10/11, 2023 at 19:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.