Reputation: 111
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?
Upvotes: 0
Views: 420
Reputation: 5762
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);
Upvotes: 1