Jump to content
  • Advertisement
Sign in to follow this  
Hardrive

Regulate Framerate SDL

This topic is 3703 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I've just gotten into using SDL. However, I can't figure out how to regulate the framerate to a constant rate. I've tried what Lazy Foo suggests at http://lazyfoo.net/SDL_tutorials/lesson14/index.php However, what seems to happen is that the computer will rapidly draw and reach the max frames per second and then wait for the next second to start again. Therefore, on moving things, there is a noticeable pause while the program waits for the second to get over, and the moving object will jump several pixels over. Does that make sense? Anyways, I was wondering if there's an easy way to regulate the framerate that doesn't introduce a lag like in the Lazy Foo example.

Share this post


Link to post
Share on other sites
Advertisement
const unsigned int FRAME_RATE = 1000 / 60;  //60 is the frame rate u want

int main(int argc, char* argv[])
{
//stuff
//..
//..

while(game->IsRunning())
{
unsigned int frameStart = SDL_GetTicks();

//Do stuff like render
//..
//..

unsigned int frameDuration = SDL_GetTicks() - frameStart;
if(frameDuration < FRAME_RATE)
SDL_Delay(FRAME_RATE - frameDuration);
}
}


Somewhat the same idea as Lazy Foo but it doesnt pause or whatever for me.

I personally think it's best to use both frame rate limiting and delta movement. Why? Because theres no point in going over 60-70 fps. The user will not be able to notice any difference, it will just be using up all your CPU so if you have anything running in the background it will run slower. You should also use delta movement in case the players computer cannot handle the 60-70 fps that you have it running at.

Share this post


Link to post
Share on other sites
Your OS actually regulated the framerate (I think) at 60 fps, so updating more often WILL cost CPU, but only for sending the SDL surface to the screen buffer...or where ever it sends it. My (untested) solution (that you could TRY) might be to simply not update the screen if the frame happened too fast and run through it again with frame-indy movement.

As ibebrett said, it's not the best idea (in any scenario I can think of) to regulate frame rates. Frame independent movement is where you base the movement of your object by how many ticks (milliseconds) have passed since last you moved it.

I don't suggest not doing frame regulation for now, but I wouldn't suggest getting too deep into your game before switching to frame independent. And if you can, doing it now wouldn't hurt either. It IS covered in a later chapter of Lazy Foo's.

One particularly big advantage for me is that if your game runs a little slow, the player can better predict where objects will be in the next frame by how fast it's moving, like how we know where things WILL be in real life. Regulating frame rates means that if you're slow, the objects on screen move slower than you'd think they should. The only advantage I can think of would be that if your game is REALLY slow, things don't move across the whole screen in one frame, but if your game causes that to happen, then your game is just too demanding for the target system. Optimize.

Share this post


Link to post
Share on other sites
Quote:
Original post by Hardrive
However, what seems to happen is that the computer will rapidly draw and reach the max frames per second and then wait for the next second to start again. Therefore, on moving things, there is a noticeable pause while the program waits for the second to get over, and the moving object will jump several pixels over. Does that make sense?

Anyways, I was wondering if there's an easy way to regulate the framerate that doesn't introduce a lag like in the Lazy Foo example.


That shouldn't be happening if it's being compiled directly from the example source.

It could be a low frame rate constant. I have the default set to 20FPS which is low.

Share this post


Link to post
Share on other sites
Quote:
Original post by Splinter of Chaos
Your OS actually regulated the framerate (I think) at 60 fps, so updating more often WILL cost CPU, but only for sending the SDL surface to the screen buffer...or where ever it sends it. My (untested) solution (that you could TRY) might be to simply not update the screen if the frame happened too fast and run through it again with frame-indy movement.

As ibebrett said, it's not the best idea (in any scenario I can think of) to regulate frame rates. Frame independent movement is where you base the movement of your object by how many ticks (milliseconds) have passed since last you moved it.

I don't suggest not doing frame regulation for now, but I wouldn't suggest getting too deep into your game before switching to frame independent. And if you can, doing it now wouldn't hurt either. It IS covered in a later chapter of Lazy Foo's.

One particularly big advantage for me is that if your game runs a little slow, the player can better predict where objects will be in the next frame by how fast it's moving, like how we know where things WILL be in real life. Regulating frame rates means that if you're slow, the objects on screen move slower than you'd think they should. The only advantage I can think of would be that if your game is REALLY slow, things don't move across the whole screen in one frame, but if your game causes that to happen, then your game is just too demanding for the target system. Optimize.

Thanks for the tip on that. I've gone through those tutorials, and I like the frame independent movement a lot.

Great work on the tutorials Lazy Foo :)

Oh, and I figured out my problem, my antivirus was scanning in the background. Sorry for wasting everybody's time.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!