1

我的实验有问题。我正在尝试制作一种方法,将一个融合Texture2D到另一个Texture2D中,然后重新加载Texture2D,这次将粘贴的纹理移动到新位置。我计划在以后的项目中完善这种方法,我将聚光灯粘贴到深色背景上以“照亮”其背后的风景。虽然我设法使粘贴方法完美运行,但我的计划的第二部分遇到了一些重大问题。我将粘贴的纹理设置为Vector2,其中 X 成员每帧增加 1,从而沿着背景拖动粘贴的纹理。但是,背景不会在每一帧之后重置,而是在上一张图像所在的背景上留下一个拖动标记。在过去的半小时里,我一直在努力解决这个问题,方法是重置背景纹理以使其在粘贴其他图像时再次清洁。

这是我的代码。

namespace CopyTest
{
    public class Image
    {
        public Texture2D Bitmap;
        public Vector2 Position;
        public String BitmapName;
        public Rectangle Viewport;
        public Color Tint = Color.White;
        public void ResetViewportDimensions()
        {
            this.Viewport = new Rectangle(this.Viewport.X, this.Viewport.Y, Bitmap.Width, Bitmap.Height);
    }
    public void Draw(SpriteBatch Target)
    {

    }
    public void FinalizeBitmap(ContentManager Target)
    {
        this.Bitmap = null;
        this.Bitmap = Target.Load<Texture2D>(this.BitmapName);
    }
    public Image(Vector2 Position, String BitmapName)
    {
        this.BitmapName = BitmapName;
        this.Position = Position;
    }
}

public class Game1 : Microsoft.Xna.Framework.Game
{
    GraphicsDeviceManager graphics;
    SpriteBatch spriteBatch;

    public Image Rainbow = new Image(new Vector2(0, 0), "Rainbow");
    public Image Eye = new Image(new Vector2(0, 0), "Glow");
    public Image PrintOut = new Image(new Vector2(0, 0), "Rainbow");

    public Vector2 Destination = new Vector2(100, 50);

    public void PrintTo(Image Source, Image Target, Vector2 Location)
    {
        Color[] A = new Color[Source.Bitmap.Width * Source.Bitmap.Width];
        Color[] B = new Color[Target.Bitmap.Width * Target.Bitmap.Width];

        int Y = 0;
        int X = 0;

        Source.Bitmap.GetData(A);
        Target.Bitmap.GetData(B);
        for (int i = 0; i < A.Length; i++)
        {
            B[(int)Location.X + X + (((int)Location.Y + Y) * Target.Bitmap.Width)] = A[i];
            X++;
            if (X == Source.Bitmap.Width)
            {
                X = 0;
                Y++;
            }
        }

        Target.Bitmap.SetData<Color>(B);
    }

    public Game1()
    {
        graphics = new GraphicsDeviceManager(this);
        Content.RootDirectory = "Content";
    }

    protected override void Initialize()
    {
        base.Initialize();
    }
    protected override void LoadContent()
    {
        spriteBatch = new SpriteBatch(GraphicsDevice);

        Eye.FinalizeBitmap(Content);
        Rainbow.FinalizeBitmap(Content);
        PrintOut.FinalizeBitmap(Content);

        Eye.ResetViewportDimensions();
        Rainbow.ResetViewportDimensions();
        PrintOut.ResetViewportDimensions();
    }

    protected override void Update(GameTime gameTime)
    {
        // Allows the game to exit
        if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
            this.Exit();

        GraphicsDevice.Textures[0] = null;

        PrintOut.FinalizeBitmap(Content);



        PrintTo(Eye, PrintOut, Destination);
        Destination.Y++;

        base.Update(gameTime);
    }
    protected override void Draw(GameTime gameTime)
    {
        GraphicsDevice.Clear(Color.CornflowerBlue);

        spriteBatch.Begin();
        spriteBatch.Draw(PrintOut.Bitmap, PrintOut.Position, PrintOut.Viewport, PrintOut.Tint);
        spriteBatch.End();

        base.Draw(gameTime);
    }
}

任何帮助都将不胜感激。

4

0 回答 0