title: Limiting FPS in C++
body: <p>I'm currently making a game in which I would like to limit the frames per second but I'm having problems with that. Here's what I'm doing:</p>
  
  <p>I'm getting the deltaTime through this method that is executed each frame:</p>
  
  <pre><code>void Time::calc_deltaTime() {
      double currentFrame = glfwGetTime();
      deltaTime = currentFrame - lastFrame;
      lastFrame = currentFrame;
  }
  </code></pre>
  
  <p>deltaTime is having the value I would expect (around 0.012.... to 0.016...)</p>
  
  <p>And than I'm using deltaTime to delay the frame through  the Sleep windows function like this:</p>
  
  <pre><code>void Time::limitToMAXFPS() {
  
      if(1.0 / MAXFPS &gt; deltaTime)
          Sleep((1.0 / MAXFPS - deltaTime) * 1000.0);
  }
  </code></pre>
  
  <p>MAXFPS is equal to 60 and I'm multiplying by 1000 to convert seconds to milliseconds. Though everything seems correct I'm sill having more than 60 fps (I'm getting around 72 fps)</p>
  
  <p>I also tried this method using while loop:</p>
  
  <pre><code>void Time::limitToMAXFPS() {
  
      double diff = 1.0 / MAXFPS - deltaTime;
  
      if(diff &gt; 0) {
  
          double t = glfwGetTime( );
  
          while(glfwGetTime( ) - t &lt; diff) { }
  
      }
  
  }
  </code></pre>
  
  <p>But still I'm getting more than 60 fps, I'm still getting around 72 fps... Am I doing something wrong or is there a better way for doing this?</p>