XNA 3.0 to 4.0 conversion causes a viewport 'merging' to go awry

Go To StackoverFlow.com

0

I have an old XNA 3.1 game with a 2D camera, that I recently converted to XNA 4.0.
I had the camera zoom by creating a new viewport, setting the bounds to the camera width/height, and then kinda 'merging' them like this:

Viewport viewport = new Viewport();
viewport.X = 0;
viewport.Y = 0;
viewport.Width = camera.DisplayWidth;
viewport.Height = camera.DisplayHeight;

Viewport priorViewport = this.GraphicsDevice.Viewport;
this.GraphicsDevice.Viewport = viewport;
GraphicsDevice.Clear(Color.Black);
DrawGameLayer(gameTime, "PreContent");
this.GraphicsDevice.Viewport = priorViewport;

However, when the new viewport's resolution becomes greater than the graphics device's viewport's resolution (on a zoom out) then it blows up on line 7, with:

The viewport is invalid. The viewport cannot be larger than or outside of the current render target bounds. The MinDepth and MaxDepth must be between 0 and 1. Parameter name: value

This worked perfectly before, but obviously is not the right way to do things now. Is there a good way to quickly fix this? Or am I stuck having to completely change how I zoom in and out?

2012-04-04 05:20
by user961794


1

There is a lot of examples for 2d cameras in xna... is the first time I see that kind of code...

viewport is not intended to do zooms... it defines a portion of screen to be drawn.

spritebatch.Begin(...,...,...,..., transform) is what you need to do camera transforms

your transform matrix can be similar to this....

Matrix CameraTransform = Matrix.CreateTranslation(-CameraPosition) 
                       * Matrix.CreateRotationZ(rotation) 
                       * Matrix.CreateScale(Zoom) 
                       * Matrix.CreateTranslation(Viewport.Center);
2012-04-05 01:41
by Blau
Ads