Reputation: 27
I am trying to set the data of a Texture2D using SetData<T>(T[], Int32, Int32)
but no matter what I put as the variables, I always get
AccessViolationException: Attempted to read or write protected memory. This is often an indication that other memory is corrupt.
I have tried using Microsoft.Xna.Color[]
and byte[]
for T, but the same error occurs. SetData<T>(T[])
works without error, but I would like to improve performance by using SetData<T>(T[], Int32, Int32)
instead.
I am using Visual Studio 2013 with .NET 4.5.1 and monogame.
Upvotes: 1
Views: 643
Reputation: 11
Before calling SetData(), you need:
GraphicsDevice.Textures[0] = null;
To modify parts of the texture, use the Rectangle overload of the SetData().
SetData<T>(int mipmapLevel, Rectangle? rect, Color[] data, int startIndex, int elementCount)
Use the rectangle to target which pixels to change, pass 0
for startIndex
and elementCount
is rect.Width * rect.Height
Upvotes: 1
Reputation: 1310
Let's split up the method:
SetData<T>(T[], Int32, Int32)
So as the method tells you, you need to specify a type, and provide an array of said type. However you're providing it with an array of the type, so it expects a parameter that looks like T[][] (which is a compile-wise valid argument. It's an array of arrays of T). However when you only provide it a standard array, that's when it complains.
As long as your array contains as many entries as Width * Height (eg. array.Length == 1024x1024)
To summarize: Take a good hard look at the method structure when something doesn't make sense.
@Edit: Try doing something like this instead:
Texture2D myTexture = new Texture2D(this.GraphicsDevice, 1024, 1024);
Color[] theColors = new Color[myTexture.Width * myTexture.Height];
// Loop through each color object and define it.
myTexture.SetData<Color>(theColors);
Let me know if this works answers your question.
Upvotes: 0