I have 3 “layers”, each layer contain other graphic elements. The top layer is the debug layer which must be above everything, then there is a loading layer and then finally the game layer itself. When I call for a new screen, the screen manager places down and stretches a 1x1 black image over the screen and fades it in. All works as it is intended. But whenever I draw on the debug layer, the whole layering order reverses and weird things happen. The game layer somehow comes over the load layer, and the debug layer is just flickering. It must be something to do with the spritefont drawing because without it, it works just fine.
I found it very surprising that not the whole debug layer goes down, but only a part of it. Here are 2 pictures:
Before loading the screen, note the “Debugggggggggggggggg” text at the bottom left of the screen.
Is everything just using SpriteBatch to be rendered? If so it might be the sorting parameters you’re supplying to SpriteBatch.Begin(). I believe SortMode.Immediate will disable sorting and just render everything in the order you supply it. That said, it will probably hurt performance as you will be switching textures and doing many draw calls under the hood. A better way might be to do the SpriteBatch.Begin() and End() block for each layer (forcing it to only sort within the layers).
Thank you for the fast responde Aranda. It is working now. I am using 3 batches now, one for each layer. Though I found that I must use Immediate sorting for the debug layer, or else some of the letters end up on other layers. It is really weird.
You are using SpriteSortMode.BackToFront, so what were you using for the depth parameter? If each draw had the exact same value for the depth, then sorting is indeterminate and you could well get odd render issues like you were seeing.