Search code examples
c#xnaviewportmonogamedrawstring

How do I make DrawString() take the current viewport into account when drawing, like Draw() does?


Currently, all my textures are being scaled and move into the right position when the viewport that I draw them in changes size or position (I don't have to do any calculations myself to do this).

However, when I use DrawString() (while still in the same viewport), and the viewport changes size or position, the text follows no logic that I can figure out. It scales as expected, but it moves in a very weird way.

Here's

a gif showing how it currently works

(to get the main player's name to move correctly when scaling down, I came up with this bad "formula": X -= Viewport.X / 2.15f. Y -= Viewport.X / 3.2f)

Now, is there any way to make DrawString() work like Draw() does when it comes to scaling with viewports?

The way I've got it set up now is:

_spriteBatch.GraphicsDevice.Viewport = ScreenGame.Viewport;
// Draw tile sprites
// Draw player sprites
// Draw text

My apologies in advance if I've forgotten to mention something relevant.


Solution

  • On the DrawString() try

    x /= Scale.X;
    y /= Scale.Y;
    

    This is of course pseudo code. I think if you can find the new scale and store it in a struct or something, you can do this to scale the text's location properly.