[英]Keep track of time in a game loop in C++
How can I keep track of time in seconds and milliseconds since my game/program started? 从游戏/程序开始以来,如何跟踪以秒和毫秒为单位的时间? I can use the
clock()
function but I hear it is not that accurate. 我可以使用
clock()
函数,但我听说它不准确。 Is there a better way? 有没有更好的办法?
You can use the chrono library in C++ Here is a code sample: 你可以在C ++中使用chrono库这是一个代码示例:
#include <chrono>
#include <iostream>
using namespace std;
using namespace std::chrono;
int main() {
high_resolution_clock::time_point t1 = high_resolution_clock::now();
high_resolution_clock::time_point t2 = high_resolution_clock::now();
duration<double> time_span = duration_cast<duration<double>>(t2 - t1);
cout << time_span.count() << " seconds\n";
return 0;
}
Note that this c++11, so to compile it you should use the flag -std=c++11 注意这个c ++ 11,所以要编译它,你应该使用标志-std = c ++ 11
$ g++ -std=c++11 test.cpp -o test
This exact piece of code gave 4e-07 seconds on my PC. 这段确切的代码在我的电脑上给了4e-07秒。
Hope that helps. 希望有所帮助。
A cross-platform and easy solution would be to use the chrono library 跨平台且简单的解决方案是使用计时库
Example: 例:
#include <iostream>
#include <chrono>
void gameFunction()
{
// start()
// end()
}
int main()
{
auto t1 = std::chrono::high_resolution_clock::now();
gameFunction();
auto t2 = std::chrono::high_resolution_clock::now();
auto elapsed_time = std::chrono::duration_cast<std::chrono::microseconds>( t2 - t1 ).count();
std::cout << elapsed_time << std::endl;
return 0;
}
Finally note that you will need a at least C++11 for this to work, so set the -std= flag to at least c++11. 最后请注意,为此需要至少使用C ++ 11,因此请将-std =标志设置为至少c ++ 11。 For example:
例如:
g++ -std=c++11 game.cpp -o game
I highly recommend you look at Handmade Hero . 我强烈建议你看看手工制作的英雄 。 Casey records creating an entire game in a series of video episodes.
凯西记录了一系列视频集中的整个游戏。 In one of the early episodes he discusses determining wall-clock time on windows using
QueryPerformanceCounter
and QueryPerformanceFrequency
在其中一篇早期剧集中,他讨论了使用
QueryPerformanceCounter
和QueryPerformanceFrequency
确定Windows上的挂钟时间。
He also discusses using cpu cycle counts although those are useful only assuming a constant processor clock speed. 他还讨论了使用cpu循环计数,尽管只有假设处理器时钟速度恒定才有用。
He talks about these issues again in later episodes: https://hero.handmade.network/episode/game-architecture/day113 and https://hero.handmade.network/episode/game-architecture/day177 他在后面的剧集中再次谈到这些问题: https : //hero.handmade.network/episode/game-architecture/day113和https://hero.handmade.network/episode/game-architecture/day177
Since you are looking for a solution in a game loop, these videos will probably be of interest at least even if you use the cross-platform solution from @Pranshu. 由于您正在寻找游戏循环中的解决方案,因此即使您使用@Pranshu的跨平台解决方案,这些视频也可能至少会引起您的兴趣。 You are likely ok for a game using a platform dependent method to get a more accurate clock.
对于使用平台相关方法的游戏,您可能无法获得更准确的时钟。
I'll point out that high_resolution_clock
provides the highest resolution clock available by the system that the library knows about so it might not be any more accurate than using system_clock
or steady_clock
. 我要指出,
high_resolution_clock
提供了库所知的系统可用的最高分辨率时钟,因此它可能不比使用system_clock
或steady_clock
更精确。 (It uses steady_clock
on my OSX box.) (它在我的OSX盒子上使用
steady_clock
。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.