# XNA: Texture wrap with spritebatch

## Recommended Posts

WebsiteWill    134
Hello everyone: According to the XNA documentation I should be able to tile a texture over a sprite by doing something like this
_spriteBatch.Begin(SpriteBlendMode.AlphaBlend, SpriteSortMode.Immediate, SaveStateMode.None);
_mainDialog.Draw(_guiTexture, _spriteBatch, true);
_guiCursor.Draw(_guiTexture, _spriteBatch);
_spriteBatch.End();


However, when I resize any of my sprites, the texture is stretched instead of tiled. Any ideas what I am missing? I've tried samplerstates 1 and 2 as well as putting my SamplerState update lines at varying positions of the code block but it all gives me the same results. TIA, Webby

##### Share on other sites
Dave Hunt    4872
SpriteBatch sets the texture coordinates on the generated quad vertices assuming that the source rectangle is to be stretched across the entire quad. Changing the TextureAddressMode won't have any effect, since the texture coordinates don't change. The only reason SpriteBatch uses Clamp mode is to deal with cases where you've specified a source rectangle that is larger than the texture size.

The only way I know of to get texture tiling is to implement your own textured quad drawing.

Hmmm... Having reread my first paragraph, the last sentence might be a way to get what you are after. I haven't tried it, but you might want to try specifying a source rectangle that is 2x the size of the texture image and see if you get tiling with your current code.

##### Share on other sites
WebsiteWill    134
Nope, it just gives an even more stretched view of the texture. Also, since I'm using this for my interface I can't just specify source rects like that since the same texture is used for very many different rects.

This is odd since according to the links below I should be able to alter graphics device settings after calling batch.Begin as long as I am in Immediate mode for these sprites.

http://blogs.msdn.com/shawnhar/archive/2006/12/14/return-of-the-spritebatch-sorting-part-3.aspx

http://msdn2.microsoft.com/en-us/library/bb313868.aspx

I suppose I could write a custom pixel shader easily enough for this, but it appears that this functionality should be allowed by default.

Stumped,
Webby

##### Share on other sites
sirob    1181
Quote:
 Original post by WebsiteWillI suppose I could write a custom pixel shader easily enough for this, but it appears that this functionality should be allowed by default.

you should be able to use a texture transform matrix to transform the sampling on the textures. However, if you expect to be able to wrap in the middle of the texture - that won't be possible without a shader (or, I don't see how it would be possible). If you want wrapping around the edge of the texture, a texture transform + wrap addressing mode should do.

Hope this helps.

##### Share on other sites
WebsiteWill    134
I suppose I could move all of my background (tileable) textures into separate files. For a GUI I really only see needing 1 or 2 at a time. However, I'd really prefer not to do this since it would cause me to switch textures out for every different GUI element that uses the background texture.

I suppose I may end up having to do something like this anyway once I implement the icons that are used for buttons and other things since I don't see any way that I am going to fit everything inside of a single texture.

Thoughts on this? Is it pretty common these days for games to use multiple textures for a single GUI?

Webby

##### Share on other sites
WebsiteWill    134
I think what I am going to do is, for my background (tileable) sprites I will recalculate their rectangles based on the overall size and actually draw multiple sprites, in a grid to fit the region. There will be a little computation involved to figure out how many sprites are needed and their placements but I don't think it would be that complicated.

I could probably calculate this only when a background element needs to resize instead of every time I draw the scene to help with performance.

Off to work I go,
Webby