I am having a major performance problem with a game I'm developing for Windows Phone 7 in C# XNA 4.0.
There's a lot of code going on, like collision, input, animations, physics and so on.
The frame rate has been set to 60fps, but is only running at 32fps.
I have tried a lot of things, like disabling though stuff like collision detection, but nothing helped getting higher frame rate.
Now randomly I discovered when disabling the drawing of the background, which is just a standard 480x800 sized image (Same as the Windows Phone Resolution), and uses the default method "spriteBatch.Draw(Textures.background, Vector2.Zero, Color.White)" the frame rate goes from 32 to 55 fps. I have also tried changing the texture to a plain white one, but that does not help either, and I have also tried moving the drawing of the background to another place in the code, but nothing changed either.
I tried making a new project, and just having the background drawed, but the fps would be at 60 fps then as it should.
I'm only having one SpriteBatch.Begin() and SpriteBatch.End(), where all the needed sprites are drawed inside.
There's 256 Texture2Ds loaded into the game, which all is loaded at the beginning of the game.
The game is a sidescroller, so the background needs to move to the left all the time, but even if I just set it to Vector2.Zero, it would still ruin the fps by -20fps.
I hope anyone has a solution to this, or at least an idea of why this is happening.
1
votes
Possibly XNA enables VSyncing by default. If so, does a windows phone refresh at 60fps? If your draw routine is running at say 59 fps, it'd force it to the next available refresh rate - 30fps
– George Duckett
Are you loading your texture in the Draw method or in the LoadContent method? Move to LoadContent..
– Rico Suter
@GeorgeDuckett It doesen't matter if VSync is on or off, it's still about the same frame rates.
– Basic
@RicoSuter As I wrote earlier, all the textures is loaded into the game at the beginning (LoadContent)
– Basic
If VSync is on, the render method could wait until a refresh, that happens at 60fps. If by the next time the refresh has just happened the render method has to wait, which could result in the exact halving of fps as you observe. (i mentioned as you were getting around 60, now around 30).
– George Duckett
1 Answers
3
votes
If you have 256 individual Texture2Ds being used within the same SpriteBatch Begiin/End call it's not surprising that performance isn't optimal unless you are ordering the sprites by texture, which you likely are not for a platformer. All that texture switching within the same batch will cause a decrease in framerate - it's likely that the background image is just the straw that breaks the camels back for your particular game setup.
Have you tried combining those 256 separate images into a smaller number of Texture2Ds (i.e. using spritesheets or a texture atlas)? Here is an older link about how proper sprite sorting can affect performance