title: C++ Jittery game loop - how to make it as smooth as possible?
body: <p>This is my gameloop code:</p>
  
  <pre><code>        while (shouldUpdate)
          {
              timeSinceLastUpdate = 0;
              startTime = clock();
  
                  while (timeAccumulator &gt;= timeDelta)
                  {
                      listener.handle();
                      em.update();
                      timeAccumulator -= timeDelta;
                      timeSinceLastUpdate += timeDelta;
                  }
  
              rm.beginRender();
                  _x-&gt;draw();
              rm.endRender();
  
              timeAccumulator += clock() - startTime;
          }
  </code></pre>
  
  <p>It runs almost perfectly, but it has some jitter to it, a few times a second instead of _x (a test entity that all it does in update is x++) moving 1 pixel to the right, it actually moves 2 pixels to the right and it's a noticeable lag/jitter effect. I'm guessing clock() isn't accurate enough. So what could I do to improve this game loop?</p>
  
  <p>If it matters I use SDL and SDL_image.</p>
  
  <p><strong>EDIT</strong>: Nothing changed after doing something more accurate than clock. BUT, what I have figured out, is that it's all thanks to timeDelta. This is how timeDelta was defined when I made this post:</p>
  
  <pre><code>double timeDelta = 1000/60;
  </code></pre>
  
  <p>but when I defined it as something else while messing around...</p>
  
  <pre><code>double timeDelta = 16.666666;
  </code></pre>
  
  <p>I noticed for the first few seconds the game started, it was smooth as butter. But just a few seconds later, the game stuttered hugely and then went back to being smooth, and repeated itself like that. The more 6's (or anything after the . really) i added, the longer the game was initially smooth and the harder the lag hit when it did. It seems floating errors are attacking. So what can I do then?</p>
  
  <p>EDIT2: I've tried so much stuff now it's not even funny... can someone help me with the math part of the loop? Since that's what's causing this...</p>
  
  <p>EDIT3: I sent a test program to some people, some said it was perfectly smooth and others said it was jittery like how I described it. For anyone that would be willing to test it here it is(src): <a href="https://www.mediafire.com/?vfpy4phkdj97q9j" rel="nofollow">https://www.mediafire.com/?vfpy4phkdj97q9j</a></p>
  
  <p>EDIT4: <strong><em>I changed the link to source code</em></strong>.</p>