I started using a RenderTarget2D in my project so I could use shaders more effectively, but when I went full screen I found that my center viewpoint was no longer centered. It still resizes correctly, but for widescreen monitors it should center the viewport to the middle of the screen. And that was working correctly until I started using a RenderTarget2D. Now, the viewport is being displayed on the left side of the screen instead.
if (graphics.IsFullScreen)
{
// 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);
}
scale = (float)width / (float)Game1.ScreenWidth;
// 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.Black);
}
GraphicsDevice.SetRenderTarget(rendertarget);
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend);
....Draw All of the Game....
GraphicsDevice.SetRenderTarget(null);
spriteBatch.End();
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, null, null, null, brightnessShader, Matrix.CreateScale(scale));
spriteBatch.Draw(rendertarget, Vector2.Zero, Color.White);
Any ideas as to why the viewport is displayed at the left instead of the center?