I have a simple XNA 4.0 Game written. I want to make a Windows Form User Control that will render this game, and provide the necessary interaction feedback (keyboard and mouse) back to XNA.
I have tried the following:
In XNA:
Redirect XNA's Game.GraphicsDevice to a RenderTarget2D.
Emit an event sending an object with KeyboardState and MouseState to be filled in by Windows Forms, at the beginning of Update()
In WinForms:
Capture the event, filling in KeyboardState and MouseState with data obtained by the usual keyboard and mouse events in Windows Forms.
On the OnPaint, call Game.RunOnFrame()
Get the RenderTarget2D from the game (as a texture).
Lock the texture's data, and try to paint it pixel by pixel in my user control's Graphics.
Another idea was just calling Game.Run() (o a new Thread) and emit an event in Game sending the RenderTarget2D.
I have found the following problems:
If I call Game.Run() I have no way to hide the game's window (which appears black, because I'm redirecting the render)
Game.Run() must be called on a different thread because it starts a new event loop (it calls Application methods), and then I'm painting in my user control from a different thread that it was created (bad, bad)
Locking a RenderTarget2D with Color, Vector4, and even Rgba32 doesn't appear to work for me (it says 'wrong structure size')
Any ideas?
Thanks in advance.