Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

c++ fps sleep while loop

#include <chrono>
#include <cstdint>
#include <iostream>
#include <thread>

int
main()
{
    using namespace std;
    using namespace std::chrono;

    using frames = duration<int64_t, ratio<1, 5>>;  // 5Hz
    auto nextFrame = system_clock::now();
    auto lastFrame = nextFrame - frames{1};;

    while (true)
    {
        // Perform intersection test

        this_thread::sleep_until(nextFrame);
        cout << "Time: "  // just for monitoring purposes
             << duration_cast<milliseconds>(system_clock::now() - lastFrame).count()
             << "ms
";
        lastFrame = nextFrame;
        nextFrame += frames{1};
    }
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #fps #sleep #loop
ADD COMMENT
Topic
Name
4+4 =