Bob
Bob

Reputation: 861

XNA: What is Viewport?

In terms of 2D I don't understand what a viewport is and what it is used for. I am trying to build a side-scroller and believe I need the viewport for the map scrolling part.

Upvotes: 4

Views: 7144

Answers (2)

Lunin
Lunin

Reputation: 370

The Viewport contains information about the actual current window displaying the game. If you are working in 2D, the main ones you'll be dealing with will be the Width, Height, AspectRatio (saves calculating it yourself), and if you're planning to deploy to 360, the TitleSafeArea (The rectangle safe to draw to on all TV's, older TV's will clip 5-10% of some of the edges)

You probably won't be changing the Width and Height of your window in runtime so if you store an X and a Y for the camera position, you can store the Width and Height of the screen and never have to look at the Viewport again except maybe to place HUD elements within the TitleSafeArea.

Upvotes: 2

Russ Clarke
Russ Clarke

Reputation: 17909

Consider a Tv displaying a scene, the viewport is simply the area of the scene that is captured by the camera.

Another way of thinking about it is the way some cameraman hold up their thumbs and index fingers to 'frame' a scene, or hold up a hollow picture frame, the light that passes through that frame is the viewport.

For a visual example, consider this gif, the green rectangle is the viewport: enter image description here

Upvotes: 9

Related Questions