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've created a simple program that draws lines from a fixed point to a random point each frame. I wanted to keep each line on the screen. However, it always seems to be cleared each time it draws on the spriteBatch even without GraphicsDevice.Clear(color) being called. What seems to be the problem?

protected override void Draw(GameTime gameTime)
{
   spriteBatch.Begin();
   DrawLine(spriteBatch);
   spriteBatch.End();

   base.Draw(gameTime);
}

private void DrawLine(SpriteBatch spriteBatch)
    {
        Random r = new Random();

        Vector2 a = new Vector2(50, 100);
        Vector2 b = new Vector2(r.Next(0, 640), r.Next(0,480));

        Texture2D filler= new Texture2D(GraphicsDevice, 1, 1, false, SurfaceFormat.Color);
        filler.SetData(new[] { Color.Black });

        float length = Vector2.Distance(a, b);
        float angle = (float)Math.Atan2(b.Y - a.Y, b.X - a.X);
        spriteBatch.Draw(filler, a, null, Color.Black, angle, Vector2.Zero, new Vector2(length,10.0f), SpriteEffects.None, 0f);

    }

What am I doing wrong?

share|improve this question
    
Don't create a new texture each frame! Create it once in LoadContent and reuse it. –  Andrew Russell Sep 17 '12 at 9:30
    
Yes, thanks Andrew. It was there to make it more understandable -- so people won't have to bother figuring out what the variables filler, a, and r are. –  Dave Croman Sep 17 '12 at 9:42
add comment

2 Answers

up vote 3 down vote accepted

That is the default behavior. Imagine if the reverse was the standard: Then all sprites would leave this trail of after-images behind, which is hardly the desired behavior.

What you can do is to have a screen-filling, transparent texture. You set your RenderTarget to that texture, draw your line, then render that texture normally.

From MSDN:

To use a render target, create a RenderTarget2D object with the width, height, and other options you prefer. Then call GraphicsDevice.SetRenderTarget to make your render target the current render target. From this point on, any Draw calls you make will draw into your render target. When you are finished with the render target, call GraphicsDevice.SetRenderTarget to a new render target (or null for the back buffer). Then at any time you can call RenderTarget2D.GetTexture to get the contents of the render target for further processing.

share|improve this answer
    
Note that setting a render target, by default in XNA 4.0, will clear the render target. Also render targets don't keep their contents through a device reset - meaning that a render target alone is not a robust solution. –  Andrew Russell Sep 17 '12 at 9:32
    
Thanks for this info! It works! –  Dave Croman Sep 17 '12 at 9:43
add comment

Just clear every frame and redraw everything - it's the way GPUs are designed to work, and trying tricksy stuff to avoid this will only cause you more and more trouble as time goes on.

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.