Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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!

share|improve this question
add comment

2 Answers

up vote 14 down vote accepted

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.

share|improve this answer
    
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. –  Nick Van Hoogenstyn Feb 17 '11 at 23:59
add comment

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

share|improve this answer
    
Simple and to the point, thanks. –  Nick Van Hoogenstyn Feb 18 '11 at 0:06
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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