user2236165
user2236165

Reputation: 741

Redraw only one texture, not all of them

I'm a little noob with XNA and I've encountered a problem. I have one texture called "dot", that I use to draw a line across the screen. Then I have a texture that I call "tool", that I want to move around when I touch it with my finger. The problem I have is that for the tool to be cleared and redrawn I call the graphicsDevice.Clear-method in XNA. But when I do that, my lines also disappears. So my question is how can i move around the tool, without causing my lines to disappear? Is there a way to only redraw the tool, without redrawing the lines? Heres a snippet of my code, first update-method:

protected override void Update (GameTime gameTime)
    {
        float elapsed = (float)gameTime.ElapsedGameTime.TotalSeconds;
        TouchCollection touchCollection = TouchPanel.GetState ();

        foreach (TouchLocation tl in touchCollection) {
            if ((tl.State == TouchLocationState.Pressed) || (tl.State == TouchLocationState.Moved)) {
                toolPos = touchCollection [0].Position;
            }
        }
        base.Update (gameTime);
    }

And the draw-method:

        protected override void Draw (GameTime gameTime)
    {
        graphics.GraphicsDevice.Clear (Color.Black);
        newVector = nextVector (oldVector);

        spriteBatch.Begin ();
        DrawEvenlySpacedSprites (dot, oldVector, newVector, 0.9f);
        spriteBatch.Draw (tool, toolPos, Color.White);
        spriteBatch.End ();

        oldVector = new Vector2 (newVector.X, newVector.Y);
        base.Draw (gameTime);
    }

Upvotes: 0

Views: 102

Answers (1)

rot13
rot13

Reputation: 96

Try using a RenderTarget2D with RenderTargetUsage.PreserveContents. If you didn't use render targets yet, here's a tutorial: http://rbwhitaker.wikidot.com/render-to-texture

All you have to do is to specify RenderTargetUsage.PreserveContents in target's constructor, draw dots to it, and draw the target and tool to the screen. It should look like this:

protected override void Draw (GameTime gameTime)
{
    graphics.GraphicsDevice.Clear (Color.Black);
    newVector = nextVector (oldVector);

    GraphicsDevice.SetRenderTarget(renderTarget);
    spriteBatch.Begin ();
    DrawEvenlySpacedSprites (dot, oldVector, newVector, 0.9f);
    spriteBatch.End ();

    GraphicsDevice.SetRenderTarget(null);
    spriteBatch.Begin();
    spriteBatch.Draw(renderTarget, new Vector2(), Color.White);
    spriteBatch.Draw (tool, toolPos, Color.White);
    spriteBatch.End()

    oldVector = new Vector2 (newVector.X, newVector.Y);
    base.Draw (gameTime);
}

Upvotes: 1

Related Questions