Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: [SOLVED]What's up with my FPS?  (Read 2128 times)

0 Members and 1 Guest are viewing this topic.

thatoneguy

  • Newbie
  • *
  • Posts: 25
    • View Profile
[SOLVED]What's up with my FPS?
« 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
« Last Edit: February 24, 2014, 09:55:18 pm by thatoneguy »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: What's up with my FPS?
« Reply #1 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.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

thatoneguy

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: What's up with my FPS?
« Reply #2 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.
« Last Edit: February 24, 2014, 10:12:47 am by thatoneguy »

G.

  • Hero Member
  • *****
  • Posts: 1593
    • View Profile
Re: What's up with my FPS?
« Reply #3 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;

thatoneguy

  • Newbie
  • *
  • Posts: 25
    • View Profile
Re: What's up with my FPS?
« Reply #4 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.