DrawableGameComponent Drawing Order…

So I’ve discovered an interesting situation with my current project, with the order that things are being rendered.

Currently, I’ve got something structured more or less the same as the Game State Management sample on the XNA Creators website. I started with something similar and canabalized it until I got it into the state that I wanted it. I’ve got it all wedged into a DrawableGameComponent in a separate library project. So, when I create a brand new XNA project, the plan is all that I have to do is add in the existing project, add in the DrawableGameComponent, and hit F5 to get stuff on the screen. That, and I really do like the idea of having everything in a separate class library.

Anyway, so just recently I discovered a different sort of an issue. In the main game drawing loop of the actual game, I’m drawing out some debug information. I discovered that when I started drawing some 3D content from one of the GameScreens it was drawing over top of the debug information that was being drawn. I checked the main draw call in the game class, and the only thing that it was drawing was the debug text. Everything else was being drawn by the DrawableGameComponent. This means that I don’t entirely have control of when things are drawn, unless they too are being drawn from DrawableGameComponents. I know there is a field on the DrawableGameComponent to control the draw order, but this only seems to affect the order of DrawableGameComponents being drawn in relation to each other.

So, I suppose there really is only one way that I can think of to solve this problem: put my debug drawing code inside a DrawableGameComponent. This is a bit of a pain in the butt, but probably the best alternative in the long run anyway, especially once I start adding other bits for scripting and a proper console.

Update: So it turns out there is a fairly straightforward solution to this problem. What happens in the DrawableGameComponents are drawn when the call is made to base.Draw() in the main rendering loop of the game. So, if you want your DrawableGameComponents to be drawn before the other stuff that you are drawing in the main rendering loop, clear your screen, then call base.Draw(), then render whatever it is you are rendering (in my case, some debug information).

Advertisements

3 comments

  1. Alex Jordan

    I too have just, JUST encountered this problem, and am looking into it. I’ll get back to you on it if I find an easier solution than using another DrawableGameComponent just for debug info.

    • Joel

      You might be interested in this thread I just found on the XNA Creator’s site:

      http://forums.xna.com/forums/t/49333.aspx

      I haven’t had a chance to check it out yet, but it looks like if you make the call to the base class’s Draw method at the end of the main loop, after you’ve drawn whatever you need, it should call the Draw method on the DrawableGameComponents after that. So, it looks like that could be our answer!

  2. Joel

    Alex: I’ve updated the original post. It looks like changing the location of the base.Draw() in the main rendering loop does indeed do the trick.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s