title: why start a timer in SDL when setting FPS?
body: <p>this is regarding <a href="http://www.lazyfoo.net/SDL_tutorials/lesson14/index.php" rel="nofollow">this</a> tutorial page from lazyfoo's set of SDL tutorials. There he first starts a timer to calculate how much time each frame should stay alive for, before it is refreshed. He does this using the following</p>
  
  <pre><code>if( ( cap == true ) &amp;&amp; ( fps.get_ticks() &lt; 1000 / FRAMES_PER_SECOND ) ) { 
   //Sleep the remaining frame time 
   SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - fps.get_ticks() ); 
  } 
  </code></pre>
  
  <p>Although I've found that fps.get_ticks() always returns 0 (??) and so isn't the above not needed(?), cant we just completely leave out the timer and just delay for 1000/FPS.</p>
  
  <p>I've tried both ways below and both give me the same thing. What am I missing here, why do we need a timer?.</p>
  
  <pre><code>#include "SDL/SDL.h"
  #include "SDL/SDL_image.h"
  #include &lt;iostream&gt;
  SDL_Surface *background = NULL;
  SDL_Surface *screen = NULL;
  SDL_Surface *msg = NULL;
  const int FPS = 20;
  
  void initialize(void){
      if (SDL_Init(SDL_INIT_EVERYTHING) == -1 ){
          std::cout&lt;&lt;"could not start sdl"&lt;&lt;std::endl;
      }
  
      screen = SDL_SetVideoMode(640,480,32,SDL_SWSURFACE);
      if (screen == NULL){
          std::cout&lt;&lt;"could not make screen"&lt;&lt;std::endl;
      }
  
  }
  void cleanUp(void){
      SDL_Quit();
      SDL_FreeSurface(background);
      SDL_FreeSurface(msg);
  }
  void loadFiles(void){
      background = IMG_Load("background.bmp");
      msg = IMG_Load("msg.bmp");
      if (background == NULL){
          std::cout&lt;&lt;"could not load background"&lt;&lt;std::endl;
      }
      if (msg == NULL){
          std::cout&lt;&lt;"could not load msg"&lt;&lt;std::endl;
      }
  }
  void blitSurf(int x,int y,SDL_Surface *source,SDL_Surface *dest){
      SDL_Rect dest_pos;
      dest_pos.x = x;
      dest_pos.y = y;
  
      if (SDL_BlitSurface(source,NULL,dest,&amp;dest_pos) == -1){
          std::cout&lt;&lt;"could not blit surface"&lt;&lt;std::endl;
      }
  }
  void update(void){
      if (SDL_Flip(screen) == -1 ){
          std::cout&lt;&lt;"could not update screen"&lt;&lt;std::endl;
      }
  }
  
  int main(int argc,char *argv[]){
      initialize();
      loadFiles();
  
      bool running = true;
      bool cap = false;
      int msg_pos_y = 0;
      int start = 0;
      int temp = 0;
      SDL_Event event;
      while (running == true){
          start = SDL_GetTicks();
  
          while (SDL_PollEvent(&amp;event)){
              if (event.type == SDL_KEYDOWN){
                  if (event.key.keysym.sym == SDLK_c){
                      if(cap == false){
                          cap = true;
                          std::cout&lt;&lt;"cap set to, true"&lt;&lt;std::endl;
                      }else{
                          cap = false;
                          std::cout&lt;&lt;"cap set to, false"&lt;&lt;std::endl;
                      }
                  }
              }   
              if (event.type == SDL_QUIT){
                      running = false;
                      std::cout&lt;&lt;"Quit was pressed"&lt;&lt;std::endl;
              }
          }
  
          blitSurf(0,0,background,screen);
          if (msg_pos_y &lt; 640){
              blitSurf(200,msg_pos_y,msg,screen);
              msg_pos_y++;
          }else{
              msg_pos_y = 0;
              blitSurf(200,msg_pos_y,msg,screen);
          }
          update();
  
  
          if ( (cap == true) &amp;&amp; ( (SDL_GetTicks()-start) &lt; (1000/FPS) ) ){
              SDL_Delay( (1000/FPS) - (SDL_GetTicks()-start) );
          }
  
          /* this works as well ??
          if ( cap == true ){
              SDL_Delay(1000/FPS);
          }
          */
      }
  
      cleanUp();
      return 0;
  }
  </code></pre>