Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR CPP

c++ fps sleep while loop

#include "chrono_io.h"
#include <chrono>
#include <cstdint>
#include <iostream>
#include <thread>

int
main()
{
    using namespace date;
    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);
        // just for monitoring purposes
        cout << "Time: " << system_clock::now() - lastFrame << '
';
        lastFrame = nextFrame;
        nextFrame += frames{1};
    }
}
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #fps #sleep #loop
ADD COMMENT
Topic
Name
2+3 =