evolon696
evolon696

Reputation: 267

retrieve/debug current FPS after regulating it?

i want my game to run on multiple computers around my house at a fixed FPS. i have a simple question: how do i debug my current fps

i am regulating my fps like this:

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

i think that should regulate my fps. the question is, how do i get the current fps?

i tried

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

and

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

but none of them gave me what i wanted.

Upvotes: 2

Views: 292

Answers (1)

genpfault
genpfault

Reputation: 52084

Give this block a shot:

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;
    }
}

Note that uint / uint will result in a uint, while uint / float is a float.

Upvotes: 1

Related Questions