我有一个我正在研究的2D游戏,它的宽高比为4:3.当我在宽屏显示器上将其切换到全屏模式时,它会拉伸.我尝试使用两个视口给游戏不应该伸展的黑色背景,但这使得游戏的大小与以前相同.我无法让它填满本应举行整个游戏的视口.
如果没有拉伸而没有我需要修改游戏中的每个位置和绘制语句,我怎么能让它全屏?我用于视口的代码如下.
// 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);
Run Code Online (Sandbox Code Playgroud)
下图显示了屏幕的外观.两侧的黑色是我想要的(并且来自第一个视口),第二个视口是游戏和矢车菊蓝色区域.我想要的是让游戏扩展以填充矢车菊蓝色区域.

使用视口http://msdn.microsoft.com/en-us/library/microsoft.xna.framework.graphics.viewport_members.aspx