简体   繁体   English

SFML的精确事件计时

[英]Precision event timing with SFML

Question

If a user wanted to know the "precise" time a key was pressed for using an event polling loop within SFML, one possible method would be to query timestamp data which comes bundled with every sf::Event . 如果用户想知道在SFML中使用事件轮询循环所按下的“精确”时间,一种可能的方法是查询与每个sf::Event捆绑在一起的时间戳数据。

However I haven't found anything in the documentation to suggest such data exists within the sf::Event class. 但是,我没有在文档中找到任何建议以表明此类数据存在于sf::Event类中。

Question 1: Does this data exist within an sf::Event . 问题1:此数据是否存在于sf::Event I have a vague recollection that it did either in an earlier version of sfml or in another window toolkit perhaps? 我有一个模糊的回忆,可能是在sfml的早期版本中,还是在另一个window工具箱中?

Question 2: If not then it is possible to accurately measure how long a key was pressed for? 问题2:如果不是,那么可以准确测量按键按下了多长时间?

Question 3: Are there any other window toolkits which will allow this? 问题3:是否有其他允许使用此工具的窗口工具包?

Code

At the moment I am doing the following: 目前,我正在执行以下操作:

double time_pressed = 0.0;

sf::Clock clock;
clock.restart();

while(window.isOpen())
{

    // Timing control
    const double delay_time = 1.0 / 100.0;
    double current_delay_time = clock.restart().asSeconds();
    if(current_delay_time < delay_time)
    {
        sf::sleep(sf::seconds(delay_time - current_delay_time));
    }

    sf::Event event;
    while(window.pollEvent(event))
    {

        if(sf::Keyboard::isKeyPressed(sf::Keyboard::A))
        {
            time_pressed += current_delay_time; // !
        }
    }

    // Other code

    window.display();

}

Is there a better or improved way of doing this? 是否有更好或更完善的方法?

I am aware that this may be OS dependent, considering that Linux is not a RTOS and I believe that Windows is? 我知道这可能取决于操作系统,因为Linux不是RTOS,而我相信Windows是Windows? (?) Both are "Operating Systems" in the sense that they manage multiple tasks, so presumably the "most accurate theoretically possible method" extends from the OS itself logging the timestamps of keypresses? (?)两者都是从操作系统上管理多个任务的意义,因此,“最精确的理论上可行的方法”可能是从操作系统本身记录按键时间的扩展而来的吗? Could one access that data if it exists? 一个人可以访问该数据(如果存在)吗?

I use Linux, but this is actually for a Windows project - something I know little about. 我使用Linux,但这实际上是针对Windows项目的-我对此一无所知。

This solution needs a sf::Clock be instantiated for every key you want to measure, because a key can be pressed while you're already measuring another. 此解决方案需要为每个要测量的键实例化sf :: Clock,因为可以在已经测量另一个键的同时按下一个键。 So it's assumed here that you want to measure time for just a particular key. 因此,这里假设您只想测量特定键的时间。 Otherwise, you could implement a system which would push an sf::Clock in a container each time a new key is pressed while another one is already pressed, and delete this clock from the container after use. 否则,您可以实现一个系统,该系统将在每次按下一个新键的同时将sf :: Clock推入一个容器,而另一个键已经被按下,并在使用后从容器中删除此时钟。 You would need to save the position of the clock in the container somehow. 您将需要以某种方式将时钟的位置保存在容器中。

Here is a solution for the key K, arbitrarily : 这是密钥K的任意解决方案:

sf::Clock kKeyClock;
bool kKeyPressed = false;
sf::Time kKeyTime;

while (window.isOpen())
{
    sf::Event event;
    while (window.pollEvent(event))
    {
        if ( event.type == sf::Event::KeyReleased )
            if ( event.key.code == sf::Keyboard::K )
            {
                kKeyTime = kKeyClock.getElapsedTime();
                kKeyPressed = false;
            }
    }

    if (sf::Keyboard::isKeyPressed(sf::Keyboard::K))
    {
        if ( !kKeyPressed )
        {
            kKeyClock.restart();
            kKeyPressed = true;
        }
    }
}

Does this solution seem accurate enough, or may I have misunderstood the question ? 这个解决方案看起来足够准确,还是我可能误解了这个问题?

I think sf::Event doesn't provide time information, but you could be sure if you look in the doc. 我认为sf :: Event不提供时间信息,但是可以确定是否查看文档。 I don't know for other window library. 我不知道其他窗口库。

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

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