Domanda

I am a MonoGame developer, and I'm working on a project which allows user to draw with his finger on

the phone screen,I do that by repeating the draw of an image on the finger path:

I used some codes like this:

The LoadConent method

texture = Content.Load<Texture2D>("b3");

renderTarget = new RenderTarget2D( this.GraphicsDevice, this.GraphicsDevice.PresentationParameters.BackBufferWidth, this.GraphicsDevice.PresentationParameters.BackBufferHeight, false, this.GraphicsDevice.PresentationParameters.BackBufferFormat, DepthFormat.None, 0, RenderTargetUsage.PreserveContents);

The Update method

   protected override void Update(GameTime gameTime)
    {
    TouchCollection touches = TouchPanel.GetState();

        foreach (TouchLocation touch in touches)
        {
            if (touch.State == TouchLocationState.Moved)
            {
                GraphicsDevice.SetRenderTarget(renderTarget);
                spriteBatch.Begin();
                spriteBatch.Draw(texture, touch.Position, Color.White);
                spriteBatch.End();
                GraphicsDevice.SetRenderTarget(null);
            }
           }
       base.Update(gameTime);
    }

The draw method

  protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.White);

        // TODO: Add your drawing code here
        spriteBatch.Begin();
        spriteBatch.Draw(renderTarget, Vector2.Zero, Color.White);
        spriteBatch.End();
       base.Draw(gameTime);
    }

I have this result:

enter image description here

So, my question is :

How to speed up the drawing for i get continu sketch.

Thanks

È stato utile?

Soluzione

Try this. First create a method that draws the sprites evently spaced apart like this:

    // increment is how far apart each sprite is drawn
    private void DrawEvenlySpacedSprites(Texture2D texture, Vector2 point1, Vector2 point2, float increment)
    {
        var distance = Vector2.Distance(point1, point2);    // the distance between two points
        var iterations = (int)(distance / increment);       // how many sprites with be drawn
        var normalizedIncrement = 1.0f / iterations;        // the Lerp method needs values between 0.0 and 1.0
        var amount = 0.0f;

        // not sure if this is needed but it's here to make sure at least one
        // sprite is drawn if the two points are very close together
        if(iterations == 0)
            iterations = 1;

        for (int i = 0; i < iterations; i++)
        {
            var drawPoint = Vector2.Lerp(point1, point2, amount);
            _spriteBatch.Draw(texture, drawPoint, Color.White);
            amount += normalizedIncrement;
        }
    }

Then change your Update method to something like this:

    private Vector2? _previousPoint;
    protected override void Update(GameTime gameTime)
    {
        TouchCollection touches = TouchPanel.GetState();

        foreach (TouchLocation touch in touches)
        {
            if (touch.State == TouchLocationState.Moved)
            {
                GraphicsDevice.SetRenderTarget(renderTarget);
                _spriteBatch.Begin();

                if (_previousPoint.HasValue)
                    DrawEvenlySpacedSprites(texture, _previousPoint.Value, touch.Position, 0.5f);

                _spriteBatch.End();
                GraphicsDevice.SetRenderTarget(null);
            }
        }

        if (touches.Any())
            _previousPoint = touches.Last().Position;
        else
            _previousPoint = null;

        base.Update(gameTime);
    }

It should draw many evenly spaced sprites between each touch point giving the effect you're looking for and without the need to speed up rendering (which you can't really do anyway).

Let me know how it goes.. Good luck :)

Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top