C++ win32 以固定的时间步长打印到控制台

C++ win32 printing to console in fixed timesteps

本文关键字:打印 控制台 时间 win32 C++      更新时间:2023-10-16

我正在尝试创建一个函数,该函数将允许我输入所需的每秒帧数和最大帧数,然后在固定的时间步长上将函数"cout"发送到控制台。我也在使用 Sleep() 来避免忙于等待。我似乎使程序的睡眠时间比它需要的要长,因为我认为它一直在睡眠命令上停滞不前。你能帮我这个吗?我在理解时间时遇到了一些麻烦,尤其是在 Windows 上。

最终,我可能会使用这种计时方法来对一个简单的游戏进行计时和动画处理,也许像乒乓球,甚至是带有可以加速对象的简单程序。我想我已经足够了解GDI和wasapi,可以在屏幕上播放声音和显示颜色,所以现在我需要了解时间。在互联网上问这个问题之前,我已经看了很长时间,我确定我错过了一些东西,但我不能完全确定我的手指:(这是代码:

#include <windows.h>
#include <iostream>
// in this program i am trying to make a simple function that prints frame:  and the number frame  in between fixed time intervals
// i am trying to make it so that it doesn't do busy waiting
using namespace std;

void frame(LARGE_INTEGER& T, LARGE_INTEGER& T3, LARGE_INTEGER& DELT,LARGE_INTEGER& DESI, double& framepersec,unsigned long long& count,unsigned long long& maxcount,bool& on, LARGE_INTEGER& mili)
{
    QueryPerformanceCounter(&T3); // seccond measurement
    DELT.QuadPart = &T3.QuadPart - &T.QuadPart; // getting the ticks between the time measurements
    if(DELT.QuadPart >= DESI.QuadPart) {count++; cout << "frame: " << count << " !" << endl; T.QuadPart = T3.QuadPart; } // adding to the count by just one frame (this may cause problems if more than one passes)
     if(count > maxcount) {on = false;} // turning off the loop
    else {DESI.QuadPart = T.QuadPart + DESI.QuadPart;//(long long)framepersec; // setting the stop tick
        unsigned long long sleep = (( DESI.QuadPart - DELT.QuadPart) / mili.QuadPart);
        cout << sleep << endl;
        Sleep(sleep);} // sleeping to avoid busy waiting
}

int main()
{
    LARGE_INTEGER T1, T2, Freq, Delta, desired, mil;
    bool loopon = true; // keeps the loop flowing until max frames has been reached
    QueryPerformanceFrequency(&Freq); // getting num of updates per second
    mil.QuadPart = Freq.QuadPart / 1000; // getting the number clock updates that occur in  a millisecond
    double framespersec; // the number of clock updates that occur per target frame
    unsigned long long framecount,maxcount; //to stop the program after a certain amount of frames
    framecount = 0;

    cout << "Hello world! enter the amount of frames per second : " << endl;
    cin >> framespersec;
    cout << "you entered: " << framespersec << " ! how many max frames?" << endl;
    cin >> maxcount;
    cout << "you entered: " << maxcount << " ! now doing the frames !!!" << endl;
    desired.QuadPart = (Freq.QuadPart / framespersec);
    while(loopon == true) {
        frame(T1, T2, Delta, desired, framespersec, framecount, maxcount,loopon, mil);
    }

    cout << "all frames are done!" << endl;
    return 0;
}

睡眠时间受系统时钟频率的限制。频率默认为 64 Hz,因此您最终会看到以 16 毫秒为增量的睡眠。任何小于 16 毫秒的睡眠时间都至少为 16 毫秒 - 根据 CPU 负载,可能会更长。同样,20 毫秒的睡眠可能会四舍五入到 32 毫秒。

您可以通过调用 timeBeginPeriod(...) 和 timeEndPeriod(...) 来更改此时间段,这可以将睡眠精度提高到 1 毫秒。如果您查看 VLC 播放器等多媒体应用程序,您会发现它们使用这些功能来获得可靠的帧定时。请注意,这会更改系统范围的调度速率,因此会影响笔记本电脑的电池寿命。

更多信息:http://msdn.microsoft.com/en-us/library/windows/desktop/dd757624%28v=vs.85%29.aspxhttp://msdn.microsoft.com/en-us/library/windows/desktop/ms686298%28v=vs.85%29.aspx

可等待计时器比Sleep更准确,并且还可以更好地与 GUI 消息循环集成(将 GetMessage 替换为 MsgWaitForMultipleObjects)。 我以前曾成功地将它们用于图形计时。

它们不会为您提供高精度,例如以亚毫秒的时间控制串行或网络输出,但无论如何,UI 更新都受到 VSYNC 的限制。