Viewport tweaks in XNA Game Studio 4.0

Originally posted to Shawn Hargreaves Blog on MSDN, Wednesday, April 14, 2010

Game Studio 4.0 includes some tweaks to simplify common patterns when working with Viewport objects. We added two constructors:

    public Viewport(int x, int y, int width, int height);
    public Viewport(Rectangle bounds);

And we added a Bounds property to Viewport, Texture2D, and PresentationParameters:

    public Rectangle Viewport.Bounds { get; set; }
    public Rectangle Texture2D.Bounds { get; }
    public Rectangle PresentationParameters.Bounds { get; }

This makes it easier to do things like resetting the viewport to cover the entire surface of a rendertarget:

    GraphicsDevice.Viewport = new Viewport(renderTarget.Bounds);

or scaling a texture to fill the entire viewport:

    spriteBatch.Draw(cat, GraphicsDevice.Viewport.Bounds, Color.White);
Blog index   -   Back to my homepage