public Texture2D MergeTextures(Texture2D[,] toMerge)
{
int targetWidth = 0;
int targetHeight = 0;
// Check if we can merge the new texture; if the sizes aren't equal, we cant do it, for now
foreach (Texture2D tex in toMerge)
{
if (toMerge[0, 0].Width != tex.Width || toMerge[0, 0].Height != tex.Height)
{
throw new Exception("Texture sizes aren't equal; cannot merge!");
}
}
int textureWidth = toMerge[0, 0].Width;
int textureHeight = toMerge[0, 0].Height;
targetWidth = toMerge[0, 0].Width * toMerge.GetLength(0);
targetHeight = toMerge[0, 0].Height * toMerge.GetLength(1);
int[] result = new int[targetWidth * targetHeight];
for (int i = 0; i < toMerge.GetLength(0); i++)
{
for (int j = 0; j < toMerge.GetLength(1); j++)
{
int[] tempData = new int[toMerge[i, j].Width * toMerge[i, j].Height];
toMerge[i, j].GetData(tempData);
for (int k = 0; k < tempData.Length; k++)
{
// if tempData[k] == 0, it will bring row from -1 to 0
int xoffset = k % textureWidth;
int startPoint = (j * toMerge[i, j].Height * targetWidth) + i * toMerge[i, j].Width;
int yoffset = (k / textureWidth) * targetWidth;
result[startPoint + xoffset + yoffset] = tempData[k];
}
if (onLoadTickListeners != null) onLoadTickListeners(this);
}
}
Texture2D resultTex = new Texture2D(Game1.GetInstance().graphics.GraphicsDevice, targetWidth, targetHeight);
resultTex.SetData(result);
return resultTex;
}