Search code examples
c++sdldelay

SDL_Delay doesn't delay before each action


I have written program which has a delay between each drawn circle, but when i put for example SDL_Delay(2) everything is black for like 5 seconds and then i see everything already drawn, but i need to see everything from begining so that it would seem like an animation.

Here is my code:

 while (t<tk){
  l.a = l.a + (l.b - l.a) * t;
  a=l.a;
  Circle cir1(a,o);
  draw_circle(cir1, canvas);
  SDL_Delay(2);
  t=t+0.001;
}

Solution

  • The thing is after each draw_circle you actually have to update the screen. Draw functions in almost all graphical engines write to a buffer and don't update the screen until you tell them to!

    I don't know how this works with SDL that doesn't use OpenGL, but with double buffer OpenGL windows you should write SDL_GL_SwapBuffers() and then after it, start clearing the screen etc as if you are drawing the screen anew!

    If it is a single buffer window, you should flush the buffer to update the screen. I never used SDL by itself (without OpenGL) so I don't know the function names, but with this hint you know why your code doesn't work and you should be able to find the functions you need from SDL documentation.