Search code examples
algorithmgame-developmentframe-rate

Game Development: How to limit FPS?


I'm writing a game, and I noticed the framerate is not stable. What basic logic do I need to limit the framerate of my game reliably? How do I calculate the sleep time?

I know how long it took to update the game one frame in microseconds and of course the frame rate (in frames per second (FPS)) I want to reach.


Solution

  • The number of microseconds per frame is 1000000 / frames_per_second. If you know that you've spent elapsed_microseconds calculating, then the time that you need to sleep is:

    (1000000 / frames_per_second) - elapsed_microseconds