Viewport tweaks in XNA Game Studio 4.0

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);

Comments (13)

  1. Nelxon says:

    Most of us have already created classes or methods to do this, but itโ€™s really nice to have it already implemented.

  2. David Amador says:

    the new spriteBatch.Draw(cat, GraphicsDevice.Viewport.Bounds, Color.White);

    is really very handy.

    Thanks posting

  3. Ah finally, previously you’d have to write an extension method for this.

  4. John Sedlak says:

    More changes that I can get behind!

    However, I would really like a SpriteBatch.DrawCat method. It would greatly reduce the amount of code I need to write just to draw a glorious cat.

  5. ShawnHargreaves says:

    > However, I would really like a SpriteBatch.DrawCat method.

    I’ve been arguing for some time that if you create a 256×256 Texture2D, but never call SetData on it, we should initialize this to the classic image of my cat.

    That’s not technically even a breaking API change, because we never explicitly specified what the contents of an uninitialized texture are! A photo of a cat strikes me as a valid implementation of "undefined" ๐Ÿ™‚

    I have yet to convince the powers that be that this would be worth the extra 32k of framework assembly size, though ๐Ÿ™‚

  6. Ollhax says:

    The only solution for that is, of course, to make a procedural cat-drawing function. Totally worth the time it would take if you ask me!

  7. ShawnHargreaves says:

    > The only solution for that is, of course, to make a procedural cat-drawing function.

    Or maybe use web services to pull down a random lolcat?

  8. Please put in viewport a swapbuffer like this

    allso presented on the xbox360

    recording of command buffers and viewports and playback function ,,

    so we can record a frame or to and play the frame back with only the visible objects or if objects that are change are updated to the new viewport

  9. ShawnHargreaves says:

    Michael: I don’t understand what command buffer playback has to do with viewports? These seem like entirely unrelated features to me?

    If you have feature requests for future versions, the Connect website is the best place to suggest them.

  10. command buffer ,

    sorry mistake,

    but still a swapbuffer as viewport

    render one frame to the fist viewport , and the second frame to the next viewport

    playback the fist viewport

    wicth meen we have to save all our rendertarget in a special place in the memory

    so that our rendertarget dont get lost

  11. sorry shawn

    more of a memory that are dicated to hold all the rendertarget thrue our render pipeline

    more of a lock memory , so we can acess these rendertarget verry fast

    you see not all rendertarget need to be update evry frame

    in the net compact fromework as special 64mb memory chunck for that,

    so when we render one of the rendertarget it is copyed to the special ram on xbox360 , and when rendering is done ,it well been copy back to the same location in the 64mb chunk as before

    that way we can control the rendertarget/texture , faster

  12. ShawnHargreaves says:


    It would be great if you could try to keep these comments at least somewhat related to the topic of the blog article! It is natural that conversations tend to wander around a little, but blog comments aren’t really the best place to bring up entirely new, unrelated subjects!

    If you have specific feature suggestions you would like to make to the XNA team, the Connect website is the best place for that.

    If you want to start a conversation about rendertargets, I would recommend the forums.

  13. Really liking the sound of all these changes. Just eager to get my hands on the real deal now…

Skip to main content