Why should I use content pipeline - c#

If I set my images in Content folder to copy always, then I can use them without building it in content pipeline. I simply call them from code like this:
testImage = Content.Load<Texture2D>("MyImage");
and it works without compilation to xnb format. I've noticed that my 46kb image file after compilation have 204kb and I don't know why.
Is there some benefit of compiling it? Or why should I do that?

Whenever you build a group of files (some PNG files for example), MonoGame stops using the PNGs in the game and starts using its own format, XNB. These files are specially designed for game texture and are, in turn, for efficient. They save the user of your game valuable space on their RAM.
Hope my explanation helps
-GHC

Related

c# Storing resources(specifically images) in folder structure

If I want to add pictures, I have to put those into the resources and access them from the picturebox.Image property by using this:
AddPicturesFromOtherFolders.Properties.Resources.myPicture
Thats the only way I know. That works fine if I have 10 or so images, but what if I had 500 images? Nobody could keep track of anything. So I would like to structure these hyperthetical 500 Images in a folder structure which I could then access with something like:
pictureBox1.Image= ../../Assets/img/specialImages/myImage.png
That would be very neat, but I have found no way, that involves 100% C# code.
I would be wuite grateful, if you could help me.
Have a nice day,
Alexander Lenssen
You could use Image.FromFile and load the image from any file you have stored in your file system. For example:
pictureBox1.Image.FromFile(#"D:/Assets/img/specialImages/myImage.png");
There is no way that involves 100% C# code. At least some Compiler options or Setup actions are nessesary. But the first question is even where to store it: Programm Directory or UserProfiles?
Asuming these images are static (will only change when a installer runs), you can just store them into the Programm Directory. And from there deploy them with the rest of the code. Getting them Into the Output directory is not that difficulty. Visual Studio has options for that: https://msdn.microsoft.com/en-us/library/0c6xyb66.aspx You could go further, like having a Shared Repository for Images (i.e., most Photoshop programms have one Content Folder under Programms).
You can go as far as "soft linking" them, wich means you can have one actuall folder on your disk that will be copied/synched into the output directories on any buil.
If you need to Update those Images them on the fly (without adminsitrative rights), stuff becomes more complicated. You can still do it via the SpecialFolders. CommonApplicationData seems like the right place to put this kind of stuff. Even Steam and Minecraft's old Java Launcher do quite some storage there. Not to mention every WebBrowser.

Load Raw Content from Unknown Directory

In my current game, modding is a HUGE part of the game. It took me a while to develop a good system of loading modded content into the game, but I finally settled on a method, and I would like to keep it if possible.
How I'm handling modded content
In the game's content folder (C:/Users/username/4X), there is a Mods folder. Each mod will have it's own folder inside of the Mod directory. The game goes through a file (Rather, it will. I haven't implemented it yet), and figures out which Mod directories that it's going to load. After it's figured that out, it loads all of the content into the game (I can explain in more detail if its pertinent to the topic, I just don't wan't to use up space on unnecessary things).
So what's your problem?
Well, the mods will all have raw resources (.fbx, .wav, .mp3), and since I can't load anything but XNB files, I have absolutely no idea how to load the mod's content. Well, I take it back, I've been thinking of a few solutions but I really don't know which is more practical, or if there is a better way of doing this.
First, I thought about borrowing some code from the Pipeline application, and building all of the mod's content the first time it was loaded, but I don't fully understand the Pipeline's code, and I didn't want to mess up something by partially implementing it.
Next, I thought about requiring mod creators to use the Pipeline to build their content before they release their mods, but that seemed kind of unprofessional, since I want to have a Mod Creation Engine that has all of the tools bundled together. Which brings me back to using some of the Pipeline's code and embedding it in the engine, but then I have the same issue as my last idea.
And finally, I thought about just loading raw content. But there's an obvious flaw with that idea.
So I guess what I'm asking is:
How can I load raw content? That is, content that hasn't been built into XNB Files
If not, how can I start learning about the code that makes the Pipeline application tick, since I'll probably have to use some of it's code?
You can actually load some raw content directly with MonoGame. For example:
var texture = Content.Load<Texture2D>("raw.png")`
should work if you've got the PNG file in the Content directory and set it's properties to Content / Copy if newer.
However, please note that this will only work for some content and some platforms. I know PNG files work, WAV files work (but they must be mono 44khz I believe) and I'm pretty sure I got MP3 files to work once.
You'll probably run into issues with FBX files and sprite fonts. There's nothing stopping you from bypassing the content manager and loading these yourself though. I've done this before with great success.
Oh, and the other thing I should mention, take a look at the TitleContainer.OpenStream method. This is a way to read a file without writing the platform specific code yourself.

Soundeffects not working. Could not load ... asset

So I'm trying to implement soundeffects whenever I click on an item. I've added a .wav sound to my Content folder.
Then I typed the following code in the Game1 class constructor:
SoundEffect soundEffect;
After that I wrote the following code in the Initialize function:
soundEffect = Content.Load<SoundEffect>("Laser_Shoot25.xnb");
Finally, I wrote the following code in my clickFunction:
soundEffect.Play();
When I tried running it, it gave me the following error.
Could not load nameOfSoundFile asset.
I've looked on Google to see if someone else had this same problem, but none of those solutions seem to work for me.
Does anyone know what else I'm supposed to do to make this work?
First thing that comes to mind is the possibility of you loading your assets before the Content object gets initialized. Please post the relevant code where you load your asset to know if this is the case (usually the initialize and LoadContent methods of your Game1 class).
The other possibility is the asset file format.
I'm not up to date with Monogame as I stopped using it with version 3.2, but if things remain the same on this aspect; then for working with sounds and spritefonts you need to add the asset as an exported xnb file, created by using the actual XNA content pipeline.
I have a blog post that explains exactly how to do all of this, which can be found here.
First, you need to install XNA on your machine (in the same post, it explains how to do it if you have Windows 8 and/or Visual Studio 2012/2013).
Then, just follow the steps detailed on the post:
So, once we have this, let’s create a new Windows Game project (it has
to be a game and not only a Content project as we will need to access
the game output directory).
After having this done, we can add our sound effects/instances to the
Content project by right clicking, selecting “Add existing item” and
choosing it in the folder explorer.
Before continuing, make sure you set the properties of the sound as
desired (in my case, I’ll be using a SoundEffect so I need to specify
it in the Content Processor field):
All that remains is to add the xnb file to the Content folder of your
Monogame project.

Is it possible to define the asset name in a custom XNA content processor?

Normally, when loading content using XNA's Content Pipeline, the compiled .xnb files are accessed using an assigned "Asset Name" that can be defined in the Visual Studio GUI. By default, this name is the same as the content's filename sans extension. As a result, you cannot normally load two files that have names differing only by extension as the generated .xnb files would have conflicting names. If you manually change the asset name of one of these files, the generated .xnb files no longer conflict.
For a level loading system I am writing, I was hoping to have the setup of loading the texture data and collision data from two separate files with the same name (level1.png and level1.col) where the collision data is simply a text file. I have written a custom content processor to load the collision data using the Content Pipeline.
It seems it is not possible to modify the asset name directly in normal game code, but I have been unable to determine if it can be done from within a custom content processor. Is this possible? Or must I modify all the asset names by hand in order to do this?
I would ask this on the App Hub forums, but I am having a rather difficult time trying to log into that site without registering (and giving credit card information) for the developer package. I'm currently using XNA for a Windows platform game, and have no interest in developing for the X360 at this time.
In the interest of providing what I found, it seems that it is not possible to change the asset names from within a custom content processor either.

image problem in wpf (image does not show up)

I don't understand why I cannot display an image in WPF. Maybe I modified my resources folder accidentally and that is the reason why I does not get displayed. So I created a new wpf application and I have this:
and when I run the program my picture gets displayed as:
Why is it that when I try doing the same thing in my program the image does not show up!?
note how when I run the program there is no image...
In my other application I just dragged the image control to my main window and then I browsed for a random image on my computer and when I complied and run it it works fine. Why is it that I cannot do the same with the application that I am working with?
EDIT:
with some images it works and with others it does not! why?
take a look:
and when I compile and run one image does not show up!
and also take a look and see how the files have the same properties.
settings for folder image:
settings for mov image:
Chances are the Image does not have the Build Action set to Resource. If it's anything else, including Embedded Resource, then it will not display properly at runtime.
Other things you can check are the the resolution of the image. In Paint, you'd use File>Properties to view the resolution.
Try saving as a different format, such as jpg. If that works, then it's likely a problem with your file.
You may need to clean and rebuild your solution as well.
I fix it and I don't understand why it works. I need to add this information for my sqllite connection to work. I just had to comment this out:
and it worked. Maybe this is a bug of sqllite that I should report because those lines where added when creating the connection with visual studio.
.BMP Image format is not supported. I had the same issue but after converting the image to .jpeg format it worked properly.

Categories