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 trying to render text (or whatever) off screen. But I have a strange issue where the render target size matches the window size and not the render targets size. Yes, it is a contradiction! Here's the render target:

rendertarget

And here's the code, chopped up and put together to demonstrate what's happening in one place :

UserInterface.SpriteBatch.Begin(SpriteSortMode.Immediate,
                                BlendState.AlphaBlend,
                                SamplerState.PointClamp, 
                                DepthStencilState.None,
                                RasterizerState.CullNone);

RenderTarget2D rt = new RenderTarget2D(UserInterface.GraphicsDevice, 200, 200);
UserInterface.GraphicsDevice.SetRenderTarget(rt);

UserInterface.GraphicsDevice.Clear(Color.Blue);

Rectangle rect = new Rectangle(0, 0, 200, 200);
UserInterface.SpriteBatch.Draw(pixel, rect, Color.White);

renderTarget.GraphicsDevice.SetRenderTarget(null);

UserInterface.SpriteBatch.End();

if (frame == 0)
    rt.SaveAsJpeg(File.OpenWrite("renderTarget.jpg"), rt.Width, rt.Height);

The render target is created at 200x200, I then set the render target on the graphics device. I clear it to blue and draw a pixel at 200x200 (in white). Lastly, I save off the render target.

But why does the sprite batch render the pixel in proportion to the window (just take my word that it is rectSize / WindowSize * RTSize) rather than filling the render target which I believe it should do in this case?

share|improve this question
add comment

1 Answer 1

up vote 7 down vote accepted

When you call SpriteBatch.Begin() with SpriteSortMode.Immediate, the current device settings will be applied to the effect of the SpriteBatch. This also includes the dimension of your current render target (the back buffer, if no specific render target is set). If you then set and draw to your newly generated render target, you will experience the result you described in your question.

To get the result you wish, you just have to set the render target before you call SpriteBatch.Begin(). Something like this should do it:

RenderTarget2D rt = new RenderTarget2D(UserInterface.GraphicsDevice, 200, 200);
Rectangle rect = new Rectangle(0, 0, 200, 200);
UserInterface.GraphicsDevice.SetRenderTarget(rt);
UserInterface.GraphicsDevice.Clear(Color.Blue);

UserInterface.SpriteBatch.Begin(SpriteSortMode.Immediate,
                                BlendState.AlphaBlend,
                                SamplerState.PointClamp, 
                                DepthStencilState.None,
                                RasterizerState.CullNone);
UserInterface.SpriteBatch.Draw(pixel, rect, Color.White);
UserInterface.SpriteBatch.End();

renderTarget.GraphicsDevice.SetRenderTarget(null);
share|improve this answer
add comment

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.