I want my game run at 60 fps, but I don't know where to start, I have the simple loop:


How do I lock the frame rate to 60 FPS?

  • \$\begingroup\$ I would just like to know, why would you need to lock it to 60 FPS? Because I have never ever ever heard of anyone wanting to clip the FPS. \$\endgroup\$ Commented Mar 11, 2012 at 22:28
  • \$\begingroup\$ at 4000fps, some (my included) video card start 'screaming'. Guess it's a sort of coil whining :-) but I defenitely prefer 200fps. Sometimes a too high fps trashes delta time too (if you use an integer with milliseconds you don't want to get close to 500fps). \$\endgroup\$
    – Valmond
    Commented Mar 12, 2012 at 10:41
  • \$\begingroup\$ That's an odd problem, my game engine runs at about 1500 FPS with a basic level and a few entities and I have none of those problems. \$\endgroup\$ Commented Mar 12, 2012 at 11:51
  • \$\begingroup\$ Here is the code that made my card scream :-) www.mindoki.com/download/SimpleTerrain.rar Another card screamed here : irrlicht.sourceforge.net/forum/… \$\endgroup\$
    – Valmond
    Commented Mar 12, 2012 at 12:34

5 Answers 5


Sleep is a very bad idea for the reasons already pointed out - the only justifiable case to use it is for saving battery on mobile devices.

If you want to lock to 60 fps one way would be to measure the time that's elapsed since the last frame ran; if that equals or exceeds (1.0 / 60.0) seconds then run a frame. Of course you'll need a high resolution timer to get this right, and of course it's a busy-wait loop that will chew up CPU time, but it works.

(You might even find some useful work to do during that busy-wait period, such as updating dynamic resources or whatever).

Another way is to use vsync. Most monitors nowadays will run at 60hz, and those that don't are extremely likely to support it, so just enable vsync, set your refresh rate if needed, and run as normal.

  • \$\begingroup\$ I proposed Sleep() because it will give a chance to have other things happened while you wait. For example you could have another critical thread in the game that absolutely need to absolutely need to run at same time (eg: getting network packets, mixing sound or reading player input). it will still works, even on single core architecture. It is also the way it is done on some professional game engines like quake 4 (check here : github.com/TTimo/doom3.gpl/blob/master/neo/framework/…). \$\endgroup\$
    – tigrou
    Commented Mar 11, 2012 at 16:48
  • \$\begingroup\$ Sleep (0) is sufficient to yield the CPU to other threads in the same process, and won't have the same precision issues that a normal Sleep does. The Doom 3/Quake 4 code uses Sleep in a multithreaded environment and explicitly where there is no work for the engine to do otherwise, which is quite different to recommending it as a general case solution for locking FPS (it's actually the spin loop that locks the ticrate in that example, not the Sleep; the Sleep just yields CPU while the spin loop is running). \$\endgroup\$ Commented Mar 12, 2012 at 13:35

There are a better solution to get 60fps: set vertical syncronisation (vsync) to true when you create Irrlicht device

//Here is the easiest common function to create irrlicht device
irr::createDevice   (   
    video::E_DRIVER_TYPE    deviceType = video::EDT_SOFTWARE,
    const core::dimension2d< u32 > &    windowSize = (core::dimension2d< u32 >(640, 480)),
    u32     bits = 16,
    bool    fullscreen = false,
    bool    stencilbuffer = false,
    bool    vsync = false,
    IEventReceiver *    receiver = 0 

For example, use this function:

IrrlichtDevice* device = createDevice(EDT_OPENGL, dimension2du(800, 600), 16, false, false, true);

Do you want to synchronize on vsync ? There are differents solutions depending on your platform. Search vsync on google. On directx, you can limit on fullscreen app with D3DPRESENT_INTERVAL_ONE or use WaitForVerticalBlank on windowed app.

Look this article


Depends what you mean by saying "you want to lock the game at 60fps".

If you want to limit the game rendering at max 60fps its very simple :

Each frame, just check if the game is running to fast, if so just wait :

//to call each frame
while( true )
    int elapsed_time = getcurrenttime() - oldtime;
    if(elapsed_time >= minimum_elapsed_time)
    sleep(1); //yield cpu to other game thread or processes...

Please note that if vertical sync is enabled, rendering will already be limited to 60fps.

But if you want to have your game logic locked at 60fps, no more no less, that's different. Then you have to separate code you want to run at 60fps from rendering code and make necessary changes to make sure that code is called at same fixed time intervals. It is called "fixed timestep". Make a search on gamedev or google. There is a lot of info on this.

EDIT : as Roy T. pointed out, code is not guaranteed to works because sleep(1); may wait more than 1ms. calling timeBeginPeriod(1); at game start (and timeEndPeriod(1); at end) should fix the problem (but have some consequences). I choose to call sleep instead of busy-wait loop to give other threads and processes a chance to run while game is waiting...

  • \$\begingroup\$ You can't use sleep to wait for very small periods because after the thread is yielded you're not entirely sure when the thread will be activated again. All you know is that it's not going to be sooner than 1ms this way. If you want your game to run at exactly 60fps you will have to use a busy loop. \$\endgroup\$
    – Roy T.
    Commented Mar 11, 2012 at 15:49
  • \$\begingroup\$ please can you provide me a while example? \$\endgroup\$
    – fracedo
    Commented Mar 11, 2012 at 15:56
  • \$\begingroup\$ gamedev.stackexchange.com/questions/18898/… especially see the link to Gaffer on Games there. \$\endgroup\$
    – Roy T.
    Commented Mar 11, 2012 at 16:24
  • \$\begingroup\$ Yes you're right, it will wait a little more than 1ms in most cases (typically 10ms on win32 systems?). So basically you will accumulate some "delay" after a few frames. I have updated code to make sure "delay" will be absorbed over the time. Running the game at a perfect 60fps (that means 16.6667 ms between each frames) is impossible... \$\endgroup\$
    – tigrou
    Commented Mar 11, 2012 at 16:34
  • \$\begingroup\$ this is what i have so far, the fps locks in 59. irr::ITimer* timer = device->getTimer(); irr::u32 timeThisFrame = timer->getTime(); timeThisFrame = timer->getTime(); while( (timer->getTime() - timeThisFrame) <= 16 ) { timer->tick(); }; This is the best solution? \$\endgroup\$
    – fracedo
    Commented Mar 11, 2012 at 16:46

If you are measuring time you also really need to be using QueryPerformanceCounter as timeGetTime jumps in large increments.

There is a fairly involved blog post on the subject of windows timing:


Also, if there are no other threads needing to work Sleep(0) will return immediately, you need to use SwitchToThread, and if that 'fails' call Sleep with a non-zero amount instead to actually go to sleep.

That said, I would still recommend using the vsync features, as that can be turned into a kernel wait on an interrupt behind the scenes and also context switch out properly while waiting, much like the sleep. Also, sleeping is a bit odd of a thing to do as you dont actually know when the vsyncs really occur, even with vsync enabled, so it is hard to sleep a proper amount and not miss the vsync on the next frame in a lot of cases (esp if youre CPU overhead is pushing 15+ milliseconds)


You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .