Hi, I am trying to work out how to change resolutions during a game. I am drawing to a render target which I am then stretching to whatever screen resolution I choose. However if I try to change resolution during gameplay I get problems. The resolution changes, but the render target is being drawn away from where it should, meaning I get a big border at the top and left of the screen.
I run an InitializeViewport() function (below) during startup, and then call it again when I have chosen my new resolution. All resolutions are fine when I start the game with them, but swapping on the fly is where it’s going wrong and I can’t spot the problem. Can anyone help?
public void InitializeViewport()
{
IsFixedTimeStep = GameInfo.info.fixedTimeStep;
graphics.PreferredBackBufferWidth = GameInfo.info.resolutionWidth; // Set screen resolution.
graphics.PreferredBackBufferHeight = GameInfo.info.resolutionHeight;
graphics.SynchronizeWithVerticalRetrace = GameInfo.info.vSync; // Set initial Vsync.
// Create render target for the implementation of viewports.
renderTarget = new RenderTarget2D(GraphicsDevice, GameInfo.info.gameplayWindowWidth, GameInfo.info.gameplayWindowHeight, false, GraphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.Depth24);
// Define default viewport (always whole of game window).
viewport0 = GraphicsDevice.Viewport;
viewport0.Width = GameInfo.info.gameplayWindowWidth;
viewport0.Height = GameInfo.info.gameplayWindowHeight;
// Define viewport 1.
viewport1 = viewport0;
viewport1.X = GameInfo.info.gameplayWindowX;
viewport1.Y = GameInfo.info.gameplayWindowY;
viewport1.Width = GameInfo.info.gameplayWindowWidth;
viewport1.Height = GameInfo.info.gameplayWindowHeight;
// Set full-screen mode
graphics.HardwareModeSwitch = GameInfo.info.hardwareModeSwitch;
graphics.IsFullScreen = GameInfo.info.fullScreen;
// Set window position.
if (!graphics.IsFullScreen)
Window.Position = new Point((GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Width / 2) - (graphics.PreferredBackBufferWidth / 2), (GraphicsAdapter.DefaultAdapter.CurrentDisplayMode.Height / 2) - (graphics.PreferredBackBufferHeight / 2));
// Set frame rate.
SetFrameRate(GameInfo.info.frameRate);
graphics.ApplyChanges();
}