Jump to content
  • Advertisement
Sign in to follow this  
WebsiteWill

XNA SpriteBatch question

This topic is 4010 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

Does anyone know if the texture is switched once you call SpriteBatch.End IF you'd called Draw on some sprites and also called DrawString which uses a spritefont and presumably a texture with the font on it? I have a texture for my UI and I am using spritefonts and I'm curious what XNA is doing in the background. The reason being is I've decided to move away from SpriteBatch for my sprites and I have a choice of continuing to use a SpriteBatch for text (because SpriteBatch for text works really well) or to implement my own. With my current code I am batching UI "sprites" and flushing the entire batch in one single draw call. But it seems like I won't be able to do this with text since I'd have to swap the texture out to render the text which would likely kill my performance -- I'd essentially have to swap textures for every UI element. Thoughts? Thanks, Webby

Share this post


Link to post
Share on other sites
Advertisement
I just had an idea that I wanted to run by you all.

If I make my UI texture 2048x2048 in size I think it would be plenty large enough to store a few bitmap fonts, the ui elements (with animated frames) as well as the icons that I would need.

Now however the tradeoff would be in sending a large 2048x2048 image to the graphics card every frame. I don't think that would matter much at all though.

What do you guys think?

Share this post


Link to post
Share on other sites
SpriteBatch has different sorting techniques that you specify in an overloaded version of the method. There are various ones that all behave differently. One sends each sprite as it's called, some sort by texture and/or depth, and some do other things. Check it out: http://msdn2.microsoft.com/en-us/library/bb195102.aspx

As for using the large texture, go for it. Textures are stored (well, can be and most often are) on the graphics card anyway so you wouldn't be sending it each frame; it would already be there.

Share this post


Link to post
Share on other sites
One thing I would have to do for that to work is to designate a certain portion of the texture to font(s) so that my UI elements ALWAYS start at a certain XY coordinate. So font 1 would start at 0,0 and go to 300,300. Font 2 starts at 0,300 and goes to 300, 600. Something like that. All UI elements are in 300,0 to x, x.

I was playing with the FontGen in the XNAExtras package found online and so far I find that if you export large fonts (starting around 25px) you'll get multiple images instead of a single one. I am trying to figure out how to force it to simply use a larger single image instead.

Another idea I had was to store the UI and fonts in a Texture3D object where the first layer is th UI parts and each following layer would be a font object. Writing a manager to keep track on what font is on each layer would be a breeze but again I'm not sure how XNA works with Texture3D and I'll have to research how to write the shader for this since individual draw calls to my UI components would require different texture layers.

So if you have any info on a method similar to this I'm all ears. I'll keep searching for anything I can find until I come across a solution I like.

Thanks for the info,
Webby

Share this post


Link to post
Share on other sites
the trade off between storing that huge map in memory and the microseconds it takes to swap out a small texture i think would make it a far better solution to simply use different textures.

Share this post


Link to post
Share on other sites
Even if you are swapping out those two images many times per frame? If this was a one or two time swap I'd not care but I'd have to swap it every time I draw a new UI control that had text on it.

Webby

Share this post


Link to post
Share on other sites
I don't understand what you mean by swapping the textures. If you're drawing all the textures in one call and then the text afterwards in separate calls, why not just use SpriteFonts?

Share this post


Link to post
Share on other sites
It doesn't work like that.

Imagine you have a window with a caption and button in that window also with a caption.

For everything to appear correctly with alpha blending you have to do
Window.Draw()
Window.Caption.Draw()
Window.Button.Draw()
Window.Button.Caption.Draw()

So you see, everytime I'd need to draw text I would have to switch out the texture for the text. If I keep each font on a separate image then I'll have to swap a bunch more.

Webby

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!