How to limit FPS in a loop with C++?

后端 未结 4 1414
不思量自难忘°
不思量自难忘° 2021-01-30 22:38

I\'m trying to limit the frames per second in a loop that is performing intersection checking, using C++ with chrono and thread.

Here is my code:

std::ch         


        
4条回答
  •  爱一瞬间的悲伤
    2021-01-30 23:35

    This is much like Galik's answer, but it keeps the syntax of the OP's question and doesn't drop down to the C API. Additionally it creates a custom unit for the frame duration which I believe is important for readability:

    #include 
    #include 
    #include 
    #include 
    
    int
    main()
    {
        using namespace std;
        using namespace std::chrono;
    
        using frames = duration>;  // 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(system_clock::now() - lastFrame).count()
                 << "ms\n";
            lastFrame = nextFrame;
            nextFrame += frames{1};
        }
    }
    

    This outputs for me:

    Time: 200ms
    Time: 205ms
    Time: 205ms
    Time: 203ms
    Time: 205ms
    Time: 205ms
    Time: 200ms
    Time: 200ms
    Time: 200ms
    ...
    

    Key things to note:

    • A concise way to document 5Hz: using frames = duration>;
    • Use of sleep_until instead of sleep_for, which takes care of the unknown of how long it takes to get the real work done.
    • No use of .count() except for I/O, and here's a library to get rid of that.
    • No manual conversion of units (e.g. / 1000).
    • No floating point units, not that there's anything wrong with that.
    • Minimal need to specify or depend upon explicit units.

    With the addition of the duration I/O library, here is how the above code would be changed:

    #include "chrono_io.h"
    #include 
    #include 
    #include 
    #include 
    
    int
    main()
    {
        using namespace date;
        using namespace std;
        using namespace std::chrono;
    
        using frames = duration>;  // 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 << '\n';
            lastFrame = nextFrame;
            nextFrame += frames{1};
        }
    }
    

    The output would differ depending upon platform (depending upon the "native duration" of system_clock). On my platform it looks like this:

    Time: 200042µs
    Time: 205105µs
    Time: 205107µs
    Time: 200044µs
    Time: 205105µs
    Time: 200120µs
    Time: 204307µs
    Time: 205136µs
    Time: 201978µs
    ...
    

提交回复
热议问题