3

Right now I'm tiling a Texture2D with 2 for-loops similar to an example from the MonoGame samples.

I was doing some reading, and I was seeing that using power of two textures (2, 4, 8, 16, 32, etc. on width & height) can be tiled with one SpriteBatch.Draw call.

Is this supported with MonoGame on iOS?

I gave it several tries, and no matter what it merely stretches the image instead of tiling it.

I am using SamplerState.LinearWrap on my SpriteBatch.Begin(), and tried using a 2048x128 png and tried it 1/4 size at 512x32, but with no luck. (Using large sizes, b/c our game runs at 2400xSomething zoomed out, b/c you can zoom in with the camera by 2.5 multiplication)

3
  • 1
    What is the source rectangle you provided to the draw method? You say it stretches the image ... which sounds like you supply a rect that contains the whole image.
    – dowhilefor
    Nov 13, 2011 at 23:37
  • I'm going to try this out here shortly. One issue, is that we use an image strip that is also tiled (a rain effect). This probably wouldn't work in tandem, right? It needs to tile in the X/Y directions and has 4 frames that simulate the rain falling by moving the source rectangle across the image strip. I guess we'd have to use a different Texture2D for each frame, right? (Assuming sizing the source rectangle works for me) Nov 14, 2011 at 13:26
  • I think my problem was the XNA samples here: msdn.microsoft.com/en-us/library/… didn't explain the source rectangle very well, @dowhilefor, post an answer and I'll mark it accepted. (Maybe elaborate on how to tile and animate with an image strip at the same time) Nov 14, 2011 at 13:57

1 Answer 1

2

You can use the SourceRectangle parameter in the draw method. To define what part of the Texture you want to display. Lets say you have a 128x128 Texture. If you supply Rect(0, 0, 128, 128) you tell the draw method to use the whole texture, the same if you would pass null to the draw method. If you supply Rect(0, 0, 64, 64) you would use the upper left part of the texture. Your sprite will display this portion, no matter how big the sprite itself is. So if your sprite is drawn with the size of 128x128 the 64x64 texture part would be scaled.

Now you can use that for animations. If you store in your texture a sequence of animation like this, you just need to recalc the source rectangle everytime you want to display the next image in your sequence.

Besides that, you could pass in a bigger value, than your source texture. XNA now needs to wrap or clamp your texture. That way you can achieve a simple tiling. If you need more than that my guess is you need to use a manual approach, like your foreach loops. Please note that Wrap is only supported if you use power of two textures.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.