1

I set the width and height of the window in my game constructor, and then pass the graphics manager to a class to check the value. In the game's initialize function, it doesn't seem to recognize the new width and height values, so it uses the default.

Here's relevant code:

Game1.cs Constructor:

graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferWidth = 1600;
graphics.PreferredBackBufferHeight = 900;
Content.RootDirectory = "Content";
World = new TestWorld(Content, graphics);

Game1.cs Initialize

World.Initialize();

TestWorld Initialize

Debug.WriteLine(Graphics.GraphicsDevice.Viewport.Width);

The TestWorld Initialize outputs the default (800).

I have tried graphics.ApplyChanges(), but it does not resolve the issue.

Shane
  • 113
  • 5
  • 1
    Have you tried to add graphics.ApplyChanges(); after? It should not be mandatory when changing size in the game constructor, but you can still try. – Alexandre Desbiens Jul 29 '14 at 18:34
  • As trivial of an answer that is it is still one. I'd recommend adding it as an answer. – ClassicThunder Jul 29 '14 at 18:36
  • @AlphSpirit I have tried the apply changes in the constructor and the game1 initialize to no avail! – Shane Jul 29 '14 at 18:36
  • @ClassicThunder See the comment from Shane. That's why I didn't post this as an answer, because I knew it was very unlikely to solve the problem. – Alexandre Desbiens Jul 29 '14 at 18:39
  • @Shane Still, there is something fishy about that TestWorld. I have personally never seen such class in XNA... Are you using any other library with your project? Or if it is a custom class, can you please provide us with samples? – Alexandre Desbiens Jul 29 '14 at 18:45
  • @AlphSpirit It's a custom class. This is the first code that's called. I didn't include the world constructor, which just takes the GraphicsDeviceManager parameter and stores it in a field. Then, I use that field in the World Initialize function. – Shane Jul 29 '14 at 18:51
  • 1
    Using Graphics.PreferredBackbufferWidth over Graphics.GraphicsDevice.Viewport.Width works. It looks like the viewport doesn't change until base.initialize gets called. – Shane Jul 29 '14 at 18:56

2 Answers2

2

Please take a look at this old answer of mine that explains the process in detail.

Basically: The members on GraphicsDeviceManager are settings that are only actually applied when ApplyChanges or CreateDevice is called. Note that CreateDevice will get called for you if you are using the built-in Game class.


Regarding additional questions asked in comments: The graphics device (and so GraphicsDevice.Viewport) is not guaranteed to be available until LoadContent gets called (ref).

Although you can usually get away with using it in Initialize, that is not strictly correct (it does not play nice if the graphics device gets reset).


The correct way to get the window/viewport size is described in detail in this answer.

I think you might need to restructure your code so that things get set up in the right order.

Andrew Russell
  • 21,268
  • 7
  • 56
  • 103
0

You need to call graphics.ApplyChanges(); in order to apply your updated settings.

Seta
  • 1,398
  • 1
  • 10
  • 19
  • I've tried this already. Doesn't seem to help. I've added it in the Game1 Constructor and the World Initialize – Shane Jul 29 '14 at 18:39
  • 1
    Strange, it works for me in my Initalize() function. It isn't called immediately, but at the end of my Initalize(), after logging messages to the log file and loading the settings config. Maybe you need to wait a little while before calling it. – Seta Jul 29 '14 at 18:41
  • I wonder if the changes are applied in the base.Initialize() for the game class... Not sure how I would get around that without moving around my functions. – Shane Jul 29 '14 at 18:52