c# - 在 XNA 游戏中无需拉伸(stretch)即可全屏显示

标签 c# xna

我正在开发一款 2D 游戏,它的纵横比为 4:3。当我在宽屏显示器上将它切换到全屏模式时,它会拉伸(stretch)。我尝试使用两个视口(viewport)为游戏不应延伸到的位置提供黑色背景,但这使游戏的大小与以前相同。我无法让它填满本应容纳整个游戏的视口(viewport)。

我怎样才能让它在不拉伸(stretch)的情况下全屏显示,而且我不需要修改游戏中的每个位置和绘图语句?我用于视口(viewport)的代码如下。

            // set the viewport to the whole screen
            GraphicsDevice.Viewport = new Viewport
            {
                X = 0,
                Y = 0,
                Width = GraphicsDevice.PresentationParameters.BackBufferWidth,
                Height = GraphicsDevice.PresentationParameters.BackBufferHeight,
                MinDepth = 0,
                MaxDepth = 1
            };

            // clear whole screen to black
            GraphicsDevice.Clear(Color.Black);

            // figure out the largest area that fits in this resolution at the desired aspect ratio
            int width = GraphicsDevice.PresentationParameters.BackBufferWidth;
            int height = (int)(width / targetAspectRatio + .5f);
            if (height > GraphicsDevice.PresentationParameters.BackBufferHeight)
            {
                height = GraphicsDevice.PresentationParameters.BackBufferHeight;
                width = (int)(height * targetAspectRatio + .5f);
            }
            //Console.WriteLine("Back:  Width: {0}, Height: {0}", GraphicsDevice.PresentationParameters.BackBufferWidth, GraphicsDevice.PresentationParameters.BackBufferHeight);
            //Console.WriteLine("Front: Width: {0}, Height: {1}", width, height);

            // set up the new viewport centered in the backbuffer
            GraphicsDevice.Viewport = new Viewport
            {
                X = GraphicsDevice.PresentationParameters.BackBufferWidth / 2 - width / 2,
                Y = GraphicsDevice.PresentationParameters.BackBufferHeight / 2 - height / 2,
                Width = width,
                Height = height,
                MinDepth = 0,
                MaxDepth = 1
            };

            GraphicsDevice.Clear(Color.CornflowerBlue);

下图显示了屏幕的外观。两侧的黑色是我想要的(来自第一个视口(viewport)),第二个视口(viewport)是游戏和矢车菊蓝色区域。我想要的是让游戏缩放以填充矢车菊蓝色区域。

Not correctly scaling

最佳答案

关于c# - 在 XNA 游戏中无需拉伸(stretch)即可全屏显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4787829/

相关文章:

c# - 什么时候归一化一个向量?

c# - Monogame C# : How can I go about creating a fixed grid of images that fit uniformly in each cell?

c# - 检查一个点是否在旋转的矩形内

c# - 有没有办法在 asp 图像控件上显示动态生成的位图?

c# - 关于静态变量如何在 ASP.NET 中工作的简单问题?

c# - 我可以为 .NET Windows 窗体窗口中的工具提示设置无限的 AutoPopDelay 吗?

c# - [C#][XNA 3.1] 如何在一个 Windows 窗体中托管两个不同的 XNA 窗口?

C#枚举题自动转换==显式枚举转换?

c# - Xamarin 表单 DisplayAlert 在从委托(delegate)调用的函数调用时不显示

windows - Windows 上的 XNA 网络