[Solved]Scaling/zooming game window?

To create a render target you should pass in a GraphicsDevice and the size (which in this case would be your native resolution.

new RenderTarget2D(GraphicsDevice, 160, 144);

By default a GraphicsDevice renders to the backbuffer, but you can change that by using

GraphicsDevice.SetRenderTarget(someRenderTargetHere);

If you pass null as a render target the GraphicsDevice will render to the backbuffer again. So in your game instance you’ll want to do something like:

// Initialization
_nativeRenderTarget = new RenderTarget2d(GraphicsDevice, 160, 144);


// Draw call
GraphicsDevice.SetRenderTarget(_nativeRenderTarget);
GraphicsDevice.Clear(_backgroundColor);
// now render your game like you normally would, but if you change the render target somewhere,
// make sure you set it back to this one and not the backbuffer
...
// after drawing the game at native resolution we can render _nativeRenderTarget to the backbuffer!
// First set the GraphicsDevice target back to the backbuffer
GraphicsDevice.SetRenderTarget(null);
// RenderTarget2D inherits from Texture2D so we can render it just like a texture
spriteBatch.Begin(samplerState: SamplerState.PointClamp);
spriteBatch.Draw(_nativeRenderTarget, _actualScreenRectangle);
spriteBatch.End();

_actualScreenRectangle would be a Rectangle like (x: 0, y: 0, width: a * 160, height: a * 144), where a is 1, 2, 3 or whatever scale factor you want.
EDIT: you’ll actually have to set the backbuffer size to the size of this rectangle by using PreferredBackBufferWidth and PreferredBackBufferHeight of your GraphicsDeviceManager. If you don’t do this in your game constructor, but later through settings or what not, you need to call GraphicsDeviceManager.ApplyChanges() for the window to actually resize.

3 Likes