1

I want to create a (WinForms) application which consists of multiple panels. For each panel, I want to be able to assign a .dll file:

example

Those .dll files should contain XNA games that should be rendered into the respective panel.

I can load those games with

var game = (Game)Activator.CreateInstance(System.Reflection.Assembly.LoadFrom(path).GetTypes()
    .First(t => t.IsSubclassOf(typeof(Game))));

Now, I cannot use game.Run() because it will start a second message loop.

Starting a new thread also fails because of InvalidOperationExceptions concerning access to controls from another thread.

The tutorials I found on the web were only able to deal with a game I write myself and include it into the main application, but I want to be able to add other games, similar to plug-ins.

How can I make my WinForms application render those XNA games, or is there a better solution to this?

pascalhein
  • 5,700
  • 4
  • 31
  • 44

1 Answers1

1

I've done something similar in the past, but I had used DrawableGameComponents, instead of multiple XNA projects. You may want to consider taking this route instead. Essentially, you will be able to call your various Draw() functions all from the same binary.

Then in your form, you can create a pointer to the Panel Handle, which will serve as a way to indicate to your graphics renderer where to draw the scene:

IntPtr panel1Handle;                
IntPtr panel2Handle;
IntPtr panel3Handle;
panel1Handle = Panel1.Handle;
panel2Handle = Panel2.Handle;
panel3Handle = Panel3.Handle;

Then, in your respective DrawableGameComponent.Draw() calls, set the area you are going to be drawing to, before doing the actual drawing:

GraphicsDevice.Present(panel1Handle);
jgallant
  • 11,143
  • 1
  • 38
  • 72
  • thank you for your proposal. I am now creating these `DrawableGameComponents` in a game I hide afterwards, and show them in the panels on my form. – pascalhein Jun 18 '13 at 17:04
  • actually, I have one more question: when running the game with multiple dll's, it shows only the first one in each panel (and it also only loads the first type). how can I load the different types and make it display a different `DrawableGameComponent` in each panel? – pascalhein Jun 19 '13 at 16:16
  • @csharpler Make sure to call GraphicsDevice.Present(panel) before you start your respective draw call in each panel. – jgallant Jun 19 '13 at 16:52