Take the 2-minute tour ×
Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

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.

share|improve this question

1 Answer 1

up vote 0 down vote accepted

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 );
share|improve this answer
    
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. –  TheBroodian Sep 21 '12 at 23:40
    
You were absolutely correct, thank you sir. –  TheBroodian Sep 22 '12 at 0:43
3  
No need to cast since myRenderTarget2D extends Texture2D you can do SpriteBatch.Draw(myRenderTarget2D, destRect, sourceRect, Color.White); –  ClassicThunder Sep 22 '12 at 4:43
    
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/… –  Roy T. May 4 at 20:40

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.