Help Needed! Little to no control over what is drawn in front

Nov 8, 2009 at 8:29 PM

I recently refactored my code to use a rudimentary forms system I have written. This new way of creating my menus is far superior to the previous method, but I have less control over what instances of SpriteBatch are used to draw my textures to the screen. I have buttons that are DrawableGameComponents that are added to a controls list in my FormManager. FormManager calls the Draw, Update, LoadContent methods etc of these buttons. Each button has its own SpriteBatch instance and is drawn outside of the code that is responsible for drawing the graphics that should sit behind the buttons.

In XNA I was able to use FrontToBack sort mode to put my window graphics on the lowest layer. In SilverSprite it's sporadic. Some of my buttons are behind my window graphics while others are in front as they should be. Tweaking the LayerDepth and sort mode only seems to make things worse.

What strategy can I use to keep my layers all drawn in the right order that supports both XNA and SilverSprite?

I thought about creating my own draw dispatcher that is a static or singleton. That would allow me to use one SpriteBatch instance for all of my graphics, but it would be a pretty hefty refactoring effort.

Appreciate any thoughts / ideas

Coordinator
Nov 14, 2009 at 4:41 PM

Are you using the latest from the source tree for this? I thought I had resolved most of these types of issues a while back. It's possible I don't fully understand how XNA handles FrontToBack etc. If you could send me a simple sample in both XNA and SilverSprite that shows this behavior it would help a lot.

Bill

Nov 14, 2009 at 5:03 PM

I'll grab the latest today and give it a try.

Nov 14, 2009 at 7:36 PM

It's still happening with the latest version. Is it only supposed to support draw order within the context of a single SpriteBatch or is it supposed to support across multiple SpriteBatch instances? What about across multiple DrawableGameComponents? Is that something that could be causing the problem? I don't have any problem working around some differences in SilverSprite because they will likely work in XNA as well anyway. I just want to make sure I know how I should approach drawing objects on specific layers.

I will say this though, I'm getting 76 FPS on my main screen with the latest SilverSprite source which has never happened. I have never seen it get above 60 before. Good work!

Coordinator
Nov 14, 2009 at 9:42 PM

I'd like it to work how it does in XNA. Does FrontToBack work across multiple spritebatches in XNA?

Nov 15, 2009 at 3:48 AM
Edited Nov 15, 2009 at 3:50 AM

I'm not an expert on this part of XNA. What I do know is that I can trial and error the LayerDepth across multple sprite batches in XNA and get it to look right, but haven't gotten it to behave properly in SilverSprite.

I guess that means that yes it does. Although in order to detail specifically how it handles it across SpriteBatches and DrawableGameComponents I would have to do more research.

Nov 16, 2009 at 1:18 PM
Edited Nov 16, 2009 at 1:19 PM

If you have a facebook account you can check out what is happening here: http://apps.facebook.com/battleschool.

Explanation of what you're seeing:

It comes up on the MainMenu page which is itself a DrawableGameComponent. This class has a FormManager in it which is also a DrawableGameComponent. The curvy line and "Cherub" logo are being drawn by a SpriteBatch in a base class of MainMenu. The obscured menu items (Play Game, Statistics, Exit Game) are supposed be shown in front of the curvy line as well as the feather icons to their left. The menu items themselves are FormComponents and are added to the FormManager. Each FormComponent has its own Draw method that is called by the Draw method on the FormManager.

Here is a screenshot of the same code in XNA: http://cid-ed3ec499646d9ba3.skydrive.live.com/self.aspx/.Public/CherubScreenshot.jpg

Let me know what I can do to help with this or if you know of a work around.

Thanks

Nov 23, 2009 at 3:58 PM

I'm going to call this resolved. It seems to have just been a training issue. I was ignoring the DrawableGameComponent.DrawOrder property entirely. I'm not sure why it worked in XNA but not SilverSprite. Perhaps by default XNA was sorting on some other secondary property after DrawOrder? Nevertheless if I pay closer attention to the DrawOrder I provide for each of my DrawableGameComponents it becomes a very trivial thing to overcome.

Firstly, order your DrawableGameComponents how you like them, and deal with DrawDepth in the SpriteBatch individually in each component.