Making Content Pipeline build external images?

Hello everyone.
I’m making a shooter game that will allow for custom maps. The problem is, the Content Pipeline doesn’t want to build the images that were not implicitly added into it (e.g. in Visual Studio). What I want to do is just throw my map files into the “Content\maps\” folder and launch the game and have all maps loaded. Is there any way to make the Monogame’s Content Pipeline build everything inside the folder?
Thanks in advance!

It’s never building files you didn’t add to the mgcb file. You should add content using the Pipeline Tool. It allows for adding multiple files or complete folders in one action.

you can load the maps during runtime if you run mgcb as a process

I’ll then have to rebuild the game, which I don’t want to do. I want users to download maps and play them without having to magically rebuild the whole game

Oops, I didn’t get this right the first time I guess.

Yes it will. Just make sure the xnb’s go in the working directory of the application

You can just store the maps in xnb format

okay then, maps that i’m working with consist of 2 things - .json description and a .png tileset. is there any way to combine them and convert to .xnb?

Sure, the xnb extension just means the content in it has some metadata for the MonoGame (or XNA) ContentManager to know how to load it. Other than that it’s just binary data that you can use however you want. So if you want everything to be in 1 xnb file you can just write out the file length followed by the binary data for each file that you want to include in it. That’s not the most convenient way to do this though.

How are you processing your levels now? Just do that and include the xnb with the saved level data.

1 Like

You can write a ContentImporter and store both the json and the textures in a single .xnb with something like output.WriteRawObject((Texture2DContent)atlas);
You can also have textures stored in separate .xnbs and linked from the main .xnb just like the Model processor does.

Here’s something close to what you want. It loads the images from a TMX file and stores it in a single .xnb with an embedded texture and some extra information extracted from the TMX. (warning: mipmaping generation still has bugs…) https://github.com/tainicom/Aether.Extras/tree/TextureAtlas/Content.Pipeline/AtlasImporter

2 Likes

The following code is not particular clean or sexy but you can basically do the same thing avaiable in XNA. This may change a bit for cross platform projects because of portable libs, but it would be easy to change how you load the stream.

        Texture2D localImage = null;
        Texture2D fromWeb = null;

        using (var filestream = new FileStream(@"tests.png", FileMode.Open))
        {
            localImage = Texture2D.FromStream(GraphicsDevice, filestream);
        }

        using (var client = new System.Net.WebClient())
        {
            var result = 
                client.DownloadData("http://opengameart.org/sites/default/files/Heroes_01preview.png");

            using (var stream = new MemoryStream(result))
                fromWeb = Texture2D.FromStream(GraphicsDevice, stream);
        }

        spriteBatch.Begin();

        if (localImage != null)
            spriteBatch.Draw(localImage, Vector2.Zero, Color.White);

        if (fromWeb != null)
            spriteBatch.Draw(fromWeb, 
                new Vector2(localImage != null ? localImage.Width : 0, localImage != null ? localImage.Height : 0), 
                Color.White);

        spriteBatch.End();
1 Like