0

I am making 3D simulations with C# XNA, I use image\text files to load data.

I want to provide user the option to select a file which then my simulation will load and use. It's not possible with C# XNA.

I was able to embed the 3D XNA App in a WinForm using this tutorial, which worked for me but the problem is after that interaction based on keyboard is not smooth at all. It jut does not respond like a game any more.

How can I achieve file browsing and other things like buttons and and all with in a game?

I could have a logic to monitor the user input and use it to provide functionality like file browsing with in the game but that is too much trouble. Is there an easy was of achieving this. If not with XNA then may be some other way like DirectX

4

1 回答 1

2

在常规 XNA 中,Game该类定期调用您的DrawUpdate函数(通常每秒 60 帧)。

XNA WinForms 示例的工作方式略有不同。没有更新/绘制循环。相反,你得到的只是一个Draw函数。并且仅在控件需要重绘时调用。这意味着:

  1. Keyboard.GetState()如果这是您进行键盘输入的方式,您将没有机会定期调用。

  2. 即使您使用的是事件驱动(非 XNA)输入,您的窗口也不会定期重绘,因此在 XNA 控件最终重绘很久以后,您可能实际上看不到输入的结果。

幸运的是,有一种方法可以强制控件快速重绘。查看第二个WinForms 示例。你会发现它是动画的(换句话说:它经常更新)。它是如何做到的?看看这段代码ModelViewerControl.cs

protected override void Initialize()
{
    // Start the animation timer.
    timer = Stopwatch.StartNew();

    // Hook the idle event to constantly redraw our animation.
    Application.Idle += delegate { Invalidate(); };
}

它使用 aStopwatch来跟踪已经过去了多少时间(因为没有Game课程可以为您做这件事)。并且它对Application.Idle事件无效 - 换句话说,只要它不忙于处理某些东西,它就会要求重绘。

(当然,这已经在第一个 WinForms 示例的文档中进行了解释。在底部的“动画”下。)

替代奖励答案:没有什么可以阻止您OpenFileDialog在常规 XNA 游戏中使用 WinForms。只需添加对 的引用System.Windows.Forms,创建对象的实例,在其上设置适当的属性,然后调用ShowDialog().

于 2011-07-05T13:48:54.860 回答