I'm going to try to do this in reverse fashion and skip straight to the punch line, and then give the back story afterward:

Is it possible to, after drawing a scene to a RenderTarget2D, only draw a select portion of the RenderTarget2D, if I don't want the entire thing?

I'm using xTile to manage world data in my game (it's a great piece of work, colinvella [xTile's author] has made an amazing product), and for the most part it works great. xTile supports parallax effects in its layers to add some wonderful depth to 2d scenes, which was great, until I implemented a dynamic split-screen system into my game. Wanted to make a co-op game that wouldn't require players to be in close proximity to each other, so I made it so that if the players separate too far apart, the singular full-screen viewport 'snaps-apart', and is replaced by two split-screen viewports, which then smoothly transition to their respective player targets. The effect is pretty smooth aside from the part where the parallax backgrounds become skewed once the viewports split, because xTile's ratio for handling parallax effects is dependent upon viewport size. This is unfortunate, because the effect would otherwise be really snazzy, but the backgrounds become pretty heavily affected when the game goes from single-viewport to multi-viewport. So, Colinvella suggests using rendertargets to record the scene at full viewport size, and then only drawing a portion of it. But as far as I can tell, that isn't even possible? That being said, I've never even used render targets before, so I'm still learning, hence the question here.


1 Answer 1


Is it possible to, after drawing a scene to a RenderTarget2D, only draw a select portion of the RenderTarget2D, if I don't want the entire thing?

I believe so.

This is mostly speculation (with a bit of logic thrown in) since I haven't tried any of this. However, RenderTarget2D inherits from Texture2D. That being said, if you only wanted to draw a portion of a particular render target, you should be able to cast the render target to a texture, then draw that texture with a sprite batch instance with a source rectangle parameter. Something like:

Texture2D texture = (Texture2D) myRenderTarget2D;
SpriteBatch.Draw( texture, destRect, sourceRect, Color.White );
  • \$\begingroup\$ So obvious! I'll give this a whirl and see what happens. I've come so far along in my game and have learned so many things, when I begin to think about implementing new things, my brain is like, "OMG WTF ARE WE DOING?!" and then I forget all the basics XD. Thanks again. \$\endgroup\$ Sep 21, 2012 at 23:40
  • 3
    \$\begingroup\$ No need to cast since myRenderTarget2D extends Texture2D you can do SpriteBatch.Draw(myRenderTarget2D, destRect, sourceRect, Color.White); \$\endgroup\$ Sep 22, 2012 at 4:43
  • \$\begingroup\$ If you require more flexibility or performance you can also use a stencil buffer: msdn.microsoft.com/en-us/library/bb976074.aspx edit: see this question for a better explanation gamedev.stackexchange.com/questions/3770/… \$\endgroup\$
    – Roy T.
    May 4, 2014 at 20:40

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .