开发者

XNA new Texture2D retains old data

I have an odd problem that I believe my relative lack of knowledge of the XNA framework prohibits me from fixing.

Basically I have a Texture2D reference that is set to a new instance of Texture2D. During runtime some of its pixels are set and this all works nicely as its drawn in the gameloop.

The odd thing is, if I set the reference to null, (it will only draw if not null) it as expected does not draw.

Later on, I set the reference to a new Texture2D and it starts drawing it to screen. The issue is it holds all the data of the original Texture2D object.

EDIT

Sorry I wasnt clear before.

What I have is something like this...

p开发者_运维问答rivate Texture2D WorkingTexture {get; set; }

private void Update()
{
 if(some input)
 {
  this.WorkingTexture = null;
 }

 if(some other input)
 {
  this.WorkingTexture = new Texture2D(this.GraphicsDevice, 500, 500, true, SurfaceFormat.Color);

  this.WorkingTexture.SetData<Color>(0, new Rectangle((int)vector2.X, (int)vector2.Y, 4, 4), colors, 0, 16);

  }
}

private void Draw()
{
 if (this.WorkingTexture != null)
 {
    spritebatch.draw(this.WorkingTexture,.....);
 } 
}

SECOND EDIT

I have also set a for loop and manually set every pixel of the texture to a color after it is created. this works yet it still shows pixels as the colors i set them previously. This is very odd.


Please disregard this thread. The issue was very embarrassingly my own doing. I had some code that buffered up pixels to be changed and yes... you guessed it I wasn't clearing it and it was doing the work all over again on the new texture instance.

very embarrassed....


Without having much detail to go on, it sounds like you are trying to load the Texture2D with the changes that you made earlier. Since you set it to null, when you reload the Texture2D, it will be back to the original. You'll need to save the changes you make to the pixels for later use if you want to use it again. Hope this helps!


So you've set up a texture like this

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

and then you apply a new reference it like so

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

right?

Thats not how it's supposed to be. You have to set up your GraphicsDevice to use a specific Texture. With the BasicEffect class, this would be something like this:

BasicEffect effect = new BasicEffect(GraphicsDevice);
effect.Texture = texture;
effect.CurrentTechnique.Passes[0].Apply();


How do you create the textures? If you load them from a resource (texture = Content.Load("texture_name")) the problem you describe sounds like a bug in your logic: your program is not doing what you think it's doing. Trace, debug...

If you're creating brand new textures (texture = new Texture2D(...)) it's possible (not verified) that the new texture is not initialized and its memory space is being retrieved from some kind of memory pool, so retrieving the recently marked to dispose texture data. Try to initialize your texture data as soon as you create it setting it to all-black, for example.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜