I’m trying to create a module for my game, and the idea is to be able to use it all my games.
So my lib project has its own content folder and its all well, but its crashing with the host project’s content folder.
Here is my component (it is its own project that I ref link to host project(phone app)):
namespace FPSComponent
{
public class FPS : DrawableGameComponent
{
private SpriteBatch spriteBatch;
SpriteFont normal;
Texture2D headerBackground;
public FPS(Game game)
: base(game)
{
spriteBatch = new SpriteBatch(Game.GraphicsDevice);
string tempRootDirectory = Game.Content.RootDirectory;
Game.Content.RootDirectory = "FPSComponentContent";
headerBackground = Game.Content.Load<Texture2D>("header-background");
normal = Game.Content.Load<SpriteFont>("normal");
//Game.Content.RootDirectory = tempRootDirectory; <-- does not work
}
public override void Update(GameTime gameTime)
{
// TODO: Add your update logic here
base.Update(gameTime);
}
public override void Draw(GameTime gameTime)
{
//GraphicsDevice.Clear(Color.CornflowerBlue);
spriteBatch.Begin();
spriteBatch.Draw(headerBackground, Vector2.Zero, Color.White);
spriteBatch.DrawString(normal, "FPS COMPONENT", new Vector2(20, 20), Color.White);
spriteBatch.End();
base.Draw(gameTime);
}
}
}
What Im trying to do where is create a standalone component to import to my projects, this is mostly for learning purposes… and not a working FPS example atm 🙂
I think you must read this article Content Pipeline MSDN
Here is the point, When you hit F5 on visual studio it will compil your project and you Assets.
By using
Game.Content.Load<Texture2D>XNA will try to find out a .xnbWhen visual studio is compiling your project, he is creating your .xnb into a specific forlder.
You are not really loading a .png or .jpg files directly to your game 🙂
You must first compile your new assets into .xnb files (kind of serialisation process)
And here is a similar question https://gamedev.stackexchange.com/