如何XNA定时工作? [英] How does XNA timing work?

查看:140
本文介绍了如何XNA定时工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何XNA保持一致和准确的60帧的帧速率?此外,它如何不保持在100%盯住CPU这样的精确计时?

How does XNA maintain a consistent and precise 60 FPS frame rate? Additionally, how does it maintain such precise timing without pegging the CPU at 100%?

推荐答案

我不知道具体怎么XNA确实,但用OpenGL玩弄当几年前我使用一些非常简单的代码来完成同样的事情。

I don't know specifically how XNA does it but when playing around with OpenGL a few years ago I accomplished the same thing using some very simple code.

在它的核心我认为XNA具有某种渲染环,它可以或可以不与一个标准甚至处理循环集成但例如起见让我们假设它不是。在这种情况下,你可以把它写一些像这样的事情

at the core of it i assume XNA has some sort of rendering loop, it may or may not be integrated with a standard even processing loop but for the sake of example lets assume it isn't. in this case you could write it some thing like this.

TimeSpan FrameInterval =  TimeSpan.FromMillis(1000.0/60.0);
DateTime PrevFrameTime = DateTime.MinValue;
while(true)
{
    DateTime CurrentFrameTime = DateTime.Now;
    TimeSpan diff = DateTime.Now - PrevFrameTime;
    if(diff < FrameInterval)
    {
        DrawScene();
        PrevFrameTime = CurrentFrameTime;
    }
    else
    {
        Thread.Sleep(FrameInterval - diff);
    }
}

在现实中,你可能会使用类似Environment.Ticks而不是创建DateTime对象(它会更准确),但我认为这说明了这一点。这应该只调用drawScene函数第二,和的线程将被睡觉,所以它不会产生任何CPU时间,其余时间约60倍。

In reality you would probably use something like Environment.Ticks instead of DateTimes (it would be more accurate), but i think that this illustrates the point. This should only call drawScene about 60 times a second, and the rest of the time the thread will be sleeping so it will not incur any CPU time.

这篇关于如何XNA定时工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆