How do I retain previously drawn graphics?

Posted by Cromanium on Game Development See other posts from Game Development or by Cromanium
Published on 2012-09-17T06:36:56Z Indexed on 2012/09/17 9:52 UTC
Read the original article Hit count: 191

Filed under:
|

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?

© Game Development or respective owner

Related posts about c#

Related posts about xna-4.0