FPS counter (sfml tutorial-46)

  • antonio27
    Likes 0

    Problem Description

    I have two versions of  fps counter:

    1:

    #include "SFML/Graphics.hpp"
    #include <iostream>
     
    int main()
    {
    sf::RenderWindow window(sf::VideoMode(600, 600), "SFML WORK!");
     
    window.setFramerateLimit(15);
     
    sf::Clock clock;
     
    while (window.isOpen())
    {
    sf::Event event;
     
    while (window.pollEvent(event))
    {
    switch (event.type)
    {
    case sf::Event::Closed:
    window.close();
     
    break;
     
    }
    }
     
    sf::Time time = clock.getElapsedTime();
    std::cout << 1.0f / time.asSeconds() << std::endl;
     
    clock.restart().asSeconds();
     
    window.clear();
     
    window.display();
    }
    }

    2:

    #include <SFML/Graphics.hpp>
    #include <iostream>
     
    int main()
    {
    sf::RenderWindow window(sf::VideoMode(600, 600), "SFML WORK!");
     
    //window.setFramerateLimit(15);
     
    sf::Clock clock;
     
    sf::Time time2 = clock.getElapsedTime();
     
    while (window.isOpen())
    {
    sf::Event event;
     
    while (window.pollEvent(event))
    {
    switch (event.type)
    {
    case sf::Event::Closed:
    window.close();
     
    break;
     
    }
    }
     
    sf::Time time = clock.getElapsedTime();
    std::cout << 1.0f / (time - time2).asSeconds() << std::endl;
    time2 = time;
     
    window.clear();
     
    window.display();
    }
    }
     
     
    first version – when I block fps with window.setFramerateLimit(15);  – output is ~ 15.4 fps, but when I comment window.setFramerateLimit(15); – output is ~ 8000 fps
     
    second version – when I block fps with window.setFramerateLimit(15);  – output is ~ 15.1 fps, but when I comment window.setFramerateLimit(15); – output is ~ 1600 fps
     
    the same pc, the same ide.
    So why is so different output ( 8k vs 1.6k ) ??? where is wrong algorithm?
  • Sonar Systems admin
    Likes 0

    Try downloading fraps to compare and see which one is closer


    This reply has been verified.
  • antonio27
    Likes 0

    Variant with 2 variables is correct in unlimited fps. Variant with clock.restart is wrong in unlimited fps. But both of them are correct in limited fps.

    Verified with fraps smiley

  • Sonar Systems admin
    Likes 0

    Which one is closer though?

  • antonio27
    Likes 0

    Second with 2 variables is much closer

  • Sonar Systems admin
    Likes 0

    OK, let me look into this.

Login to reply