重置XNA中的Texture2Ds

时间:2013-05-28 06:55:55

标签: c# xna reset texture2d

我的实验遇到了问题。我正在尝试创建一个方法,将一个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);
    }
}

非常感谢任何帮助。

0 个答案:

没有答案