Spritebatch Transform Matrix and Fonts

So Im using a 16x16 tilesheet. To make this look good in 1920x1080 I figured I scale the output x4 to 64x64.

Matrix transform = Matrix.CreateScale(4);
_spriteBatch.Begin(SpriteSortMode.Deferred, null, SamplerState.PointClamp, null, null, null, transform);

Then I wanted to draw a siple FPS counter text - and realized that the font also get scaled to x4.

Obviously this can be solved by doing another _spriteBatch.Begin() without the transform - but as far as I know it is considered bad practice to call multiple _spriteBatch.Begin() in the same Draw method.

What to do? =)

Unleash the bigbrains!

No, that’s fine. Reducing the number of batches can improve performance, but a couple of batches per frame are not going to hurt you.

If you really want to combine it into a single batch, you can remove the transform, and scale the sprites individually. Just multiply the position and size by 4 when you draw them.

2 Likes

Yep, and remember to call the text last.

EDIT

Easily forgotten…

I wanted to add, you can use the same SpriteBatch in-series to get interesting effects such as below:

image

Thanks. Could it be the same spriteBatch instance or should it be a seperate. Or perhaps it doesnt matter?

You can use the same spritebatch over and over as far as my experimenting has concluded…

PSEUDOCODE
spritebatch1
spritebatch2


spritebatch1.begin
spritebatch1.draw
spritebatch1.end

spritebatch1.begin
spritebatch1.draw
spritebatch1.end

spritebatch2.begin
spritebatch2.draw
spritebatch2.end

Works fine for me…

Aye, I know it works fine :slight_smile: Just wondering if theres any drawbacks / profit in doing it one way over the other. Perhaps seems like trivial question, but better to learn to do things right for the long run =P

1 Like