7
\$\begingroup\$

I've been looking into how to use the XNA Content pipeline appropriately for a game I am working on. I have however been having a hard time wrapping my head around it and am not sure on the best way to proceed.

The specific example is, I have a top down 2-d RPG style game. I want to create a utility (probably Windows Forms) that will allow me to easily create and edit maps. Each map consists of 1 or more graphic layers, each layer containing a N x N grid of tiles. In turn each tile starts out as a bitmap.

Given the above information I see a few ways to proceed:

  1. Store the map info in an XML file compatible with the Content Pipeline XML loader. Each tile is stored as a string reference to the corresponding texture asset. Texture assets are added in the game project.
  2. Store the map info in an XML file compatible with the Content Pipeline XML loader. Each tile is stored as the path to the corresponding .bmp/.png files.

Now there are things that I really don't like about both approaches. The main issue with approach one is that the map creation and the .xnb texture asset creation are completely separate. The .xml map file is created by the utility, while the texture asset is added to my content in my game. This becomes an issue trying to make sure the .xml map file and all the texture assets in the project stay in sync.

Approach two would allow me to keep all the appropriate image file next to the map files using the map utility. However, it seems like I shouldn't have to do something like this. Also I would need to be more intelligent about loading the maps so that I am not rapidly loading the same image off disc for each tile.

So my question is: If you want to add a content generation utility (like a map editor), what is the best way to make sure that the custom content (map file) stays in sync with the texture assets. Also, keep in mind that in my map editor I would need the ability to load and display the texture assets.

Any input is much appreciated.

\$\endgroup\$

3 Answers 3

6
\$\begingroup\$

What you want is to is get your textures to build based on the contents of the XML file. This way you don't have to add the textures to your content project, the pipeline will automatically include the texture file into the build for you.

To do this you will need to write a custom content processor (you can still use the standard XML importer), and use ContentProcessorContext.BuildAsset in that processor to reference the external texture.

I think that you will need to create a custom ContentTypeWriter and ContentTypeReader to write and then read the ExternalReference that BuildAsset gives you.

\$\endgroup\$
1
  • \$\begingroup\$ Andrew is quite right here! \$\endgroup\$
    – Roy T.
    Jul 10, 2011 at 11:54
2
\$\begingroup\$

Put all your tile-images into a single image file, and reference them by the tile-number.

This will be much more performant and error-proof, and has the advantage that you can set up your levels in a tile-editor like Tiled.

\$\endgroup\$
3
  • \$\begingroup\$ Won't this only work more performantly for extremely small games since loading too big a texture is slow and above certain resolutions impossible \$\endgroup\$
    – Roy T.
    Jul 10, 2011 at 7:30
  • 1
    \$\begingroup\$ @Roy T: If you have so many tiles that your packed texture is "too big", chances are you probably need to be packing them into one or more textures for rendering performance. \$\endgroup\$ Jul 10, 2011 at 7:48
  • \$\begingroup\$ @Andrew Russel, yeah that's what I meant :). But I just started calculating and a 4096x4096 texture (max on older videocards) can pack 64x64 (aka 4096) sprites of 64x64, so maybe it's not as much of a problem as I thought. \$\endgroup\$
    – Roy T.
    Jul 10, 2011 at 11:53
1
\$\begingroup\$

I'm not 100% sure I got your question clear in my head, but try this:

You'll have to make the map editor leading, don't add any assets to your Content project except for one XML file (game.xml or something). Now have your level editor edit the game.xml file, by adding links to other files. For example creating a new level in your level editor adds the tag <level rank=5>Content/MyLevel/level5.xml/> to your XML file.

Now to get everything built you need to write a custom content pipeline extension. (Make sure to set the new importer and processor on your game.xml file instead of the default XML processor)

This processor will traverse the XML files and build all the assets by calling the appropriate importers and processor. This will leave you with everything nicely built and not having to go back and forth to the content project and your editor.

Resources: http://msdn.microsoft.com/en-us/library/bb447754.aspx

\$\endgroup\$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .