I’m attempting to draw many textures onto one texture to create a map for an RTS game, and while I can can draw an individual texture onscreen, drawing them all to a render target seems to have no effect (the window remains AliceBlue when debugging) . I am trying to determine whether or not anything is even drawn to the render target, and so I am trying to save it as a Jpeg to a file and then view that Jpeg, from my desktop. How can I access that Jpeg from MemoryStream?
protected override void LoadContent()
{
spriteBatch = new SpriteBatch(GraphicsDevice);
gridImage = new RenderTarget2D(GraphicsDevice, 1000, 1000);
GraphicsDevice.SetRenderTarget(gridImage);
GraphicsDevice.Clear(Color.AliceBlue);
spriteBatch.Begin();
foreach (tile t in grid.tiles)
{
Texture2D dirt = Content.Load<Texture2D>(t.texture);
spriteBatch.Draw(dirt, t.getVector2(), Color.White);
}
test = Content.Load<Texture2D>("dirt");
GraphicsDevice.SetRenderTarget(null);
MemoryStream memoryStream = new MemoryStream();
gridImage.SaveAsJpeg(memoryStream, gridImage.Width, gridImage.Height); //Or SaveAsPng( memoryStream, texture.Width, texture.Height )
// rt.Dispose();
spriteBatch.End();
}
I made a simple screenshot method,
Also, Are you loading
Texture2D dirt = Content.Load<Texture2D>(t.texture);Every frame? It looks like it… Dont do that! That will cause massive lag loading hundreds of tiles, hundreds of times per second! instead make a global textureTexture2D DirtDextureand in yourLoadContent()method doDirtTexture = Content.Load<Texture2D>(t.texture);Now when you draw you can dospriteBatch.Draw(DirtTexture,...Do the same with
spriteBatch = new SpriteBatch(GraphicsDevice);andgridImage = new RenderTarget2D(GraphicsDevice, 1000, 1000);You dont need to make new RenderTarget and Spritebatch each frame! Just do it in the
Initialize()Method!Also see RenderTarget2D and XNA RenderTarget Sample For more information on using render targets
EDIT: I realize its all in LoadContent, I didnt see that because the formatting was messed up, remember to add your Foreach (Tile, etc) in your Draw Method