# Frame rate cap

**URL:** <https://discourse.libsdl.org/t/frame-rate-cap/10645>\
**Category:** SDL Development\
**Created:** [May 7, 2004, 3:07am UTC](https://discourse.libsdl.org/t/frame-rate-cap/10645 "2004-05-07T03:07:01Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![Will\_McGugan](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/will_mcgugan/32/350_2.png) [@Will\_McGugan](https://discourse.libsdl.org/u/Will_McGugan)\
**Post date:** [May 7, 2004, 3:07am UTC](https://discourse.libsdl.org/t/frame-rate-cap/10645/1 "2004-05-07T03:07:01Z")

</div>

Hi,

I’d like to restrict the framerate of my game - when its running on the  
desktop - so that it uses minimal CPU. What is the best way of doing this in  
a cross-platform manner?

I’ve done it by timing the render time for the frame, then if it is quicker  
than the required frame time, sleep using SDL\_Delay. But SDL\_Delay may not  
be accurate enough on some platforms to use this way.

I’m also interested in knowing if SDL\_Delay( 0 ) has the same effect on all  
platforms as it does on Windows, ie. give up remaining time slice.

Thanks in advance…

Will McGugan

---

<div class="post-metadata">

**Author:** ![Kostas\_Kostiadis](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/k/848f3c/32.png) [@Kostas\_Kostiadis](https://discourse.libsdl.org/u/Kostas_Kostiadis)\
**Post date:** [May 7, 2004, 3:44am UTC](https://discourse.libsdl.org/t/frame-rate-cap/10645/2 "2004-05-07T03:44:21Z")

</div>

That’s the only way to do it in my knowledge…  
I assume by “render time” you mean your game loop  
time (i.e. rendering, physics, input handling, etc.)

The way its done in most games, is how you describe it below…  
e.g. for a 60fps game you’ll have something like

void foo::MainGameLoop()  
{  
// get current time and store it in startTime

// flip buffers

// handle input

// update physics

// draw scene

// get current time and store it in endTime

// sleep for a desired amount of time if  
// (endTime - startTime) is less than 1/60 (or  
// whatever framerate you like)  
}

As far as accuracy is concerned, I wouldn’t worry about it.  
I doubt your loop’s timing will need to be more accurate  
than what SDL\_Delay can offer.

cheers,  
Kos.\> ----- Original Message -----

> From: [sdl-admin@libsdl.org](mailto:sdl-admin@libsdl.org) [mailto:sdl-admin at [libsdl.org](http://libsdl.org)]On Behalf Of  
> Will McGugan  
> Sent: 07 May 2004 12:07  
> To: SDL mailing list  
> Subject: [SDL] Frame rate cap
> 
> Hi,
> 
> I’d like to restrict the framerate of my game - when its running on the  
> desktop - so that it uses minimal CPU. What is the best way of doing this in  
> a cross-platform manner?
> 
> I’ve done it by timing the render time for the frame, then if it is quicker  
> than the required frame time, sleep using SDL\_Delay. But SDL\_Delay may not  
> be accurate enough on some platforms to use this way.
> 
> I’m also interested in knowing if SDL\_Delay( 0 ) has the same effect on all  
> platforms as it does on Windows, ie. give up remaining time slice.
> 
> Thanks in advance…
> 
> Will McGugan
> 
> * * *
> 
> SDL mailing list  
> SDL at [libsdl.org](http://libsdl.org)  
> [http://www.libsdl.org/mailman/listinfo/sdl](http://www.libsdl.org/mailman/listinfo/sdl)

---

<div class="post-metadata">

**Author:** ![David\_Olofson](https://discourse.libsdl.org/user_avatar/discourse.libsdl.org/david_olofson/32/115_2.png) [@David\_Olofson](https://discourse.libsdl.org/u/David_Olofson)\
**Post date:** [May 7, 2004, 3:59am UTC](https://discourse.libsdl.org/t/frame-rate-cap/10645/3 "2004-05-07T03:59:43Z")

</div>

> Hi,
> 
> I’d like to restrict the framerate of my game - when its running on  
> the desktop - so that it uses minimal CPU. What is the best way of  
> doing this in a cross-platform manner?
> 
> I’ve done it by timing the render time for the frame, then if it is  
> quicker than the required frame time, sleep using SDL\_Delay. But  
> SDL\_Delay may not be accurate enough on some platforms to use this  
> way.

On most systems, the scheduler has a granularity of approximately 10  
ms, so you’re right; it’s not always accurate enough. What you can do  
is something like this:

```
while(time_left > 0)
	if(time_left > 10)
		SDL_Delay(10);

```

where “time\_left” is some expression involving SDL\_GetTicks(), that  
returns the number of ms left until it’s time to render the next  
frame.

> I’m also interested in knowing if SDL\_Delay( 0 ) has the same  
> effect on all platforms as it does on Windows, ie. give up  
> remaining time slice.

Nope, SDL\_Delay(0) does nothing on Linux and other Un\*x-like platforms

- or at least, that’s the way it was last time I looked. (Some 1.2.x  
version.)

I once suggested it should call sched\_yield() or something, so it does  
the same thing on all platforms, but for one reason or another,  
nothing was done.

//David Olofson - Programmer, Composer, Open Source Advocate

.- Audiality -----------------------------------------------.  
| Free/Open Source audio engine for games and multimedia. |  
| MIDI, modular synthesis, real time effects, scripting,… |  
`-----------------------------------\> [http://audiality.org](http://audiality.org) -’  
— [http://olofson.net](http://olofson.net) — [http://www.reologica.se](http://www.reologica.se) —On Friday 07 May 2004 13.06, Will McGugan wrote:

---

<div class="post-metadata">

**Author:** ![Neil\_Bradley](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/n/dfb087/32.png) [@Neil\_Bradley](https://discourse.libsdl.org/u/Neil_Bradley)\
**Post date:** [May 7, 2004, 9:47am UTC](https://discourse.libsdl.org/t/frame-rate-cap/10645/4 "2004-05-07T09:47:01Z")

</div>

> I’d like to restrict the framerate of my game - when its running on the  
> desktop - so that it uses minimal CPU. What is the best way of doing this in  
> a cross-platform manner?
> 
> I’ve done it by timing the render time for the frame, then if it is quicker  
> than the required frame time, sleep using SDL\_Delay. But SDL\_Delay may not  
> be accurate enough on some platforms to use this way.
> 
> I’m also interested in knowing if SDL\_Delay( 0 ) has the same effect on all  
> platforms as it does on Windows, ie. give up remaining time slice.

I wound up doing the following:

- Create a timer that runs @ the frame rate I want
- Create a semaphore for throttling on
- In the timer callback, do a SDL\_SemPost()
- In the foreground code, after a frame is rendered, do a SDL\_SemWait on  
that semaphore

That keeps CPU utilization at zero for that thread when there’s nothing  
else going on.

–\>Neil-------------------------------------------------------------------------------  
Neil Bradley “Your mistletoe is no match for my T.O.W. missile!”  
Synthcom Systems, Inc. - Santabot - Futurama  
ICQ #29402898
