我可以用std::chrono::high_resolution_clock替换SDL_GetTicks吗?
Can I replace SDL_GetTicks with std::chrono::high_resolution_clock?
检查c++中的新东西,我找到了std::chrono库。
我想知道std::chrono::high_resolution_clock是否可以很好地替代SDL_GetTicks?
使用std::chrono::high_resolution_clock
的优点是避免在Uint32
中存储时间点和时间持续时间。std::chrono
库附带了各种各样的std::chrono::duration
库,您应该使用它们。这将使代码更具可读性,更少歧义:
Uint32 t0 = SDL_GetTicks();
// ...
Uint32 t1 = SDL_GetTicks();
// ...
// Is t1 a time point or time duration?
Uint32 d = t1 -t0;
// What units does d have?
vs: using namespace std::chrono;
typedef high_resolution_clock Clock;
Clock::time_point t0 = Clock::now();
// ...
Clock::time_point t1 = Clock::now();
// ...
// Is t1 has type time_point. It can't be mistaken for a time duration.
milliseconds d = t1 - t0;
// d has type milliseconds
用于保存时间点和时间持续时间的类型化系统与仅在Uint32
中存储内容相比没有开销。除了可能的东西将被存储在Int64
代替。但如果你真的想的话,你也可以自定义:
typedef duration<Uint32, milli> my_millisecond;
可以使用以下命令检查high_resolution_clock
的精度:
cout << high_resolution_clock::period::num << '/'
<< high_resolution_clock::period::den << 'n';
SDL_GetTicks返回毫秒,所以完全可以使用std::chrono,但要注意单位转换。它可能不像SDL_GetTicks那么简单。而且,起始点也不会相同。
相关文章:
- 模板参数替换失败,并且未完成隐式转换
- 在VS代码中交叉编译Windows与Linux上的MinGW的SDL程序
- 如何用转义符替换字符串中的所有特殊字符
- Mix_Init和Mix_OpenAudio SDL之间的区别是什么
- SDL 窗口不会弹出
- 为什么除非添加括号,否则构造函数上的模板替换会失败?
- 在一个读写器队列中,我可以用volatile替换原子吗
- 用符号版本替换对函数的所有调用
- 如何通过替换顺序代码的while循环来添加OpenMP for循环
- 替换基于地图的所有引用
- 按平均值替换数组中的元素
- SDL 操纵杆按钮按下/C++
- 我可以在这里替换什么,因为我不能在 C# 中使用隐式变量的 lambda 函数?
- 如何将字节数组元素替换为修改的十六进制 ASCII 符号?
- VSCode IntelliSense无法识别SDL框架的SDL_image扩展库
- 初始化 std::vector 替换为单大括号而不是双大括号
- 删除/替换C++字符串中的多字符 (ÿû)
- 将 malloc 替换为数组
- 如何替换此示例代码片段中已弃用的handler_type_t或 boost::asio::handler_type?
- 我想用sdl中的图像替换一个矩形