Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing RenderTarget Results in Purple Screen?

I'm attempting to change RenderTargets at runtime, so I can draw some elements at runtime, manipulate them and then finally draw the texture to the screen. Problem is, the screen turns purple if I change the RenderTarget at runtime. Here's the code I've got in Draw:

        RenderTarget2D tempTarget = new RenderTarget2D(GraphicsDevice, 128, 128, 1,
            GraphicsDevice.DisplayMode.Format, GraphicsDevice.PresentationParameters.MultiSampleType,
            GraphicsDevice.PresentationParameters.MultiSampleQuality, RenderTargetUsage.PreserveContents);

        GraphicsDevice.SetRenderTarget(0, tempTarget);
        GraphicsDevice.Clear(ClearOptions.Target, Color.SpringGreen, 0, 0);
        GraphicsDevice.SetRenderTarget(0, null);

It doesn't seem to matter how I create the RenderTarget, if I do it at runtime (and I do need to create in-memory textures at runtime and draw on them with SpriteBatch) it results in an entirely purple screen. What can I do to fix this?

like image 504
jasonh Avatar asked Jul 17 '10 06:07

jasonh


3 Answers

It looks like the best option is to create the RenderTarget somewhere other than Draw, draw to it during Update, save the resulting texture (and manipulate as necessary) then draw that texture during Draw.

like image 67
jasonh Avatar answered Nov 06 '22 12:11

jasonh


I know this is late, but the solution is to write to the RenderTarget BEFORE you clear the screen and beginning drawing your other items.

protected override void Draw(GameTime gameTime)
{
     GraphicsDevice.SetRenderTarget(_renderTarget);

     //...
     //Perform Rendering to the specified target
     //...

     GraphicsDevice.SetRenderTarget(null);

     GraphicsDevice.Clear(Color.CornflowerBlue);

     //...
     //Code that draws to the users screen goes here
     //...
}

This should prevent you from rendering in the Update method as suggested by others, which is counter-intuitive in many aspects.

like image 2
Michael Aquilina Avatar answered Nov 06 '22 12:11

Michael Aquilina


When spritebatch.End() is called objects are written to the backbuffer or in your case to tempTarget. To make the texture,

  • change the target
  • call begin
  • call all of the draws
  • end the spritebatch
  • set target back to null

then use the render2d

like image 1
Christopher Ross Avatar answered Nov 06 '22 12:11

Christopher Ross