Sign in to follow this  

Best way to handle drawing in XNA?

This topic is 3662 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

So I'm planning my game (as I posted earlier on) and now I have a specific question regarding design... If I have "entities" that need to be drawn that are a couple of classes deep in structure, how do I handle the drawing of them in XNA when I am using SpriteBatch? Would I just hand off a reference to said spritebatch all the way down the line until it finally reaches the entity, at which point it adds itself to the spritebatch? The structure I layed out looks like this: World -> Map -> Layers -> Game Entities And obviously in structure it's best to just delegate tasks to the "lower" end of things so at the top ("world") you can just say "draw yourself, world!" and it goes. So how have people been handling that?

Share this post


Link to post
Share on other sites
Well there are a couple of ways to handle it..

A) Global static spritebatch as suggested
B) Pass a reference of the spritebatch to each gameentity on creation
C) create a global spritemanager which contains the actual textures/sprites to render for each game entity.. then each game entity will pass the spritemanager all the post-transformed information each frame so it can render everything centrally..
D) do A) but make the container class non-static & give every game entity a reference to the top-level Game which owns the container..

Not sure if there are anymore?

Share this post


Link to post
Share on other sites
I myself went for the global SpriteBatch since I only needed one. All the graphics I have are handled by an "AutoGrh" object, which basically loop through the frames of an animation (or if no animation, does nothing) and returns the "Grh" it is currently at, which states the texture and texture coordinates of the frame. I then just pass that to the SpriteBatch. To make a graphic, you just pass the Grh index and animation method when creating it, and grab the Grh of the frame to draw it. Very simple to use.

Of course, this could have a Draw method which adds it to the SpriteBatch automatically, or each map could have its own SpriteBatch, but that all just seems a bit too far. I like it to just tell me in a very simple form what to draw, then have the main Draw use that information so it could be modified on special occasions if needed.

As for best, I would say whatever makes the most sense given the scenario, combined with what you use in the rest of the code. Trying to read code that constantly fluctuates between format, naming conventions, etc gets very annoying since you can't predict what it is or what it does.

Share this post


Link to post
Share on other sites

This topic is 3662 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this