10
\$\begingroup\$

I just wanted to know if there was a way to find out the size of the window in XNA. I don't want to set it to a specific size; I would like to know what dimensions it currently displays as automatically. Is there a way to find this information out?

I realize I probably should have found this information out (or set it myself manually) before working on the game, but I'm a novice and am now hoping to work within the dimensions I have already become invested in. Thanks!

\$\endgroup\$
15
\$\begingroup\$

Here are your options:

To get the back-buffer size use:

GraphicsDevice.PresentationParameters.Bounds (for a rectangle) or BackBufferWidth and BackBufferHeight.

You want the back-buffer size if you're doing stuff like setting viewports, taking screenshots, etc.

To get the Viewport, use:

GraphicsDevice.Viewport.Bounds (for a rectangle) or Width and Height.

The size of the viewport is what you want to use when you're actually rendering stuff. Any coordinates you pass to SpriteBatch are in pixel-aligned client space in terms of the viewport: (0,0) for the top left pixel to (width-1,height-1) for the bottom right. If you're doing 3D stuff, projection space goes from (-1,-1) in the bottom left of the viewport to (1,1) in the top right.

You can adjust the size and position of the viewport on screen (for doing effects like split-screen). So, while it is initialised to the size of the back-buffer, it is not necessarily always the same.

If you're doing interface layout stuff, particularly if you will run on the Xbox 360, be aware of Viewport.TitleSafeArea. This tells you what region is definitely visible on screens that may cut off some of the border.

If, for some reason, you're actually working with the game window itself, use Game.GameWindow.ClientBounds.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ Thanks a lot! I should have specified that it was a 2D game but that was nice of you to give both answers. Reading up on Viewports now. \$\endgroup\$ – Nick Van Hoogenstyn Feb 17 '11 at 23:59
2
\$\begingroup\$

GraphicsDevice.Viewport.Bounds - this returns Rectangle2D and it has parameters Width and Height.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.