Author Topic: Framerate regulation question  (Read 5965 times)

0 Members and 1 Guest are viewing this topic.

Bizarreofnature

• Newbie
• Posts: 48
Framerate regulation question
« on: January 31, 2018, 04:39:15 pm »
Hello!

I am really clueless when it comes to framerate regulation.
Im aware there is a nice function for this in SFML... however:

I want to do it the hard way and make it all myself.
I dont want sourcecode, im searching for someone who can explain to me what to do in words.

My goal is to capping the framerate of my game by 60 fps.

I have no problem when it comes to measuring time, the problem i have is i simply dont know how to cap framerate.

EDIT: If it helps to explain, of course id like to see sourcecode.

MfG Bizarreofnature!
« Last Edit: January 31, 2018, 04:49:21 pm by Bizarreofnature »

Bizarreofnature

• Newbie
• Posts: 48
Re: Framerate regulation question
« Reply #1 on: January 31, 2018, 05:10:05 pm »
If i understood it right i have to make a variable that counts to 60 per sec and if 60 is reached, the game no longer gets updated. Is this it? Is it that easy and im just stupid?

Zinidane

• Newbie
• Posts: 4
Re: Framerate regulation question
« Reply #2 on: January 31, 2018, 06:27:56 pm »
One way to do it is to calculate the time it takes a frame to render, and wait the remaining time until the next frame, in order to have let's say 60 of them in a second (60 fps = 16.67 ms per frame) . So in order to do that  you want to :

1. Get the clock time in a variable (frameStartTime = some clock function)
3. check how much time passed (frameTime = clock function - frameStartTime)
4. It will probably be less than 16.67 ms (for 60fps) so you want to wait the difference (Sleep(16.67 - frameTime))
5.Rinse and repeat

Now that's only the basic idea behind it, it's not perfect but i hope it helps you.

EDIT : btw this method does not really cap the frame rate to 60 but rather enforce a frame rate of 60.
« Last Edit: January 31, 2018, 06:29:32 pm by Zinidane »

Bizarreofnature

• Newbie
• Posts: 48
Re: Framerate regulation question
« Reply #3 on: January 31, 2018, 06:49:48 pm »
Thanks mate, this helps a lot

Im curious.. are there other ways to realize this aswell?

Bizarreofnature

• Newbie
• Posts: 48
Re: Framerate regulation question
« Reply #4 on: January 31, 2018, 07:05:56 pm »
Btw Zinidane, what time i use to update my game? 16.67 or the frametime?

Zinidane

• Newbie
• Posts: 4
Re: Framerate regulation question
« Reply #5 on: January 31, 2018, 08:35:41 pm »
I'm not sure i understand your question. I'll redirect you to the article that helped me understand, it really is a good read on the subject. It starts from what I told you and there's more after.

http://gameprogrammingpatterns.com/game-loop.html

Now I realize that the frameTime variable in my example is poorly named. It should have been timeElapsed because it represents how much time the game loop took to execute and render.

16.67 ms is the time the frame should take to have 60 of them in a second. (16.67 * 60 = 1000.2 ms)
So if your game loop took 4 ms, you should sleep 16.67 - 4 = 12.67 ms until the next frame, thus enforcing 60 fps.

EDIT : You will realize with more reading on the subject that the sleep method is not exact so it's pretty hard to have EXACTLY 60fps. With this method you'll have around 59 - 61 fps. close enough
« Last Edit: January 31, 2018, 08:37:41 pm by Zinidane »

Bizarreofnature

• Newbie
• Posts: 48
Re: Framerate regulation question
« Reply #6 on: February 01, 2018, 07:00:48 pm »
Thanks mate. I never believed it was so easy actually.