SDL_Delay Question

Started by
0 comments, last by Ashaman73 13 years, 11 months ago
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.
Advertisement
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);  }}

This topic is closed to new replies.

Advertisement