在调节后检索/调试当前的FPS?

时间:2012-01-14 08:24:43

标签: 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。问题是,我如何获得当前的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

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

1 个答案:

答案 0 :(得分:1)

给这个区块一个镜头:

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