0

I have this struct:

public struct LevelElements
{
    public Texture2D levelTexture;
    // other variables...
}

and I initialize it in this way:

for (int i = 0; i < 2; i++)
    levelElements[i] = new LevelElements
    {
        levelTexture = content.Load<Texture2D>("Terrain/level"),
        // other variables...
    }

Then I draw the first texture while modifying it with textureLevel.SetData method.
The problem is that if I draw the second one it looks the same as the modified first one, and not as the original one loaded from the content.
Why have both levelTextures the same reference? Doesn't Content.Load<Texture2D>() create a new instance?

PS: I don't need to create a copy of that texture, I was only testing my code and I found this behaviour.

pinckerman
  • 4,115
  • 6
  • 33
  • 42

1 Answers1

1

I haven't used it before but it seems to me that you would want to declare it like this in order to use it as an instance variable:

Texture2D texture = new Texture2D( resourceDevice, image.PixelWidth, image.PixelHeight, false, SurfaceFormat.Color);

read on: from msdn

John Faulkner
  • 990
  • 7
  • 10
  • Nice method, but BitmapSourceExtensions.CopyTo seems to exists only in Silverlight. Anyway your solution is correct, "forcing" the creation of a new instance it now works! – pinckerman Sep 09 '13 at 00:23
  • 1
    yea I wasn't suggesting that you use that exact method, just that you need to instantiate a "new" instance of Texture2D instead of using the static declaration. – John Faulkner Sep 09 '13 at 03:53