Question

I'm making a level-editor for a game, and I don't want to do "content->add and choose an image/sprite/texture" and then do Texture2D texture1 = Content.Load<Texture2D>(@"path\name");, I want to add to my project every texture I need by OpenFileDialog.

I already implemented the OpenFileDialog, so I'm at the point when I have a file stream with my image. Now, how can I load the texture to be permanent in my content folder? (So I would have .xnb file?)

I read to do use the function Texture2D.FromStream(GraphicsDevice, filestream);, but this doesn't create the .xnb file in my folder; i just need something that does the same thing as the usual way to load textures, with "content->add", ecc.

Thanks!

Was it helpful?

Solution

I recommend simply generating a file which is a record of all resources for your game to load. I have stopped relying on the XNA Content Project template as it requires you to be very deliberate about every resource you add, which is less conducive to rapid prototyping for game development.

When your editor starts, it reads the file (with paths to each resource you will need), and appends to this file every time you open a new texture (from your dialog), then loads that texture. In this way, the next time your project runs, your game will automatically load every resource that has been added. How you manage excessively large resource caches and exactly when it loads and unloads is up to you, but I have experienced this is much more effective.

If you are concerned about packaging, you can also set up some simple compression to box up your textures and sounds.

//This will just load all textures from a folder,
// and works well enough if you want to keep your game open/moddable.
foreach (string str in Directory.GetFiles("resource\\texture\\","*.png", SearchOption.AllDirectories))
{
    int lastSlash = str.LastIndexOf('\\');
    string textureName = ((lastSlash > -1) ? str.Substring(lastSlash + 1) : str).Replace(".png","");
    Util.Log(LogManager.LogLevel.Debug, "Loading texture: " + str);
    textures[textureName]= Texture2D.FromStream(context.GraphicsDevice, (Stream)File.OpenRead(str));
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top