Sign in to follow this  
X Abstract X

SDL_Delay Question

Recommended Posts

Sup, I'm working on a game using SDL and OpenGL. I have my timing all sorted out to run the game logic at a fixed rate and render as fast as possible. With VSycnc enabled my FPS would cap to 60. That worked great but then I disabled VSync for testing purposes. The FPS would reach really high numbers obviously, but then when I would try to exit the window it would be too hung up to close. So, I tried adding SDL_Delay(x) at the end of my main loop and through trial and error I found out I needed to delay for atleast 2 ms to be able to have control over closing the application. My question is, what should I do? I'm not sure using SDL_Delay is a good idea. Should I cap the rendering speed myself? Even capping the rendering speed could lead to problems if a slower computer is struggling to render at the capped rate I'm guessing.

Share this post


Link to post
Share on other sites
Do you want a fix rate of 60fps even with VSync off ?

In this case just measure the time for your last frame and wait for the rest of the frame until atleast 1/60 s has been passed.

Pseudo code

while(...mainloop)
{
Time startTime = Clock::getTimeInMs();

.. handle events ..

.. run game logic, render scene ...

Time endTime = Clock::getTimeInMs();
Time frameDuration = endTime - startTime;

// compare to 60fps = 1/60 s = 1000/60 ms
if( frameDuration< (1000/60))
{
// wait for the rest of 1/60s
SDL_delay( (1000/60) - frameDuration);
}
}



Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this