I have a 2D game I'm working on, it's 4: 3 aspect ratio. When I switch it to full screen mode on my widescreen monitor, it stretches. I tried using two viewports to give a black background where the game should not stretch, but this left the game in the same size as before. I could not get it to fill the viewing area, which was supposed to contain the entire game.
How can I make it work in full screen mode without stretching and without me to change each position and make an expression in the game? The code I use for viewports is below.
// 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);
The following illustration shows what the screen looks like. Black on the sides is what I want (and from the first viewport), and the second viewport is the game and the blue is cornflower blue. I want to get the scale of the game to fill the blue area of โโthe cornflower.

c # xna
Califer
source share