0

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.

Itteh Kitteh
  • 437
  • 5
  • 6
Erra
  • 93
  • 6
  • Why does Erra's name not move? Is it not calling `DrawString` or is that supposed to be what you want the final output to be? – AustinWBryan Dec 23 '15 at 05:38
  • @AustinWBryan Erra's name is the main player, I don't know why I wrote "main player" instead of just "Erra", I guess it's a being tired thing. So yes, Erra's name is how I want the final output to be, but the formula I used for Erra's name only works because it's in the center of the viewport (again, I haven't found out how to get the text to scale without coming up with a formula for it, which I haven't been able to yet) – Erra Dec 23 '15 at 05:56

1 Answers1

2

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.

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
  • Thanks a ton, I was way too tired to realize how easy it actually was.. I mean, I already had calculated the scale like this: Vector2 gameScale = new Vector2((float)ScreenGame._Viewport.Width / (float)ScreenGame_Width, (float)ScreenGame._Viewport.Height / (float)ScreenGame_Height); and all I had to do was X *= Scale.X and Y *= Scale.Y (the reason I do multiplication instead of division I guess is because I calculate the scale like so: currentWidth / MaxWidth instead of MaxWidth / currentWidth?), possibly wrong about the calculation, I'm silly tired, but everything works now, so thanks again! – Erra Dec 23 '15 at 14:33
  • I still find it odd though that Draw() takes the scale into account when drawing and scaling, whereas DrawString() needs you to manually tell it to multiply the position with the scale. Strange implementation. – Erra Dec 23 '15 at 14:42
  • Yeah, but they always have a reason for these shorts of things.. and I just noticed that your character name is Erra, and that's your account name. So did you name your character after yourself or vice versa? – AustinWBryan Dec 24 '15 at 04:37
  • Erra is just a nickname I use online :) Also, super embarrassing.. I was completely wrong, Draw() and DrawString() function the same way when it comes to scaling.. here's the function I use for drawing sprites (I had totally forgotten that I had written a custom function for it): private void DrawScaledSprite(Texture2D Sprite, Vector2 Position, Vector2 Scale) { _spriteBatch.Draw(Sprite, new Vector2(Scale.X * Position.X, Scale.Y * Position.Y), null, Color.White, 0f, Vector2.Zero, Scale, SpriteEffects.None, 0f);} – Erra Dec 24 '15 at 14:18
  • Haha oh okay (: And, hey, at least this shows you had done the procedural abstraction part right! – AustinWBryan Dec 24 '15 at 20:45