简体   繁体   English

在Windows窗体用户控件中嵌入XNA游戏

[英]Embeding a XNA Game in a Windows Form User Control

I have a simple XNA 4.0 Game written. 我有一个简单的XNA 4.0游戏。 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. 我想制作一个Windows窗体用户控件来渲染这个游戏,并提供必要的交互反馈(键盘和鼠标)回XNA。

I have tried the following: 我尝试过以下方法:

In XNA: 在XNA中:

  • Redirect XNA's Game.GraphicsDevice to a RenderTarget2D. 将XNA的Game.GraphicsDevice重定向到RenderTarget2D。

  • Emit an event sending an object with KeyboardState and MouseState to be filled in by Windows Forms, at the beginning of Update() 发送一个事件,发送一个具有KeyboardState和MouseState的对象,由Windows Forms填写,在Update()的开头

In WinForms: 在WinForms中:

  • Capture the event, filling in KeyboardState and MouseState with data obtained by the usual keyboard and mouse events in Windows Forms. 捕获事件,使用Windows窗体中常用的键盘和鼠标事件获取的数据填充KeyboardState和MouseState。

  • On the OnPaint, call Game.RunOnFrame() 在OnPaint上,调用Game.RunOnFrame()

  • Get the RenderTarget2D from the game (as a texture). 从游戏中获取RenderTarget2D(作为纹理)。

  • 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() (oa new Thread) and emit an event in Game sending the RenderTarget2D. 另一个想法是调用Game.Run()(新的线程)并在游戏中发送一个发送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()我无法隐藏游戏的窗口(显示为黑色,因为我正在重定向渲染)

  • 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) Game.Run()必须在不同的线程上调用,因为它启动一个新的事件循环(它调用Application方法),然后我在我的用户控件中绘制它创建的不同线程(坏,坏)

  • Locking a RenderTarget2D with Color, Vector4, and even Rgba32 doesn't appear to work for me (it says 'wrong structure size') 使用Color,Vector4甚至Rgba32锁定RenderTarget2D似乎对我不起作用(它表示“错误的结构大小”)

Any ideas? 有任何想法吗?

Thanks in advance. 提前致谢。

There is a great code sample on xna's site about embedding xna in a winform. 在xna的网站上有一个很好的代码示例,关于在winform中嵌入xna。 It takes you through all the steps. 它将指导您完成所有步骤。 I make games for xbox, but I use this for the level editor we've made for our engine. 我为xbox制作游戏,但我将它用于我们为引擎制作的关卡编辑器。

http://xbox.create.msdn.com/en-US/education/catalog/sample/winforms_series_1 http://xbox.create.msdn.com/en-US/education/catalog/sample/winforms_series_1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM