# Using a Viewpoint and a RenderTarget2D in XNA

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?

• Some additional info. Commenting out the following lines gives me a centered view. GraphicsDevice.SetRenderTarget(rendertarget); GraphicsDevice.SetRenderTarget(null); spriteBatch.Draw(rendertarget, Vector2.Zero, Color.White); And it also makes the background viewport actually be black, as it should be. As it is now, I'm getting a blue-ish background. Jul 6 '11 at 21:59

Pretty sure every time you change rendertargets the Viewport defaults back to the client. Anyways I wouldn't alter the viewport at all. You don't show us how you create your rendertarget but if its the same size as the viewport you were using just draw everything to the rendertarget and then replace.

spriteBatch.Draw(rendertarget, Vector2.Zero, Color.White);


with

spriteBatch.Draw(rendertarget, new Vector2((ScreenWidth - RenderWidth) / 2, 0), Color.White);


or you can use the source and destination matrices and not have to worry about scaling.

I ended up moving the viewport code to right after I set the render target to null and it worked beautifully without me having to modify the position of every single thing I'd drawn so far.