XNA texture stretching at extreme coordinates
- by Shaun Hamman
I was toying around with infinitely scrolling 2D textures using the XNA framework and came across a rather strange observation.
Using the basic draw code:
spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointWrap, null, null);
spriteBatch.Draw(texture, Vector2.Zero, sourceRect, Color.White, 0.0f, Vector2.Zero, 2.0f, SpriteEffects.None, 1.0f);
spriteBatch.End();
with a small 32x32 texture and a sourceRect defined as:
sourceRect = new Rectangle(0, 0, Window.ClientBounds.Width, Window.ClientBounds.Height);
I was able to scroll the texture across the window infinitely by changing the X and Y coordinates of the sourceRect.
Playing with different coordinate locations, I noticed that if I made either of the coordinates too large, the texture no longer drew and was instead replaced by either a flat color or alternating bands of color. Tracing the coordinates back down, I found the following at around (0, -16,777,000):
As you can see, the texture in the top half of the image is stretched vertically.
My question is why is this occurring? Certainly I can do things like bind the x/y position to some low multiple of 32 to give the same effect without this occurring, so fixing it isn't an issue, but I'm curious about why this happens. My initial thought was perhaps it was overflowing the coordinate value or some such thing, but looking at a data type size chart, the next closest below is an unsigned short with a range of about 32,000, and above is an unsigned int with a range of around 2,000,000,000 so that isn't likely the cause.