c++ - 调节后检索/调试当前 FPS?

标签 c++ sdl frame-rate

我希望我的游戏能够以固定的 FPS 在我家附近的多台计算机上运行。我有一个简单的问题:如何调试当前的 fps

我是这样调节我的 fps 的:

SDL_Init(SDL_INIT_EVERYTHING);
const unsigned FPS = 20;
Uint32 start = SDL_GetTicks(); // How much time (milliseconds) has passed after SDL_Init was called.

while (true)
{
  start = SDL_GetTicks();

  if (1000 / FPS > SDL_GetTicks() - start)
    SDL_Delay(1000 / FPS - (SDL_GetTicks() - start)); // Delays program in milliseconds

我认为这应该可以调节我的帧率。问题是,我如何获得当前的 fps?

我试过了

std::stringstream fps;
fps << 1000 / FPS - (SDL_GetTicks() - start);
SDL_WM_SetCaption(fps.str().c_str(), NULL); // Set the window caption

fps << start / 1000; // and vice versa

但他们都没有给我想要的东西。

最佳答案

试一试这个 block :

while(true)
{
    // render
    // logic
    // etc...

    // calculate (milli-)seconds per frame and frames per second
    static unsigned int frames = 0;
    static Uint32 start = SDL_GetTicks();
    Uint32 current = SDL_GetTicks();
    Uint32 delta = (current - start);
    frames++;
    if( delta > 1000 )
    {
        float seconds = delta / 1000.0f;
        float spf = seconds / frames;
        float fps = frames / seconds;
        cout << "Milliseconds per frame: " << spf * 1000 << endl;
        cout << "Frames per second: " << fps << endl;
        cout << endl;

        frames = 0;
        start = current;
    }
}

请注意,uint/uint 将产生一个 uint,而 uint/float 是一个 float

关于c++ - 调节后检索/调试当前 FPS?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8861037/

相关文章:

ffmpeg - ffmpeg 认为音频帧和音频样本之间有什么区别?

ios - 奇怪的 SpriteKit 帧速率下降 - 怀疑挥之不去的 SKPhysicsBody 实例

c++ - 仍然无法将 WDK 8 集成到 VisualC++ 2012 速成版中?

c++ - 静态链接 SHFolder 组件

c++ - 循环后变量的内存会发生什么变化? (C++)

c - SDL_AddTimer 和线程,清除定时器队列

c++ - 指针不取值

c - 在 SDL 中设置 OGL 主要版本会导致错误

c++ - 自定义复制赋值运算符使程序崩溃(C++)

java - 处理/罗技C920采集帧率视频话语