1
\$\begingroup\$

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.

\$\endgroup\$
7
  • 1
    \$\begingroup\$ 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. \$\endgroup\$ Jul 29, 2014 at 18:34
  • \$\begingroup\$ As trivial of an answer that is it is still one. I'd recommend adding it as an answer. \$\endgroup\$ Jul 29, 2014 at 18:36
  • \$\begingroup\$ @AlphSpirit I have tried the apply changes in the constructor and the game1 initialize to no avail! \$\endgroup\$
    – Shane
    Jul 29, 2014 at 18:36
  • \$\begingroup\$ @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. \$\endgroup\$ Jul 29, 2014 at 18:39
  • 1
    \$\begingroup\$ Using Graphics.PreferredBackbufferWidth over Graphics.GraphicsDevice.Viewport.Width works. It looks like the viewport doesn't change until base.initialize gets called. \$\endgroup\$
    – Shane
    Jul 29, 2014 at 18:56

2 Answers 2

2
\$\begingroup\$

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.

\$\endgroup\$
0
\$\begingroup\$

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

\$\endgroup\$
3
  • \$\begingroup\$ I've tried this already. Doesn't seem to help. I've added it in the Game1 Constructor and the World Initialize \$\endgroup\$
    – Shane
    Jul 29, 2014 at 18:39
  • 1
    \$\begingroup\$ 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. \$\endgroup\$
    – Seta
    Jul 29, 2014 at 18:41
  • \$\begingroup\$ 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. \$\endgroup\$
    – Shane
    Jul 29, 2014 at 18:52

You must log in to answer this question.

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