如何使用缩放矩阵

时间:2014-03-08 05:04:10

标签: c# xna monogame

我有一个用我的代码创建的矩阵:

    screenscalex = (float)_graphics.GraphicsDevice.Viewport.Width / 1920f;
    screenscaley = (float)_graphics.GraphicsDevice.Viewport.Height / 1920f;
    ScalingFactor = new Vector3(screenscalex, screenscaley, 1);
    Global.SpriteScale = Matrix.CreateScale(ScalingFactor);

但我无法弄清楚如何使用矩阵缩小我的精灵这是我目前用来缩小它们的代码:

    batch.End();
    batch.Begin(SpriteSortMode.Immediate,null, null, null, null, null, Global.SpriteScale);
    //This is where the background gets drawn
    backgroundsprite = new Sprite(background, Vector2.Zero);
    backgroundsprite.Draw(batch);
    //ive tried this too below
    //batch.Draw(background, new Rectangle(0, 0, graphics.PreferredBackBufferWidth, graphics.PreferredBackBufferHeight), Color.White);

2 个答案:

答案 0 :(得分:0)

在Microsoft提供缩放精灵的示例中,他们使用SpriteBatch.Draw方法的重载,该方法接受Single类型的scale参数。

protected override void Draw(GameTime gameTime)
{
  ...
  // Initialize the batch with the scaling matrix
  spriteBatch.Begin();
  // Draw a sprite at each corner
  for (int i = 0; i < spritepos.Length; i++)
  {
      spriteBatch.Draw(square, spritepos[i], null, Color.White,
          rotation, origin, scale, SpriteEffects.None, depth);
  }
  spriteBatch.End();
  base.Draw(gameTime);
}

Scaling Sprites Based On Screen Size (MSDN)

SpriteBatch.Draw Method (MSDN)

答案 1 :(得分:0)

这个问题似乎出现了很多。不久前我在博客上写了一个快速教程并附上解释。

http://www.craftworkgames.com/blog/monogame-code-snippets/monogame-resolution-independence/

最重要的代码是缩放矩阵,就像你在问题中创建的那样。

var scaleX = (float)GraphicsDevice.Viewport.Width / (float)VirtualScreenWidth;
var scaleY = (float)GraphicsDevice.Viewport.Height / (float)VirtualScreenHeight;

_screenScale = new Vector3(scaleX, scaleY, 1.0f);     

var scaleMatrix = Matrix.CreateScale(_screenScale);

_spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, scaleMatrix);
// your drawing code here
_spriteBatch.End();

但是,这不是全部,所以我建议您阅读完整的教程。