开发者

What's the best way to drive an animation?

开发者 https://www.devze.com 2023-01-25 11:39 出处:网络
I have a form with a panel in which I want to display an animated 3D scene using XNA. What\'s the best way to have this panel continuously repainted?

I have a form with a panel in which I want to display an animated 3D scene using XNA. What's the best way to have this panel continuously repainted?

I'm currently thinking about adding a System.Windows.Forms.Timer to the form and adding an event handler to Timer.Tick which updates my 3D scene and 开发者_运维知识库calls Invalidate on the panel.

Is there a better way to do this? The rendering isn't critical, I don't want the rest of the form to become inresponsive, but on the other hand if there's time available I would like the rendering to happen at the refresh rate of the monitor.


Yes, a Timer would work. Here's a clever trick I saw used in a Microsoft example that showed how to use XNA in a Winforms project. It uses the Application.Idle event:

public partial class Form1 : Form {
    public Form1() {
        InitializeComponent();
        pictureBox1.Paint += new PaintEventHandler(pictureBox1_Paint);
        Application.Idle += new EventHandler(Application_Idle);
        this.FormClosed += delegate { Application.Idle -= Application_Idle; };
    }

    private void Application_Idle(object sender, EventArgs e) { 
        pictureBox1.Invalidate(); 
    }

    int cnt;
    void pictureBox1_Paint(object sender, PaintEventArgs e) {
        cnt++;
        e.Graphics.DrawString(cnt.ToString(), this.Font, Brushes.Black, 0, 0);
    }
}

I used a PictureBox instead of a Panel to hide the flicker, PB has the DoubleBuffered property turned on. That isn't very relevant to XNA though. Using vsync isn't going to work afaik. You probably ought to check out that example, the 'spinning triangle' was completely smooth without any tearing when I tried it. This thread talks about it.

0

精彩评论

暂无评论...
验证码 换一张
取 消