So I am using SDL2.0 for making a Game Engine. Where I am having a problem is using SDL_Delay().

SDL_Delay's minimum delay time is 15ms, I've tried everything, and my FPS is Stuck at 64 because of this. I have seen another post about this but it does not seem to answer the question with SDL_Delay.

I can turn on VSYNC, and I Get a fixed 60FPS, Please explain what the SDL Library is doing that I cannot implement Myself?

This is very weird, I dont think I need to post my code for this, the code is not wrong, Its the SDL_Delay(), wether it be SDL_Delay(1) or SDL_Delay(15) , it will always delay for 15ms, does anyone know a way of fixing this so I can still use SDL_Delay.

I have seen on SDL Documentation they say this is expected, because of OS Scheduling.

Then I read OS Scheduling does this to save power consumption... I have High Performance turned on my computer, not Power Saver.

Does anyone know a workaround for this? I am very hardheaded and do not want to use VSYNC.

Or I might just have to. LOL Thanks in advance!!

Edit: https://wiki.libsdl.org/SDL_Delay

  • 1
    \$\begingroup\$ You can't use SDL_Delay to control framerate, because delaying for a fixed amount of time doesn't take account of how long your frames otherwise last (and that time may be variable). \$\endgroup\$ – Maximus Minimus Mar 30 '15 at 7:43

You can't reliably control the FPS with functions like SDL_Delay, they call the operating sleep function, which tells the operating system "Please don't give me any CPU time for at least N miliseconds", the operating system is then free to decide:

  1. At what granularity (i.e., as you said you specify 1ms and it waits for 15ms, it has a granularity of 15ms) it controls sleeping.
  2. Whether it wants to run other programs before bringing your program back up.

This combines to making SDL_Delay not useful for any reliable timing.

What VSYNC is doing is something different, with VSYNC enabled, when you send a new frame to the monitor it will wait for a Vertical SYNC signal to return control to the program so it can start with the next frame.

What you want to do, if you don't want to use VSYNC, is something more like this:


int lastTickTime = now();
int lastFrameTime = now();
while ( gameIsRunning ) {
    while ( now() > lastTickTime + TIME_PER_TICK ) {
        //Not included, logic to detect when we're *way* behind so we can just drop some ticks
        lastTickTime += TIME_PER_TICK;
    runFrame( now() - lastFrameTime );
    lastFrameTime = now();

This separates the rendering (runFrame) from when physics and such is updated, this allows you to have smooth graphical effects while your game logic runs at a fixed delta time.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.