我希望我的游戏能够以固定的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

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

最佳答案

试一下这个方块:

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 / floatfloat

07-24 13:40