XNA Platformer (2D) - Framerate/FPS fluctuations

1.6k views Asked by At

I'm new to C Sharp, and writing a game w/ the XNA Framework. I've created a project that's a heavily modified version of the "Platformer" XNA starter kit.

I'm seeing (seemingly) random fluctuations with the framerate. Sometimes it'll run at 60 FPS the whole time, sometimes it'll start at 60 FPS then drop to 49-52, and othertimes it'll drop to 49-52 immediately. Using Fraps to display framerate (not recording video to disk).

The unique nature of this game requires that it run at 60 FPS consistently.

So it seems some race condition or random factor is causing a difference between individual runs of the exe. Numerous rebuilds make no difference.

This fluctuation occurs on both my desktop and laptop with exactly the same frequency, so it's not an issue w/ hardware, anti-virus, etc.

I've searched about how to lock framerates in XNA, and my code seems to be doing much of what it needs to do, including an attempt to clamp at 60 FPS (using IsFixedTimeStep, SynchronizeWithVerticalRetrace).

The game is absolutely capable of 60 FPS from beginning to end, I see it all the time. When it's running at 60 FPS it does not tax CPU, RAM or any other resource as far as I can tell.

Anyone else experienced this?

Thanks, - S

7

There are 7 answers

0
Drew Noakes On

I would check whether these periodic slowdowns coincide with GC events, specifically of generations 1 or 2. From what you're describing, that seems quite plausible. If so, look at whether you can reduce object churn in your code via reuse, stack allocations, etc.

0
Coincoin On

Try disabling V sync SynchronizeWithVerticalRetrace=false and see if it helps. V sync have the nasty side effect of dramatically dropping your framerate if an event occurs at sync time or if a frame is slightly too long to render, since it will have to wait for the next sync. However, you will likely experience tearing. However, if framerate stability is more important than tearing free display, that might be a good compromise.

0
Empyrean On

The inconsistency you describe means that the problem is either caused by

  • An environmental factor such as another process; or
  • A code path in your game that isn't taken on every run

The most likely cause is another process that is running on both your computers.

Close down all non-essential processes like media players. Windows Media Player and iTunes can both reduce your framerate while they are playing. Fraps should be ok as long as it isn't recording but I would implement your own built in FPS display just to be sure.

Use the windows performance monitor to check if there is a process consuming cpu or memory. Especially look for instances of your game that have failed to shutdown properly and are still running in the background.

Other things you could try to narrow down an environmental cause include:

  • Determine if the game runs at 60fps after a clean reboot
  • Determine if the game always runs at 60fps on the first run
  • Launch your game from explorer instead of visual studio
  • Determine if running in Release or Debug mode has any impact
  • Run your game on a friend's computer

If the cause is from a code path inside your game that isn't executed on every run you could:

  • repeatedly play and try to determine what it is you do in the game that triggers the slow down.
  • implement an input recording and playback system so that the same run through the game is exactly repeatable
  • profile your garbage collection and general performance to look for any problems that stand out
0
FreeAsInBeer On

You could sidestep this issue completely. As long as your game runs at a minimum of about 35 fps, the framerate drops won't be noticeable to the human eye. In order to sidestep this issue, ensure that all of your update code takes into account the GameTime object that it's passed. Usually you would multiply any calculations by it so that you get smaller calculations when the framerate is high, and much more pronounced results when your game is updating slowly. Overall, it will make your game visuals seem more fluid.

Have you tried allowing the game to run at it's max framerate? If so, do you still receive huge framerate drops? If you don't know how to do this, I can post the code when I get home if you need it.

0
FrootyBear On

Are you drawing anything outside of the visible area? I've had the same issue (framerate would suddenly drop). After trying everything it turned out I was sometimes drawing things just outside of the visible area or halfway between visible and invisible areas of the screen. Given you're using the platformer starter kit, this is probably your problem as well.

0
Emond On

You will never be able to force the game to run at 60 fps.

If the game really needs to run at this speed you are probably updating game state per frame. If so, you should change that into an update that takes elapsed time into consideration.

0
Scott Nelson On

I had the exact same problem. I created a game based on the Platformer starter kit, and I use Windows 7. The program runs fine most of the time at 60 FPS, but then it sometimes it will drop down to 52 FPS for about 30 seconds, and then it will go back to 60 FPS.

I discovered that by turning off the Windows 7 Aero features, my game now stays at 60 FPS consistently. I had to switch over to the Windows 7 Basic theme with no Aero, and now the game is running nice and smooth.