восстановить / отладить текущий FPS после его регулирования? - PullRequest
2 голосов
/ 14 января 2012

Я хочу, чтобы моя игра работала на нескольких компьютерах вокруг моего дома с фиксированным 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 Ответ

1 голос
/ 16 января 2012

Дайте этому блоку шанс:

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.

...