title: Time based loop and Frame based loop
body: <p>Trying to understand the concepts of setting constant speed on game loop. My head hurts. I read the <a href="http://www.koonsolo.com/news/dewitters-gameloop/" rel="noreferrer">deWiTTERS page</a>, but I can't see the why/how...when I get it...it slips.</p>
  
  <pre><code>while(true)
  {
        player-&gt;update() ;
        player-&gt;draw() ;
  }
  </code></pre>
  
  <p>This will run as fast as possible depending on how fast a processor is...I get that. </p>
  
  <p>To run at the same speed on all computers, the logic is what I don't get. If I am trying to run at 60fps, then it means for every 16ms the objects move by a frame, yeah? What I don't get is how the <code>update()</code> or <code>draw()</code> may be too slow.</p>
  
  <p>deWiTTERS example (I used 60):</p>
  
  <pre><code>const int FRAMES_PER_SECOND = 60;
  const int SKIP_TICKS = 1000 / FRAMES_PER_SECOND;
  
  DWORD next_game_tick = GetTickCount();
  // GetTickCount() returns the current number of milliseconds
  // that have elapsed since the system was started
  
  int sleep_time = 0;
  
  bool game_is_running = true;
  
  while( game_is_running ) {
      update_game();
      display_game();
  
      next_game_tick += SKIP_TICKS;
      sleep_time = next_game_tick - GetTickCount();
      if( sleep_time &gt;= 0 ) {
          Sleep( sleep_time );
      }
      else {
          // Shit, we are running behind!
      }
  }
  </code></pre>
  
  <p>I don't understand why he gets the current time before the loop starts. And when he increments by <code>SKIP_TICKS</code> I understand he increments to the next 16ms interval. But I don't understand this part as well:</p>
  
  <pre><code>sleep_time = nextgametick  - GetTickCount() 
  </code></pre>
  
  <p>What does <code>Sleep(sleep_time)</code> mean? The processor leaves the loop and does something else? How does it achieve running 60fps? </p>