XNA调整窗口大小而不增加分辨率

1 c# windows xna

我想在更大的窗口上创建一个低分辨率的游戏.(例如,96x54 res在960x540尺寸窗口上).

我该怎么做?有没有办法独立于首选的后缓冲区宽度和高度调整窗口大小?或者我应该保持一个低分辨率的渲染目标,我只是在我调整最近的纹理采样时将其绘制为窗口上的全屏四边形?

提前致谢,

xoorath

lzc*_*zcd 6

我倾向于选择"渲染到纹理"解决方案,以便我可以允许像全屏这样的东西而不会扭曲.

我用来实现这个目的的类通常看起来像:

class VirtualScreen
{
    public readonly int VirtualWidth;
    public readonly int VirtualHeight;
    public readonly float VirtualAspectRatio;

    private GraphicsDevice graphicsDevice;
    private RenderTarget2D screen;

    public VirtualScreen(int virtualWidth, int virtualHeight, GraphicsDevice graphicsDevice)
    {
        VirtualWidth = virtualWidth;
        VirtualHeight = virtualHeight;
        VirtualAspectRatio = (float)(virtualWidth) / (float)(virtualHeight);

        this.graphicsDevice = graphicsDevice;
        screen = new RenderTarget2D(graphicsDevice, virtualWidth, virtualHeight, false, graphicsDevice.PresentationParameters.BackBufferFormat, graphicsDevice.PresentationParameters.DepthStencilFormat, graphicsDevice.PresentationParameters.MultiSampleCount, RenderTargetUsage.DiscardContents);
    }

    private bool areaIsDirty = true;

    public void PhysicalResolutionChanged()
    {
        areaIsDirty = true;
    }

    private Rectangle area;

    public void Update()
    {
        if (!areaIsDirty)
        {
            return;
        }

        areaIsDirty = false;
        var physicalWidth = graphicsDevice.Viewport.Width;
        var physicalHeight = graphicsDevice.Viewport.Height;
        var physicalAspectRatio = graphicsDevice.Viewport.AspectRatio;

        if ((int)(physicalAspectRatio * 10) == (int)(VirtualAspectRatio * 10))
        {
            area = new Rectangle(0, 0, physicalWidth, physicalHeight);
            return;
        }


        if (VirtualAspectRatio > physicalAspectRatio)
        {
            var scaling = (float)physicalWidth / (float)VirtualWidth;
            var width = (float)(VirtualWidth) * scaling;
            var height = (float)(VirtualHeight) * scaling;
            var borderSize = (int)((physicalHeight - height) / 2);
            area = new Rectangle(0, borderSize, (int)width, (int)height);
        }
        else
        {
            var scaling = (float)physicalHeight / (float)VirtualHeight;
            var width = (float)(VirtualWidth) * scaling;
            var height = (float)(VirtualHeight) * scaling;
            var borderSize = (int)((physicalWidth - width) / 2);
            area = new Rectangle(borderSize, 0, (int)width, (int)height);
        }
    }

    public void BeginCapture()
    {
        graphicsDevice.SetRenderTarget(screen);
    }

    public void EndCapture()
    {
        graphicsDevice.SetRenderTarget(null);
    }

    public void Draw(SpriteBatch spriteBatch)
    {
        spriteBatch.Draw(screen, area, Color.White);
    }


}
Run Code Online (Sandbox Code Playgroud)

然后在我的游戏中,初始化趋势看起来像:

  VirtualScreen virtualScreen;

    protected override void Initialize()
    {
        virtualScreen = new VirtualScreen(96, 54, GraphicsDevice);
        Window.ClientSizeChanged += new EventHandler<EventArgs>(Window_ClientSizeChanged);
        Window.AllowUserResizing = true;
        base.Initialize();
    }

    void Window_ClientSizeChanged(object sender, EventArgs e)
    {
        virtualScreen.PhysicalResolutionChanged();
    }
Run Code Online (Sandbox Code Playgroud)

随着对Update的所有重要调用:

protected override void Update(GameTime gameTime)
    {
        virtualScreen.Update();

        base.Update(gameTime);
    }
Run Code Online (Sandbox Code Playgroud)

然后绘制自己的行为:

 protected override void Draw(GameTime gameTime)
    {
        virtualScreen.BeginCapture();


        GraphicsDevice.Clear(Color.CornflowerBlue);
        // game rendering happens here...


        virtualScreen.EndCapture();

        GraphicsDevice.Clear(Color.Black);
        spriteBatch.Begin();
        virtualScreen.Draw(spriteBatch);
        spriteBatch.End();

        base.Draw(gameTime);
    }
Run Code Online (Sandbox Code Playgroud)

有了这个,我基本上可以停止关注分辨率,只关注游戏.