我正在开发一款 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)是游戏和矢车菊蓝色区域。我想要的是让游戏缩放以填充矢车菊蓝色区域。
最佳答案
使用视口(viewport) http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.viewport_members.aspx
关于c# - 在 XNA 游戏中无需拉伸(stretch)即可全屏显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4787829/