SFML community forums

Help => Window => Topic started by: Ninjaboi on September 22, 2011, 09:31:39 pm

Title: Limit frame rate
Post by: Ninjaboi on September 22, 2011, 09:31:39 pm
Probably a question that's answered somewhere else, but I've been trying to limit the frame rate in an application, and I think I'm having a meltdown with simple math  :oops: .

Code: [Select]
framerate = Window.GetFrameDuration();

if( framerate < (unsigned)( 1 / 60 ) )
{

Window.Wait( ((unsigned)( 1 / 60 ) - framerate) );

}


I'm wanting 60 fps, though I'm not even sure if that is correct to get it. If it is, would changing the value dividing into 1 ( 60 ) to say 30 make the fps 30? I did this fine in SDL ( using a very similar method ) though I'm still new working with SFML. I already saw the tutorial page that talks about handling time.

http://www.sfml-dev.org/tutorials/1.3/window-time.php

Which gave me a way to get the time between frames.

EDIT: I already know about V-Sync, as well as setting the frame limit for a window via SetFramerateLimit(). I'd rather not make the application rely on the refresh rate of the monitor, and I don't want to simply limit what the window displays  8) .

Hope that makes sense  :P .
Title: Limit frame rate
Post by: Nexus on September 22, 2011, 09:38:23 pm
This topic has been discussed a lot in this forum, you'll certainly find many posts about it. Words to search for are SetFramerateLimit, Clock and Sleep.

Anyway, sf::Window::SetFramerateLimit() should be exactly what you need. It automatically waits as long as necessary, limiting the frame rate of the whole thread.
Title: Limit frame rate
Post by: Ninjaboi on September 22, 2011, 10:07:08 pm
Thanks for telling me that Nexus, I was under the impression that sf::Window::SetFramerateLimit() only limited the displaying on the window itself. Now I know  :D . I was almost at the point where I was going to use OS specific code for time management just for framerate limiting :twisted: .