简体   繁体   中英

Why does one SpriteBatch.Draw causes frame rate to drop 23 fps?

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.

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 (ie using spritesheets or a texture atlas)? Here is an older link about how proper sprite sorting can affect performance

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM