Skip to main content
gamedev.net
🔒 Locked

SDL Framerate

Started by SigmaX May 26, 2006 at 11:14 AM 3 replies 800+ views
Original Post
SigmaX
SigmaX
Hi everyone. I feel stupid asking this because I know Lazy foo has a tutorial on this (I can't access his site from work for some reason). How do you control the framerate of a program. Currently my applications are running wildly out of control and eating up way too much of the CPU. Thanks!
-)------ Ed
Derakon
Derakon
I believe that SDL_gfx has a built-in mechanism to do frame limiting; you can write one yourself as well. Basically you want to do something like this:

while(gameIsRunning) {  startTime = getTimeInMilliseconds(); runOneFrame(); endTime = getTimeInMilliseconds(); sleepInMilliseconds(endTime - startTime / FramesPerSecond * 1000);}


Sleeping is the key thing here; when your program sleeps, it cedes control to the OS, which can then run other programs. This will keep your program from eating all of the CPU.
Jetblade: an open-source 2D platforming game in the style of Metroid and Castlevania, with procedurally-generated levels
Ravuya
Ravuya
You can put SDL_Delay(0) at the end of your main loop, which should allow the OS time to manage whatever it wants without throttling. This is particularly useful on laptops and computers featuring CPU-controlled fans.
Rob Loach
Rob Loach
The SDL_gfx library features a framerate manager which I really recommend you take a look at.
Rob Loach [Website] [Projects] [
SigmaX
SigmaX
Thanks for your responces. I was able to get the frame rate controled like Derakon suggested. It has halved my CPU usage which, is awsome. I just need to do a little more optimization and I should be all set! Thanks again.
-)------ Ed

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.