6
\$\begingroup\$

I'm making a 2D game in XNA/Monogame and I'm using Stardew Valley as a visual template (it's got the SNES style I'm going for). I'm having trouble mimicking the feature to change screen resolution and even get the game to draw as I'd like it.

As background, Stardew map tiles are 16x16 pixels. On screen the map is about 30 tiles wide by 16 tall. Naively this would make the screen 480x270 pixels, which is way too small on my 1920x1080 monitor. I'd like the game window to match my screen, but to get the game objects to scale properly.

Is there a proper way to change the screen resolution in XNA? Maybe some scaling factor in the SpriteBatch object that I'm not aware of?

\$\endgroup\$
1
  • \$\begingroup\$ take a look at MonoGame.Extended some neat solutions to already use or learn from. \$\endgroup\$
    – lozzajp
    Apr 24, 2017 at 12:19

1 Answer 1

6
\$\begingroup\$

The easiest way is to set it through the GraphicsDeviceManager.

graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferWidth = 480;
graphics.PreferredBackBufferHeight = 270;

I think setting the graphics to fullscreen after that should scale everything up properly.

graphics.IsFullScreen = false;

Note that if you want your game to work on different monitors you'll need a more robust solution because not all monitors have the same aspect ratio.

Also note you can handle scaling manually by creating a Matrix to pass to SpriteBatch when drawing.

// Somewhere accessible
const int TargetWidth = 480;
const int TargetHeight = 270;
Matrix Scale;

// Somewhere in initialisation
float scaleX = device.PreferredBackBufferWidth / TargetWidth;
float scaleY = device.PreferredBackBufferHeight / TargetHeight;
Scale = Matrix.CreateScale(new Vector3(scaleX, scaleY, 1));

// Somewhere with drawing
protected override void Draw(GameTime gameTime)
{
    SpriteBatch.Begin(SpriteSortMode.Immediate, null, null, null, null, null, Scale);
    Root.Draw(SpriteBatch, gameTime);
    SpriteBatch.End();

    base.Draw(gameTime);
}
\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .