I’ve been digging around the forums and tutorials, but can’t really find an up to date article that describes what I want to do.
So the idea is that I have an ‘engine’ project that derives from Game (so I get all of the updates and managers) and then a WPF editor that hosts the window for the engine rendering.
I’ll need to have multiple WPF controls (embedding in a windows form host is fine) so I can run the editor & things like texture browsers/particle system editors at the same time.
I’ve tried creating and running the Game instance from within the editor, but that pops up window unique to the Game instance, and I haven’t been able to figure out how to explicitly set it’s window handle; I’m left with a game window and my WPF application running simultaneously.
I’ve also tried not deriving my engine from Game and setting up my own GraphicsDeviceManager, but the trouble is whenever I try and create a GraphicsDevice I get an Null Reference Exception
Anyone have article links or pointers on how to accomplish this?
Actually creating a WPF window with a Game embedded doesn’t take too much work.
Create your WPF window, then add a WindowsFormsHost to it (you’ll need to reference System.Windows.Forms in your project).
Before you call Game::Run, pass the windows handle (an IntPtr) of the WindowsFormHost to your class that manages the Game instance. You’ll also want to register an event handler with your GraphicDeviceManager::OnPrepareDeviceSettings.
When you call Game::Run, the function you registered with OnPrepareDevice settings is called, and you just set the window handle of the PresentationParameters to the window handle of your WindowsFormsHost. The Game will then run in your WPF application.
Hi, to my regret is impossible to me to make a project in this way.
When I try I catch this exception:
An unhandled exception of type 'System.BadImageFormatException' occurred in mscorlib.dll
Additional information: Could not load file or assembly 'Game1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null' or one of its dependencies. An attempt was made to load a program with an incorrect format.
Anyone can tell me how to fix this error or share a correct project.
I would be very grateful
Looks like you’ve already got a solution, but in case it helps anybody else: I wrote a MonoGame module for my Gemini framework, which shows one method for doing interop between WPF and MonoGame (mostly based on code from other people).
I also made an GameUpdater class based on today’s branch from monogame.
It works very well and the FrameRateCounter shows as expected, but in slow FPS the entire WPF application run slow, not only the DrawingSurface.
If you want to test, change the type variable from the BlurGameModule class to 3 (CPU blur) or 4 (GPU blur).
Do you already have any solution for the update method or any sugestions for my GameUpdater class?
So if the MonoGame content is slow to render, it will slow down the rest of the WPF UI. I don’t think there’s any easy way around that, using this technique.
Do you already have any solution for the update method or any sugestions for my GameUpdater class?
@tgjones
Perhaps setup another thread and draw from there? I am not sure if it is possible to call draw directly on DrawingSurface. DrawingSurface is inheredly tied to WPF composition.
@thiagoromam
Do you absolutly need to use DrawingSurface? The airspace problem will be solved on win10, until then the alternative is SwapChainRenderTarget and bring up a new Window or draw UI from MonoGame.
I didn’t know that the airspace would be solved on win10.
I first tried the WindowsFormsHost to leave Monogame intact, but we did a lot of things to integrate it in WPF successfully.
One of the biggest problems was handling two different tabs with a host to MonoGame and different sizes.
With the DrawingSurface method we removed a lot of code to deal with different tabs and mixed integrations between WPF key events and WindowsForms mouse events and cursors.