SFML community forums

Help => General => Topic started by: thatoneguy on February 24, 2014, 09:28:31 am

Title: [SOLVED]What's up with my FPS?
Post by: thatoneguy on February 24, 2014, 09:28:31 am
For the life of me I can't figure out what the issue is. I've done plenty of searches, but I think I might be missing something since I now have tunnel vision. I'm using SFML 2.1 for this.

Here's the minimal example with code from a search:
int main() {
    sf::RenderWindow Window(sf::VideoMode(100, 100, 32), "Test");
    Window.setFrameLimit(60);

    sf::Clock clock;
    float lastTime = 0;
    while (Window.isOpen()) {
        float currentTime = clock.restart().asSeconds();
        float fps = 1.f / (currentTime - lastTime);
        lastTime = currentTime;
        std::cout << fps << std::endl;
    }

    return 0;
}

Console:
-327.225
32258.1
23255.8
-1002
 
And so on with various numbers
Title: Re: What's up with my FPS?
Post by: eXpl0it3r on February 24, 2014, 09:43:57 am
You don't call window.display(), thus the frame limiter doesn't get active, instead you just have an infinite loop that goes as fast as it can (20k-40k fps apparently). ;)

There is a nice FPS class on the wiki, if you need some more inspiration.
Title: Re: What's up with my FPS?
Post by: thatoneguy on February 24, 2014, 10:05:26 am
You don't call window.display(), thus the frame limiter doesn't get active, instead you just have an infinite loop that goes as fast as it can (20k-40k fps apparently). ;)

There is a nice FPS class on the wiki, if you need some more inspiration.

Woops forgot about that. I call it right before calculating the FPS but still with the same results.
Title: Re: What's up with my FPS?
Post by: G. on February 24, 2014, 12:15:51 pm
FPS are the inverse of the (mean) frame length.
Your currentTime variable is essentially the time elapsed since lastFrame, so you don't need a lastTime variable to compute your FPS:
float currentTime = clock.restart().asSeconds();
float fps = 1.f / currentTime;

You can also compute your FPS with the time elapsed since the clock started, you would need a lastTime variable and you shouldn't restart the clock:
float currentTime = clock.asSeconds();
float fps = 1.f / (currentTime - lastTime);
lastTime = currentTime;
Title: Re: What's up with my FPS?
Post by: thatoneguy on February 24, 2014, 09:54:41 pm
FPS are the inverse of the (mean) frame length.
Your currentTime variable is essentially the time elapsed since lastFrame, so you don't need a lastTime variable to compute your FPS:
float currentTime = clock.restart().asSeconds();
float fps = 1.f / currentTime;

You can also compute your FPS with the time elapsed since the clock started, you would need a lastTime variable and you shouldn't restart the clock:
float currentTime = clock.asSeconds();
float fps = 1.f / (currentTime - lastTime);
lastTime = currentTime;

Oh I see. Thank you G.!

And thanks to eXpl0it3r his post led me to solve the problem in my up-scaled project. My "while" loop wasn't calling window.display() every loop therefore I had huge numbers for the FPS.