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

Author Topic: Use setFramerateLimit or not?  (Read 1676 times)

0 Members and 1 Guest are viewing this topic.

Canvas

  • Full Member
  • ***
  • Posts: 107
    • View Profile
Use setFramerateLimit or not?
« on: June 15, 2012, 04:01:13 pm »
Hey guys just a quick question, in my code im telling my RenderWindow to have this set
screen.setFramerateLimit(60);
now when i run the program, i have a int going up by 1 every frame, it goes to 255 and then goes back, now if i run the program the window goes from black to white in 8 seconds...this is wrong, 255 / 60 = 4, so it should take the window 4 seconds to update, not 8, if i set it to 120 it works fine, but im alittle bit nervous running a game at 120 FPS, does anyone have any small piece of code i can use that uses the clock to update the window instead of the setFramerateLimit

Cheers

Canvas

I now have this

framtRate 60.0f/1000.0f
if(fps.getElapsedTime().asMilliseconds() > frameRate)
{
        fps.restart();
        screen.clear(sf::Color(currentState,currentState,currentState));
}
screen.display();
but the screen update is way to fast

its ok i had screen.setVerticalSyncEnabled(false); i now have screen.setVerticalSyncEnabled(true); :) post can be deleted (sorry about my mass of topics to be deleted)
« Last Edit: June 15, 2012, 04:35:40 pm by Laurent »

Mario

  • SFML Team
  • Hero Member
  • *****
  • Posts: 879
    • View Profile
Re: Use setFramerateLimit or not?
« Reply #1 on: June 15, 2012, 06:28:59 pm »
You're doing it wrong. :)

60 / 1000 = 0.06

That means you'd toggle the screen once every 60 milliseconds; that's 16.67 FpS!

If you'd like to do it that way (which I wouldn't do, so I guess it's just some testing), then you have to change your calculation:

1000 / 60 = 16.67

This will cause the contents of the if() block to be executed once every 16.67 ms; i.e. (up to) 60 times per second.

Edit:
Just wanted to mention this: DO NOT rely on the fixed framerate through VSync! It might be forced off through drivers and the OS might modify it as well (e.g. Windows 7 in windowed mode will limit framerate to 60, 30 or 15 fps depending on performance).
« Last Edit: June 15, 2012, 06:30:54 pm by Mario »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Use setFramerateLimit or not?
« Reply #2 on: June 16, 2012, 01:34:35 am »
that uses the clock to update the window instead of the setFramerateLimit

As was meantioned before you should not rely on VSync but since you don't want to max out everyones CPU you really should use setFramerateLimit. It uses sf::Sleep to wait between frames and not maxing out the CPU (not busy-waiting). So you can create your own code to only draw every certain timestep but it will then just loop as fast as possible on your CPU when you're doing nothing.
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/