简体   繁体   English

如何根据窗口大小调整C#XNA游戏中的精灵大小?

[英]How do I resize sprites in a C# XNA game based on window size?

I'm making a game in C# and XNA 4.0. 我正在用C#和XNA 4.0进行游戏。 It uses multiple objects (such as a player character, enemies, platforms, etc.), each with their own texture and hitbox. 它使用多个对象(例如玩家角色,敌人,平台等),每个对象都有自己的纹理和命中框。 The objects are created and drawn using code similar to the following: 使用类似于以下代码的代码创建和绘制对象:

class Object
{
    Texture2D m_texture;
    Rectangle m_hitbox;

    public Object(Texture2D texture, Vector2 position)
    {
        m_texture = texture;
        m_hitbox = new Rectangle((int)position.X, (int)position.Y, texture.Width, texture.Height);
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(texture, m_hitbox, Color.White);
    }
}

Everything works properly, but I also want to allow the player to resize the game window. 一切正常,但我也想允许玩家调整游戏窗口的大小。 The main game class uses the following code to do so: 主游戏类使用以下代码进行操作:

private void Update(GameTime gameTime)
{
    if (playerChangedWindowSize == true)
    {
        graphics.PreferredBackBufferHeight = newHeight;
        graphics.PreferredBackBufferWidth = newWidth;
        graphics.ApplyChanges();
    }
}

This will inevitably cause the positions and hitboxes of the objects to become inaccurate whenever the window size is changed. 每当更改窗口大小时,这将不可避免地导致对象的位置和命中框变得不准确。 Is there an easy way for me to change the positions and hitboxes based on a new window size? 我有一种简单的方法可以根据新窗口大小来更改位置和点击框吗? If the new window width was twice as big as it was before I could probably just double the width of every object's hitbox, but I'm sure that's a terrible way of doing it. 如果新窗口的宽度是以前的两倍,那么我可能只是将每个对象的Hitbox的宽度加倍,但是我确信这是一种糟糕的方法。

Consider normalizing your coordinate system to view space {0...1} and only apply the window dimensions scalar at the point of rendering. 考虑将坐标系标准化以查看空间 {0 ... 1},并且仅在渲染点应用窗口尺寸标量。

View Space to Screen Space Conversion 查看空间到屏幕空间的转换

Pseudo code for co-ordinates: 坐标的伪代码:

x' = x * screenResX
y' = y * screenResY

Similarly for dimensions. 尺寸类似。 Let's say you have a 32x32 sprite originally designed for 1920x1080 and wish to scale so that it fits the same logical space on screen (so it doesn't appear unnaturally small): 假设您有一个最初为1920x1080设计的32x32子画面,并且希望进行缩放,以使其适合屏幕上的相同逻辑空间(因此不会显得异常小):

r = 32 * screenResX' / screenResY
width' = width * r
height' = height * r

Then it won't matter what resolution the user has set. 然后,用户设置的分辨率将无关紧要。

If you are concerned over performance this may impose, then you can perform the above at screen resolution change time for a one-off computation. 如果您担心这可能会影响性能,则可以在屏幕分辨率更改时执行上述操作,以进行一次性计算。 However you should still always keep the original viewspace {0...1}. 但是,您仍应始终保留原始视区{0 ... 1}。

Collision Detection 碰撞检测

It's arguably more efficient to perform CD on screen space coordinates 在屏幕空间坐标上执行CD可以说效率更高

Hope this helps 希望这可以帮助

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

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