XNA Creators Club Online
Page 1 of 1 (5 items)
Sort Posts: Previous Next

Beginner Q: Drawing to a texture instead of the screen

Last post 1/28/2008 4:19 PM by Lord Ikon. 4 replies.
  • 1/27/2008 12:46 PM

    Beginner Q: Drawing to a texture instead of the screen

    Hello guys,

    Thanks in advance for the help.


    In my game, my world map (2d tiles) is dynamically generated and changes infrequently.  For that reason, I don't want to loop through it and call the spritebatch.Draw routine for every tile for every frame.  I thought the solution was to create a new rendertarget and draw to that surface and then use the GetTexture() function of render target to save my world map as a texture.  That way, each frame just draws that final texture to the screen, and I only have to redraw it when necessary.

    The problem is when I create my render target I'm making it as big as the world map, which is a 30x30 map of 100-pixel wide and high tiles.  Creating this target of 3000x3000 doesn't work.  I cannot draw to a rendertarget of 3000x3000 and using the Save() function of the GetTexture() function saves an all black bitmap (although with the correct number of pixels).

    I can draw this correctly directly to the screen without an issue, so what is the appropriate method for what I'm trying to accomplish here?

    Thanks,

    -Ben

  • 1/27/2008 12:54 PM In reply to

    Re: Beginner Q: Drawing to a texture instead of the screen

    The Draw calls are very light, and I would think that it would be easier to just draw every frame, unless you run into performance issues. I have drawn thousands of sprites per frame without any issue.

    As for Texture2D limits, many graphics cards have a limit of 2048x2048, so for maximum compatability you should stay below this limit.

    Bill Reiss - Dr. Popper currently in peer review

    XNA Tutorials for Beginners
  • 1/27/2008 4:01 PM In reply to

    Re: Beginner Q: Drawing to a texture instead of the screen

    You may be able to find a way to glom all tiles of a specific type together in your world. For example, you may have grassTexture1 on 1/5th of your tiles. If you group those together, and draw all grass tiles at once you would be able to do it in a single draw call. If all tiles are not on a screen at once you could then split your world up in a quad-tree fashion, so you're only drawing the grass tiles once per quad-tree node on the screen, which would only be a few calls.

    XNA QuickStart Engine (3D Game Engine for XNA) | My site
    "I'll be whatever I want to do!", Philip J. Fry
  • 1/28/2008 3:34 PM In reply to

    Re: Beginner Q: Drawing to a texture instead of the screen

    Hey guys,

    Thanks for the responses!

    In regards to drawing right to the screen each time, there is an additional complication.  The way the tiles stack means that each one can have one or more shadows drawn right over it.  My routine not only loops through each tile and draws it, but then has to check for a shadow from each of the eight directions and draw the shadow as well.  The tile has already calculated which shadows it needs, so my routine just tests eight booleans when it draws a tile, but still a 30x30 tile map gets more and more complicated.

    Right now, it hovers around 28-34 frames per second.


    My next step is, I believe, to store a small texture with each tile object that already has the shadow drawn onto it.  That would remove the need for me to test for and draw the shadows seprately, since they would already be on the tile's texture.

    Even further, for the sake of memory conservation,I'd want to load all combinations of tile and shadows into the pipeline and then just have the tile reference the correct image. Yea?

    Thanks again!


    Here is the link to the tileset and how it works, just for a visual reference:  http://lostgarden.com/2007/05/cutegod-prototyping-challenge.html
  • 1/28/2008 4:19 PM In reply to

    Re: Beginner Q: Drawing to a texture instead of the screen

    For shadows you usually want to render the whole scene to a depth buffer and use shadow mapping, then render the whole scene normally, then use the shadow map to render the shadow map on top of it. This requires a two-pass system, but will draw all shadows at once.

    XNA QuickStart Engine (3D Game Engine for XNA) | My site
    "I'll be whatever I want to do!", Philip J. Fry
Page 1 of 1 (5 items) Previous Next