If you have a command line app that needs to create an XNA graphics device, the code in this answer should be of some assistance.
In a nutshell, you need some of the classes from the WinForms sample to avoid having to mess around creating a graphics device services and so on (specifically the classes ServiceContainer
and GraphicsDeviceService
).
Then you can do this:
Form form = new Form(); // Dummy form for creating a graphics device
GraphicsDeviceService gds = GraphicsDeviceService.AddRef(form.Handle,
form.ClientSize.Width, form.ClientSize.Height);
ServiceContainer services = new ServiceContainer();
services.AddService<IGraphicsDeviceService>(gds);
content = new ContentManager(services, "Content");
Tada - now you have a working ContentManager
that you can use to load stuff. I believe you should be able to get the actual GraphicsDevice
from the GraphicsDeviceService
, too.
The form you create is never displayed. Remember to reference System.Windows.Forms.dll
in your project.
Disclaimer: This was written for XNA 3.1. I haven't tested it in 4.0, but I suspect it will work with little or no modification.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…