Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate FPS with sfml

This question is NOT related to the main loop in sfml. How do I calculate the actual framerate (for example, I have vsync enabled, but the main loop still runs at a high speed), so that it actually displays how fast the screen is updated (not the speed of the main loop). Measuring main loop speed is not important for me, but the actual window update speed is.

like image 450
Ruiqi Li Avatar asked Nov 21 '25 05:11

Ruiqi Li


1 Answers

SFML did not provide a way to retrieve your current framerate, neither backends like OpenGL does. Therefore, the only way is to monitor the main loop speed, as you suggested.

Also, window.setFrameLimit(60) or window.setVerticalSyncEnabled(true) or internal loop sleep on a 60Hz monitor causes the same effect in my SFML application, with the difference of V-Sync being more CPU and GPU expensive (due to their synchronization ways). Therefore you can rely on calculating FPS by using chrono for example in your main loop.

Make sure to wrap your draw calls by using time_point(s), into start and end points and calculating time elapsed with std::chrono::time_duration.

Example:

std::chrono::high_resolution_clock::time_point start;
std::chrono::high_resolution_clock::time_point end;
float fps;
while(window.isOpen()){
    // Perform some non-rendering logic there...

    // Performed. Now perform GPU stuff...
    start = std::chrono::high_resolution_clock::now();
    // window.draw, etc.
    end = std::chrono::high_resolution_clock::now();

     fps = (float)1e9/(float)std::chrono::duration_cast<std::chrono::nanoseconds>(end-start).count());
}
like image 132
Fabio Crispino Avatar answered Nov 23 '25 20:11

Fabio Crispino



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!