c++ - std::chrono - fixed time step loop -


i'm trying make fixed time step loop using < chrono >.

this code:

#include <iostream> #include <chrono>  int main() {     std::chrono::steady_clock::time_point start;     const double timeperframe = 1.0 / 60.0;     double accumulator = 0.0;     int = 0;     while(true)     {         start = std::chrono::steady_clock::now();         while(accumulator >= timeperframe)         {             accumulator -= timeperframe;             std::cout << ++i << std::endl;             //update();         }         accumulator += std::chrono::duration_cast<std::chrono::duration<double>>(std::chrono::steady_clock::now() - start).count();         //render();     }     return 0; } 

value of variable "i" printed less 60 times second. same situation takes place when i'm trying change "timeperframe" "1.0". wrong it?

#include <iostream> #include <chrono> #include <thread>  int main() {     using namespace std::chrono;     using framerate = duration<steady_clock::rep, std::ratio<1, 60>>;     auto next = steady_clock::now() + framerate{1};     int = 0;     while(true)     {         std::cout << ++i << std::endl;         //update();         std::this_thread::sleep_until(next);         next += framerate{1};         //render();     }     return 0; } 

here's same thing busy loop:

int main() {     using namespace std::chrono;     using framerate = duration<steady_clock::rep, std::ratio<1, 60>>;     auto next = steady_clock::now() + framerate{1};     int = 0;     while(true)     {         std::cout << ++i << std::endl;         //update();         while (steady_clock::now() < next)             ;         next += framerate{1};         //render();     }     return 0; } 

Comments

Popular posts from this blog

inversion of control - Autofac named registration constructor injection -

verilog - Systemverilog dynamic casting issues -

ios - Change Storyboard View using Seague -