Been reading up on various methods in the XNA framework, and found this one.

I've done some Google searching, but I can't seem to find any information on how to use this particular method overload. Parameters 3-5 are easy enough to understand, since they are used in another overload, but I'm not so sure about the first two.

I'm fairly sure that the first parameter is the mipmap level we are setting data to, but the second one has me completely lost.

However, I'm not exactly sure what I should be passing in here. I know it can be a null object, or a valid Rectangle object, but I'm not sure whether it is being used to specify the size of the mipmap texture like this:

Mipmap arrangement from MSDN

Or whether it is supposed to be its position and size inside the Texture2D object like this:

Mipmap arrangement from NVIDIA GPUGems

Can someone shed some light on this please, and maybe provide a parameter sample or two so I can better understand? (I'm one of those people that can spend 2 hours trying to understand a theory and make absolutely no progress, and get it in about 5 minutes after a few examples [or half dozen for more complex stuff])


1 Answer 1


It is the the position and size within the Texture2D object. Set data can only alter the color data within the minimap. It can not alter the size or create new ones.

The Nullable<Rectangle> rect is the destination within the Texture2D and the int startIndex, int elementCount is the source from the dataColors array.

Here are a few examples.

The Texture is generated with the below code

texture2D = new Texture2D(graphics.GraphicsDevice, 32, 32);

var dataColors = new Color[32 * 32];
for (var i = 0; i < dataColors.Count(); i++) {
    dataColors[i] = new Color(Color.Red.R, i % 225, 0);

Having a Rectangle the size of the Texture2D is the same as if the Rectangle is null. Worth nothing is that the Width * Height of the rectangle must be the same as the elementCount. Also the startIndex + elementCount must be less or equal than the size of the Color array and the Texture2D.

texture2D.SetData(0, new Rectangle(0, 0, 32, 32), dataColors, 0, 32 * 32);

enter image description here

Here I change the destination rectangle and sample from the beginning of the Color array.

texture2D.SetData(0, new Rectangle(6, 6, 8, 15), dataColors, 0, 8 * 15);  

enter image description here

Here I use the same destination rectangle as before and sample starting at an off set for the of the Color array.

texture2D.SetData(0, new Rectangle(6, 6, 8, 15), dataColors, 112, 8 * 15);  

enter image description here

  • \$\begingroup\$ Thanks. Thats cleared that up. I assume if I passed '1' for the level parameter instead of '0', it'll write the data to a separate layer to the texture independent of the one shown in your pics? \$\endgroup\$
    – Seta
    Commented Dec 12, 2014 at 3:14
  • 1
    \$\begingroup\$ Yes but only if that minimap layer exists, it will not create one for you. \$\endgroup\$ Commented Dec 12, 2014 at 3:22
  • \$\begingroup\$ Sorry for the late reply, only just got around to testing this in my game (last few days have been a complete structural hoverhaul & tidyup to most of the code). So far, it seems to be working. There are a few more things I want to check, but I'll need to finish fixing/upgrading everything else first (like the InputSystem) before I'll accept. I'll mark it up in the meantime. \$\endgroup\$
    – Seta
    Commented Dec 16, 2014 at 10:27

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .