c++ - 每 0.25 秒做一次
问题描述
以下函数连续运行:
void MyClass::handleTriggered(float dt)
{
// dt is time interval (period) between current call to this function and previous one
}
dt
类似于0.0166026
秒(来自每秒 60 帧)。
我打算每秒钟做点什么0.25
。目前我正在使用我们处于 60 fps 的事实,即函数调用每秒发生 60 次:
static long int callCount = 0;
++callCount;
// Since we are going with 60 fps, then 15 calls is 1/4 i.e. 0.25 seconds
if (callCount % 15 == 0)
// Do something every 0.25 seconds (every 15 calls)
现在我想知道如何以另一种方式做到这一点,使用float
类型而不是int
:
static float sumPeriod = 0.0;
// Total time elapsed so far
sumPeriod += dt;
if (/* How to compose the condition? */) {
// Every 0.25 seconds do something
}
解决方案
你必须总结dt
's,当它们达到 0.25 时,你从总和中减去 0.25 并做你的事情。
static float sum = 0.0;
sum += dt;
if(sum > 0.25) {
sum -= 0.25;
// Do what you want to do every 0.25 secs here.
}
推荐阅读
- tensorflow - tf.transform 可以处理 viewfs:// 路径吗?
- python - Django ModelMultipleChoice 验证错误
- javascript - MongoDB collection.findOne API 没有响应
- c# - SQLcommandTimeout = -1 是什么意思?
- reactjs - 组件上的扩展运算符禁用 eslint-config-airbnb 上的 react/prop-types 规则
- java - javax.xml 库在写入文件时添加 ^M 字符
- biztalk - BizTalk:SB 消息传递适配器:它是否轮询?或任何其他回电?
- java - 如何将时间戳字符串转换为另一个时区的时间戳
- r - R:在 ggplot 中分配手动颜色将自身限制为几种颜色
- vba - 获取具有动态名称的目录中最后修改的文件或文件夹