SFML 事件不显示?
SFML Events not displaying?
我的 SFML 程序中的事件功能不起作用。 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 循环中,因为 documentation says:
A mistake that people often make is to forget the event loop, simply because they don't yet care about handling events (they use real-time inputs instead). Without an event loop, the window will become unresponsive. It is important to note that the event loop has two roles: in addition to providing events to the user, it gives the window a chance to process its internal events too, which is required so that it can react to move or resize user actions.
应该只在 window 循环结束时调用 window.display()
,不需要在按下每个键后显示 window。
我的 SFML 程序中的事件功能不起作用。 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 循环中,因为 documentation says:
A mistake that people often make is to forget the event loop, simply because they don't yet care about handling events (they use real-time inputs instead). Without an event loop, the window will become unresponsive. It is important to note that the event loop has two roles: in addition to providing events to the user, it gives the window a chance to process its internal events too, which is required so that it can react to move or resize user actions.
应该只在 window 循环结束时调用 window.display()
,不需要在按下每个键后显示 window。