Reputation: 33
My understanding of Viewport is that it crops an area of the screen at the position and dimensions specified. I have a large 2D tile map which requires cropping to display only inside a smaller square area of the screen, so as to view only that area of the larger map. However, after implementing a viewport with the following code, any sprite drawn while the viewport is in effect is highly distorted, as if the entire original screen dimensions are scaling down to fit the dimensions of the viewport.
public override void Draw(GameTime gameTime_) {
SpriteBatch spriteBatch = screenManager.spriteBatch;
spriteBatch.Begin(SpriteSortMode.Immediate,BlendState.AlphaBlend,SamplerState.PointClamp,DepthStencilState.Default,RasterizerState.CullNone);
//draw panel
spriteBatch.Draw(minimapPanelGfx,new Vector2(panelX,panelY),Color.White);
//set up and activate viewport
Viewport mapView = new Viewport(panelX+88,panelY+55,260,260);
Viewport origView = TQGame.graphics.GraphicsDevice.Viewport;
TQGame.graphics.GraphicsDevice.Viewport = mapView;
//draw minimap (simplified as example)
spriteBatch.Draw(minimapGfx,new Vector2(panelX+100,panelY+100),Color.White);
//restore original fullscreen viewport
TQGame.graphics.GraphicsDevice.Viewport = origView;
//
spriteBatch.End();
}
Although the desired map display should remained unscaled, the viewported map display is condensed into the left upper side of the viewport. All I wish to achieve is to draw only the area of the full-scale map that falls within the viewport borders. Any help with this issue will be greatly appreciated.
--
SOLVED: It occurred to me to place the viewport code outside the SpriteBatch code and now it's working as intended. :)
Upvotes: 1
Views: 96
Reputation: 33
It occurred to me to place the viewport code outside the SpriteBatch code and now it's working as intended. :)
Upvotes: 2