MGZero
MGZero

Reputation: 5963

Stitching together an array of tiles

Suppose I have an array of Rectangles corresponding with tiles in a texture atlas. What I want to do is take these tiles and create a Texture2D object out of them. Basically, I want to put the pixel data of each tile together sequentially to form one image. How could I go about doing this? Would Texture2D.SetData() be of use here?

Upvotes: 2

Views: 382

Answers (1)

Alex Anderson
Alex Anderson

Reputation: 860

RenderTarget2D target = new RenderTarger2D(...); 
//I cant remeber the arguments off the top of my head.
//I think its GraphicsDevice, Width, Height, GenerateMipmap, SurfaceFormat, Depthformat

GraphicsDevice.SetRenderTarget(target);
GraphicsDevice.Clear(Color.Black); //any colour will do
using(SpriteBatch b = new SpriteBatch(GraphicsDevice))
{
   b.Begin();

   //Loop through all texture and draw them, so ...
   for(int y = 0; y < 10; i++)
     for(int y = 0; y < 10; i++)
       batch.Draw(Texture, new Rectangle(xPos, yPos, width, height), Color.White));

   b.End();
}

GraphicsDevice.SetRenderTarget(null);

//Then to access your new Texture, just do 
Texture newTexture = target; //Target inherits from Texture2D so no casting needed

Hope this helps :)

Upvotes: 1

Related Questions