C++ win32 以固定的时间步长打印到控制台
C++ win32 printing to console in fixed timesteps
我正在尝试创建一个函数,该函数将允许我输入所需的每秒帧数和最大帧数,然后在固定的时间步长上将函数"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 的限制。
- c ++控制台应用程序,如何在控制台外部打印字符
- 控制台打印时间功能产生1
- 如何通过按 F2 或 F3 等键使控制台打印某些内容
- cout 不打印任何内容到控制台
- 如何在 C++ 中将 U32 字符串和 U16string 打印到控制台
- 打印到文件和控制台 C++
- 在 c++ 中打印到控制台的最佳方式是什么?
- 我的控制台正在打印随机单词以及没有执行正确的数学运算
- C++控制台打印在同一行上,带回车符
- 硬编码字符串与强制转换为 PUCHAR 并打印到控制台时从控制台读取的字符串的结果不同
- V8 控制台.log不打印
- 使用 C++ shell 为 C++ 中的 2 D 动态数组赋值,控制台不会打印出任何内容
- cURL 保持打印传输到控制台?
- SDL 未打印到控制台
- qDebug() 在升级到 ubuntu 17.10(和 Fedora)后停止工作(不再打印到控制台)
- 使用具有 UTF8 编码的源文件将 UTF8 符号打印到 Windows 控制台
- 如何在 ATL COM 模块中打印控制台输出
- 打印到控制台或字符串/流
- Cout 无法在显示器上打印(控制台)
- 禁止从导入的 DLL 打印控制台