简体   繁体   English

使用vector2 Monogame C#缩放精灵大小

[英]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: 在Game1的Initialization函数中,运行以下代码:

 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. 正是VIEWPORT可以告诉您spritebatch投影矩阵的宽度/高度。

That said, you should really look into Bjarke's post, because you don't usually want to be rescaling every individual item. 就是说,您应该真正关注Bjarke的帖子,因为您通常不希望重新缩放每个项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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