简体   繁体   中英

Scaling a sprite size using a vector2 Monogame C#

so I'm currently working on resolution independence for my game, and I'm testing it out on a sword image. The position changing is working, but whenever I start doing the size I end up with a blank screen.

These are the functions I run to get the new position and size of the sprite.

    private static float CalcRatio(Vector2 size)
    {
        return size.Y / size.X;
    }

    public static Vector2 CalculateNewPos(Vector2 refPos, Vector2 refScreenSize, Vector2 currentScreenSize)
    {
        return new Vector2((refPos.X / refScreenSize.X) * currentScreenSize.X, 
                          (refPos.Y / refScreenSize.Y) * currentScreenSize.Y);
    }

    public static Vector2 CalculateNewSize(Vector2 refSize, Vector2 refScreenSize, Vector2 currenScreenSize)
    {
        float origRatio = CalcRatio(refSize);
        float perW = refSize.X * 100f / refScreenSize.X;
        float newW = perW / 100f * currenScreenSize.X;
        float newH = newW * origRatio;
        return new Vector2(newW, newH);
    }

In the Initialization function in Game1 I run this code:

 graphics.PreferredBackBufferHeight = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height;
 graphics.PreferredBackBufferWidth = GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width;
 swordPosition = CalculateNewPos(swordRefPosition, new Vector2(1920, 1080), new Vector2(GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width, GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height));
 swordSize = CalculateNewSize(swordRefSize, new Vector2(1920, 1080), new Vector2(GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width, GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height));

In the load function I run this:

swordTexture = content.Load<Texture2D>("SOLDIER_Sword");
swordPosition = new Vector2(300, 0);
swordRefSize = new Vector2(557, 490);
swordSize = new Vector2(557, 490);
swordRefPosition = new Vector2(300, 0);
swordColor = Color.White;
sword = new StaticSprite(swordTexture, swordPosition, swordSize, swordColor);

In update everytime the screen resolution changes (I have buttons set to do that) this:

 graphics.PreferredBackBufferHeight = setHeight;
 graphics.PreferredBackBufferWidth = setWidth;
 graphics.ApplyChanges();
 swordPosition = CalculateNewPos(swordRefPosition, new Vector2(1920, 1080), new Vector2(setWidth, setHeight));
 swordSize = CalculateNewSize(swordRefSize, new Vector2(1920, 1080), new Vector2(setWidth, setHeight));

And in draw:

batch.Draw(swordTexture, swordPosition, null, swordColor, 0f, Vector2.Zero, swordSize, SpriteEffects.None, 0f);

Sorry there is so much code, I'm really stumped and can't pinpoint where it's going wrong, so I just included everything that changes the variables.

Thank you so much for taking the time to look through this.

Note that current display mode is only meaningful if you are fullscreen. It's the VIEWPORT that tells you the width/height of spritebatch's projection matrix.

That said, you should really look into Bjarke's post, because you don't usually want to be rescaling every individual item.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM