I know this question has been asked many times before. However, all solutions I have found after over an hour of googling are essentially the same thing. Everyone says that
The GraphicsDevice.Viewport
width and height by default on my computer is 800x480, try setting a size above that that will be noticeable - like 1024x768.
graphics.PreferredBackBufferHeight = 768;
graphics.PreferredBackBufferWidth = 1024;
graphics.ApplyChanges();
The above code in Initialize
was sufficient to expand the window for me.
It is frustrating that (as you say) "A lot of people say that the ApplyChanges() call is not necessary, and equally as many say that it is" -- the fact of the matter is that it depends on what you are doing and where you are doing it!
(How do I know all this? I've implemented it. See also: this answer.)
Do this in your constructor (obviously if you rename Game1
, do it in your renamed constructor!)
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferHeight = 600;
graphics.PreferredBackBufferWidth = 800;
}
// ...
}
And do not touch it during Initialize()
! And do not call ApplyChanges()
.
When Game.Run()
gets called (the default template project calls it in Program.cs
), it will call GraphicsDeviceManager.CreateDevice
to set up the initial display before Initialize()
is called! This is why you must create the GraphicsDeviceManager
and set your desired settings in the constructor of your game class (which is called before Game.Run()
).
If you try to set the resolution in Initialize
, you cause the graphics device to be set up twice. Bad.
(To be honest I'm surprised that this causes such confusion. This is the code that is provided in the default project template!)
If you present a "resolution selection" menu somewhere in your game, and you want to respond to a user (for example) clicking an option in that menu - then (and only then) should you use ApplyChanges
. You should only call it from within Update
. For example:
public class Game1 : Microsoft.Xna.Framework.Game
{
protected override void Update(GameTime gameTime)
{
if(userClickedTheResolutionChangeButton)
{
graphics.IsFullScreen = userRequestedFullScreen;
graphics.PreferredBackBufferHeight = userRequestedHeight;
graphics.PreferredBackBufferWidth = userRequestedWidth;
graphics.ApplyChanges();
}
// ...
}
// ...
}
Finally, note that ToggleFullScreen()
is the same as doing:
graphics.IsFullScreen = !graphics.IsFullScreen;
graphics.ApplyChanges();