Generate tilemap then save it as bitmap

I’m playing with rendering a hex-tiled map. Rendering the tiles to the screen is one thing, but I wonder–would it be reasonably easy to save the resultant bitmap as a “merged” texture to reduce the calculations necessary each tick? Especially if the map will never change. Then again, I dont know if underlying optimizations in the game would make that pointless. Would it even be worth considering?

Well, you could render to a RenderTarget2D and then save that as a bitmap, but just keeping the RenderTarget2D around and reusing that would probably work better.

With a RenderTarget2D, SpriteBatch (and 3D graphics) can be made to render to that instead of the back buffer. Then the RenderTarget2D can be reused and rendered like a regular Texture2D. Though I think you have to set the RenderTargetUsage to PreserveContents in its constructor if you want to reuse it without redrawing to it each frame.

To change the render target, use graphicsDevice.SetRenderTarget, where the parameter passed is the RenderTarget2D when you want to render to that, or null when you want to render to the back buffer (the screen, basically).

Also, I should mention that I personally wouldn’t worry about it unless and until it becomes a problem. Rendering some tiles to the screen doesn’t seem like it would be that expensive.

Thank you very much, this was helpful!

1 Like