Aero68

How do I use the ContentManager when not using the FrameWork.Game

In all samples we can find "ContentManager aLoader = new ContentManager(Service);"
Since I dont use Game as a baseclass I don't have "Service".

So I create a service like this "GameServiceContainer Service = new GameServiceContainer()"

Now, every thing I try to load, textures, models I get FileNotFound

Any idees



Re: XNA Framework Using ContentManager without Xna.FrameWork.Game

Leaf.

The ContentManager uses the IServiceProvider to find an IGraphicsDeviceService implementation, so you need to make sure that you have an IGraphicsDeviceService implementation (normally this is GraphicsDeviceManager) added to the GameServiceContainer.

Cheers,
Leaf.






Re: XNA Framework Using ContentManager without Xna.FrameWork.Game

Aero68

Thanks, but the GraphicsDeviceManager needs a Game instance as a parameter!



Re: XNA Framework Using ContentManager without Xna.FrameWork.Game

tportz

You don't need to use the GraphicsDeviceManager class. Just provide your own implementation of IGraphicsDeviceService.



Re: XNA Framework Using ContentManager without Xna.FrameWork.Game

Aero68

You must have some documentation that I haven't

Is anyone here using XNA without the Game framework
I don't want you guys writing my code but am stuck. My code looks like this!

public partial class Form1 : Form
{
Microsoft.Xna.Framework.Graphics.
GraphicsDevice gd = null;
Microsoft.Xna.Framework.Graphics.
Model model = null;
Microsoft.Xna.Framework.
GameServiceContainer Service = new GameServiceContainer();
Texture2D mTexture = null;

public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
Microsoft.Xna.Framework.Graphics.
PresentationParameters pp = new Microsoft.Xna.Framework.Graphics.PresentationParameters();
pp.IsFullScreen =
false;
pp.BackBufferCount = 1;
pp.BackBufferHeight = splitContainer1.Panel2.Height;
pp.BackBufferWidth = splitContainer1.Panel2.Width;
gd =
new Microsoft.Xna.Framework.Graphics.GraphicsDevice(Microsoft.Xna.Framework.Graphics.GraphicsAdapter.DefaultAdapter,
Microsoft.Xna.Framework.Graphics.
DeviceType.Hardware,
this.splitContainer1.Panel2.Handle,
Microsoft.Xna.Framework.Graphics.
CreateOptions.HardwareVertexProcessing,
pp);

//Service.AddService(typeof(MyGraphicsDeviceService), new MyGraphicsDeviceService());
//ContentManager aLoader = new ContentManager(Service);
//mTexture = aLoader.Load<Texture2D>(@"Textures\DefaultTexture.dds") as Texture2D; // File not found :(

}

private void RenderArea_Paint(object sender, PaintEventArgs e)
{
if (gd.GraphicsDeviceStatus == Microsoft.Xna.Framework.Graphics.GraphicsDeviceStatus.Normal)
{
gd.Clear(Microsoft.Xna.Framework.Graphics.
Color.Blue);

gd.Present();
}
}

}





Re: XNA Framework Using ContentManager without Xna.FrameWork.Game

Aero68

Ok, now I have implemented my own GraphicsDeviceService that I add to the GameServiceContainer but I still get "File not found".



Re: XNA Framework Using ContentManager without Xna.FrameWork.Game

Leaf.

Do you know that you are providing correct paths to the Load method The ContentManager will look for the assets relative to the executing assembly, so if you use "somemodel" it will look for "somemodel.xnb" in the same folder as your exe.

Cheers,
Leaf.






Re: XNA Framework Using ContentManager without Xna.FrameWork.Game

Aero68

Thanks for your replys.

I don't think it's possible using the ContentLoader in VS2005 Pro and a standard C# project!
I have no problem using it in GS and load assets since the properies for a assets tell wich importer etc to use.

I give up and "wrap" needed functionality from my DX9 engine to managed code instead, XNA is still great fun for creating games,
but maybe not so usefull for creating tools using .net winforms and xna.





Re: XNA Framework Using ContentManager without Xna.FrameWork.Game

Shawn Hargreaves - MSFT

It's totally possible to use the ContentManager from any .NET programming environment.

If you are getting a file not found error, that almost certainly means your XNB files aren't in the directory the loader is looking, or that you are passing the wrong asset name strings. By default the ContentManager will look in the same directory as the executable, but you can override that using the constructor that takes an explicit content root directory parameter.





Re: XNA Framework Using ContentManager without Xna.FrameWork.Game

John.Doe

Well, if you implement your own IGraphicsDeviceService and use the correct path and correct path format, it works. But writing a tool that works with the ContentPipeline is kind of a pain. There are some other threads in the forum discussing what would be neccessary to convert a standard Texture into one that the content pipeline can read. In short: The VSE the textures and models and so forth are converted to a special format for the contentpipeline, one can mimic this by creating a VS project file on the fly and calling msbuild to execute the conversion before loading the file. The result is instead of a texture "black.tga" a "black.xnb" - for this example taken from the Spacewar starter kit (just create one and compile it, then add the texture from the debug folder to your Gameless project).

Then you need a IGraphicsDeviceService

class SimpleGraphicsDeviceService : IGraphicsDeviceService, IGraphicsDeviceManager
{
private GraphicsDevice graphicsDevice;
private System.Windows.Forms.Control renderControl;
public SimpleGraphicsDeviceService(System.Windows.Forms.Control renderControl)
{
this.renderControl = renderControl;
}

#region IGraphicsDeviceService Members
public event EventHandler DeviceCreated;
public event EventHandler DeviceDisposing;
public event EventHandler DeviceReset;
public event EventHandler DeviceResetting;

public GraphicsDevice GraphicsDevice
{
get { return this.graphicsDevice; }
}
#endregion

#region
IGraphicsDeviceManager Members
public bool BeginDraw()
{
//throw new Exception("The method or operation is not implemented.");
return false;
}

public void CreateDevice()
{
Microsoft.Xna.Framework.Graphics.
PresentationParameters presentationParameters = new
Microsoft.Xna.Framework.Graphics.PresentationParameters();
presentationParameters.IsFullScreen =
false;
presentationParameters.BackBufferCount = 1;
presentationParameters.BackBufferHeight =
this.renderControl.Height;
presentationParameters.BackBufferWidth =
this.renderControl.Width;
this.graphicsDevice = new GraphicsDevice(
GraphicsAdapter.Adapters[0],
DeviceType.Hardware,
this.renderControl.Handle,
CreateOptions.HardwareVertexProcessing,
presentationParameters);
}

public void EndDraw()
{
//throw new Exception("The method or operation is not implemented.");
}
#endregion
}

And a windows form (the form has just one panel, I omitted the initialize method), that provides the IServiceProvider interface to the ContentManager:

public partial class Form1 : Form, IServiceProvider

{

private SimpleGraphicsDeviceService graphicsDeviceService;

private Microsoft.Xna.Framework.Content.ContentManager contentManager;

public Form1()
{
InitializeComponent();
}

protected override void OnLoad(EventArgs e)
{
base.OnLoad(e);
this.graphicsDeviceService = new SimpleGraphicsDeviceService(this.panel1);
this.graphicsDeviceService.CreateDevice();
this.contentManager = new Microsoft.Xna.Framework.Content.ContentManager(this);
Texture2D texture = this.contentManager.Load<Texture2D>(@"Black") as Texture2D;
MessageBox.Show("Texture null " + (texture == null));
}

#region IServiceProvider Members
public new object GetService(Type serviceType)
{
if (serviceType == typeof(Microsoft.Xna.Framework.Graphics.IGraphicsDeviceService))
return this.graphicsDeviceService;
else
return base.GetService(serviceType);
}
#endregion

}





Re: XNA Framework Using ContentManager without Xna.FrameWork.Game

Aero68

Miljon Thanks John,
All my code was working except that I had missed the small part that ContentManager only loads xnb files .

And Shawn Hargreaves Blogg had all info I needed to create them on the fly!

Thanks,
Anders Eriksson.