SFML事件不显示

SFML Events not displaying?

本文关键字:显示 事件 SFML      更新时间:2023-10-16

我的SFML程序中的Events功能不起作用。main()的一般结构如下所示。我想注册最后按下的键并相应地切换屏幕。我知道下面将要求键保持按下,但即使这样做作为测试也不起作用。

我遗漏了什么语法?我在论坛上看到的教程和示例代码使它看起来像下面一样简单。我曾见过在按键上使用开关,但这似乎更多地用于电子游戏中的即时响应。

int main()
{
    srand (time(NULL));
    sf::RenderWindow window(sf::VideoMode(SIZE, SIZE), "Curling Party");
    window.setFramerateLimit(30);
   ...
    while(window.isOpen())
    {
        sf::Event event;
        window.pollEvent(event);
        if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Q)
        {
            window.close();
            break;
        }
        if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::R)
        {

            ...
            window.display();
        }

        if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::B)
        {
            ...
            window.display();
        }
        if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape)
        {
           ...
        }

        window.clear(sf::Color::White);
        window.draw(house[0]);
        window.draw(house[1]);
        window.draw(house[2]);
        window.draw(house[3]);
        window.display();
    }
    return 0;
}

window.pollEvent(event)应该在while循环中,如文档所述:

人们经常犯的一个错误是忘记事件循环,仅仅是因为他们还不关心处理事件(他们使用实时输入代替)。如果没有事件循环,窗口将变得无响应。重要的是要注意,事件循环有两个角色:除了向用户提供事件之外,它还为窗口提供了处理其内部事件的机会,这是必需的,以便它可以对移动或调整用户操作做出反应。

虽然window.display()应该只在窗口循环结束时调用,但您不需要在每个键被按下后显示窗口。