Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What's the usual way of controlling frame rate?

Tags:

c++

graphics

I'm new to graphics, so I don't know how people usually control the frame rate for rendering things. I mean, how can you set you application to render at, say, 30fps? There's probably lots of APIs that offers such thing, but I need to code it from scratch.

like image 697
hstefan Avatar asked Apr 01 '11 03:04

hstefan


People also ask

What determines your frame rate?

It is dictated by the speed of the video card, CPU, and memory in a computer. A user with a higher FPS is capable of performing better and reacting faster than someone with a slower FPS. Many games today have a command or keyboard shortcut key combination that displays the frames per second you are getting.

Does locking FPS improve performance?

Using an FPS cap is primarily helpful for weaker systems and can also positively affect high-end systems. An in-game FPS cap creates less input delay, while an FPS cap through external programs is more stable.


1 Answers

There are two "usual" ways of "controlling" framerate, and neither is quite that simple.

The first and more controlling of the two, and something that is usually optional, is VSync. This forces the video card to only push out a new frame when the monitor is done refreshing. Many monitors refresh at 60 Hz, so you tend to get 60 FPS.

This works quite well to cap framerate to monitor refresh rate, but when framerate drops below refresh, it's forced to the next multiple. Thus, as the framerate starts to drop a bit, you lose quite a bit of potential rendering time, because it's forced to 60, then 30, then 20, etc.

(a bit of info about vsync in DirectX and OpenGL)

The second method, commonly used (with vsync optionally added on) is not to limit framerate. Instead, adjust your code to handle differences. This is far more flexible in the long run and generally better coding, IMO, and much simpler than trying to force a particular FPS count.

Assuming you have a simple render loop, it starts out looking something like:

while ( gameloop )
{
    float framedelta = ( timeNow - timeLast )
    timeLast = timeNow;

    for each ( GameObject object in World )
    {
        object->Animate(framedelta);
        object->Move(speed * framedelta)
    }

    render();
}

You want to look at the time difference/time passed/delta, and work from there. Allow the framerate to scale based on hardware and settings (there are too many variations for you to predict or handle even half), and make your game work with that instead of controlling it. Much easier for you and more flexible and stable in practice.

like image 133
ssube Avatar answered Oct 10 '22 05:10

ssube