Limit frame rate to 60 fps on Linux

Started by
11 comments, last by owl 15 years, 8 months ago
How can I limit the frame rate? I've searched a lot on internet but couldn't find any way to implement this. I did find how to show the frame rate, but nothing related to limit it. I also would like to have vertical sync activated (I'm not using SDL cause I'm already using QT), but couldn't find any good info for a Linux user.
Advertisement
I'm surprised you didn't find anything, this is a common problem many people face and there are many ways to face this. Here is just one good link from gamedev I found similar to yours:
http://www.gamedev.net/community/forums/topic.asp?topic_id=499665&whichpage=1&#3259539
What i found worked well was not limiting the frames drawn, let it draw as fast as possible, but only update the information only 60 times a second.
So you set up a timer that will call say,
UpdateInformation(); every 1/60th of a second, but just let your program draw as fast as possible. That way even though you are drawing at like maybe 200fps, information is only updating at the right pace.
Quote:Original post by Deftoned
What i found worked well was not limiting the frames drawn, let it draw as fast as possible, but only update the information only 60 times a second.
So you set up a timer that will call say,
UpdateInformation(); every 1/60th of a second, but just let your program draw as fast as possible. That way even though you are drawing at like maybe 200fps, information is only updating at the right pace.


Would it be better by setting up two threads, one for logic and the other for graphics where the graphics thread draw with vsync enabled?
Last time I checked there was no (at least not an easy one) way to enable/disable vsinc from your app. That kind of functionality should be implemented in the xorg library or wrapped by SDL, but it isn't. In fact, there is no way to chose the refresh rate of a given resolution. If you set 640x480 with SDL it will default to 60hz no matter if your monitor can do 120hz.

To limit your framerate just flip the buffers once every 1/60 of a second.

if ((current_millisecs - last_milisecs_drawn) >= 1000/60)
DrawStuff();

all your logic calculations should always be done before that period of time or your framerate will drop.

I would personally make all graphics time-based, so no matter what's the speed of the computer or the refresh, the graphics will be always updated timely.
[size="2"]I like the Walrus best.
Quote:Original post by ma_hty
Quote:Original post by Deftoned
What i found worked well was not limiting the frames drawn, let it draw as fast as possible, but only update the information only 60 times a second.
So you set up a timer that will call say,
UpdateInformation(); every 1/60th of a second, but just let your program draw as fast as possible. That way even though you are drawing at like maybe 200fps, information is only updating at the right pace.


Would it be better by setting up two threads, one for logic and the other for graphics where the graphics thread draw with vsync enabled?


Questions about multithreading frequently appear in the Game Programming forum, so try and have a look there. But otherwise, you can PM me, and I will tell you about the 2 common methods (you are partly describing one of them), so that we will not steal focus from the actual topic of this thread.
Quote:Original post by owl
Last time I checked there was no (at least not an easy one) way to enable/disable vsinc from your app. That kind of functionality should be implemented in the xorg library or wrapped by SDL, but it isn't. In fact, there is no way to chose the refresh rate of a given resolution. If you set 640x480 with SDL it will default to 60hz no matter if your monitor can do 120hz.

It is easy if you use the xrandr libs and glx directly. I'm quite sure that SDL exposes this functionality.

[OpenTK: C# OpenGL 4.4, OpenGL ES 3.0 and OpenAL 1.1. Now with Linux/KMS support!]

Quote:Original post by Fiddler
Quote:Original post by owl
Last time I checked there was no (at least not an easy one) way to enable/disable vsinc from your app. That kind of functionality should be implemented in the xorg library or wrapped by SDL, but it isn't. In fact, there is no way to chose the refresh rate of a given resolution. If you set 640x480 with SDL it will default to 60hz no matter if your monitor can do 120hz.

It is easy if you use the xrandr libs and glx directly. I'm quite sure that SDL exposes this functionality.


Where can I find this xrandr lib. owl?
Quote:Original post by Fiddler
It is easy if you use the xrandr libs and glx directly. I'm quite sure that SDL exposes this functionality.
if you are using SDL, you can enable vsync by calling SDL_GL_SetAttribute(SDL_GL_SWAP_CONTROL, 1), but I wont guarantee it works on all linux systems.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

If you aren't using SDL or some other wrapper library, you have to use one of the GLX extensions GLX_SGI_swap_control or GLX_MESA_swap_control.

It also obviously depends on your driver and the driver settings, which you can configure using DriConf (your distro should have a package). You can override those settings using environment variables, vblank_mode in the case of vblank settings.

Side note: The thread title is misleading, since we're talking about syncing to vblank, which may be at something else than 60Hz. If you really want to limit yourself to 60fps, you should just use an appropriate sleeping primitive (e.g. call to select).

Edit: Obviously, DriConf only works with the open source drivers. I'd expect the proprietary drivers have their own configuration tools.
Widelands - laid back, free software strategy

This topic is closed to new replies.

Advertisement