How to correctly calculate FPS in XNA?

Posted by Tomek Tarczynski on Stack Overflow See other posts from Stack Overflow or by Tomek Tarczynski
Published on 2010-03-24T22:07:28Z Indexed on 2010/03/25 0:13 UTC
Read the original article Hit count: 772

Filed under:
|

I wrote a component to display current FPS.
The most important part of it is:

    public override void Update(GameTime gameTime)
    {
        elapseTime += (float)gameTime.ElapsedRealTime.TotalSeconds;
        frameCounter++;

        if (elapseTime > 1)
        {
            FPS = frameCounter;
            frameCounter = 0;
            elapseTime = 0;
        }
        base.Update(gameTime);
    }

In most cases it works ok, but recently I had a problem.
When I put following code into Update method of game strange thing starts to happen.

       if (threadPath == null || threadPath.ThreadState != ThreadState.Running)
        {
            ThreadStart ts = new ThreadStart(current.PathFinder.FindPaths);
            threadPath = new Thread(ts);
            threadPath.Priority = ThreadPriority.Highest;
            threadPath.Start();
        }

Main idea of this code is to run pathFinding algorithm in different thread all the time.

By strange things I mean that sometimes FPS drasticly decreases, this is obvious, but displayed FPS changes more often than once a second. If I understand this code FPS can't change more often than once a second.

Can someone explain me what's going on?

© Stack Overflow or respective owner

Related posts about XNA

Related posts about fps