帧限制器|为什么还有多余的毫秒

Framelimiter | Why are there extra milliseconds?

本文关键字:多余 限制器 为什么      更新时间:2023-10-16

在对这个东西进行了大量测试之后,我仍然不明白为什么在毫秒限制之外会附加额外的毫秒。

在这种情况下,整个运行循环应该持续4000ms,然后打印4000,然后再打印一些其他数据,但它总是在4013ms左右。

我目前知道问题不在于压力测试,因为如果没有压力测试,它仍然在4013ms左右。此外,压力测试的时间是有限制的,剩余时间内可以完成多少渲染来证明时间是合理的。我也知道它不是"SDL_GetTicks",包括我初始化变量的时间,因为它只在第一次调用时开始计时。这也不是调用函数所需的时间,因为我也用一个非常轻量级的纳秒计时器进行了测试,结果是一样的。

以下是我的一些结果,打印在最后:

  • 4013 100 993 40
  • 4013 100 1000 40
  • 4014 100 1000 40
  • 4012 100 992 40
  • 4015 100 985 40
  • 4013 100 1000 40
  • 4022 100 986 40
  • 4014 100 1000 40
  • 4017 100 993 40

与第三列(渲染的帧数量)不同,第一列的变化不应该超过退出循环等所需的几纳秒。这意味着它甚至不应该显示差异,因为在这种情况下计时器的范围是毫秒。我在它们之间重新编译,列表基本上保持不变。

这是代码:

#include <iostream>
#include <SDL/SDL.h>
void stress(int n) {
    n = n + n - n * n + n * n;
}
int main(int argc, char **argv) {
    int running = 100,
        timestart = 0, timestep = 0,
        rendering = 0, logic = 0,
    SDL_Init(SDL_INIT_EVERYTHING);
    while(running--) { // - Running loop
        timestart = SDL_GetTicks();
        std::cout << "logic " << logic++ << std::endl;
        for(int i = 0; i < 9779998; i++) { // - Stress testing
            if(SDL_GetTicks() - timestart >= 30) { // - Maximum of 30 milliseconds spent running logic
                break;
            }
            stress(i);
        }
        while(SDL_GetTicks() - timestart < 1) { // - Minimum of one millisecond to run through logic
            ;
        }
        timestep = SDL_GetTicks() - timestart;
        while(40 > timestep) {
            timestart = SDL_GetTicks();
            std::cout << "rendering " << rendering++ << std::endl;
            while(SDL_GetTicks() - timestart < 1) { // - Maximum of one rendering frame per millisecond
                ;
            }
            timestep += SDL_GetTicks() - timestart;
        }
    }
    std::cout << SDL_GetTicks() << " " << logic << " " << rendering << " " << timestep << std::endl;
    SDL_Quit();
    return 0;
}

详述众包评论-如果你的操作系统决定切换任务,你最终会出现0到1毫秒之间的随机错误(或者,如果SDL_GetTicks将对其内部计时器进行舍入,则为-5到.5,但你的结果总是大于预期,这表明它实际上是在截断)。这些将在你的下一次繁忙等待中"均衡",但不会在循环结束时出现,因为那里没有"下一次忙碌等待"。为了抵消它,你需要一个在开始游戏循环之前的参考点,并将其与GetTicks进行比较,以衡量"泄露"了多少时间。此外,每帧X毫秒的方法和计算过程中的繁忙等待/中断并不是我见过的最干净的方法。你可能应该在谷歌上搜索一下游戏循环并阅读一下。