gpt4 book ai didi

c++ - SFML:在 pollEvent 循环之前调用 waitEvent

转载 作者:行者123 更新时间:2023-11-28 05:07:03 25 4
gpt4 key购买 nike

这是我在最近的 SFML 2.4.2 中的代码

#include <SFML/Graphics.hpp>
#include <iostream>

int main() {
sf::RenderWindow window(sf::VideoMode(800, 600), "My First SFML Game", sf::Style::Titlebar | sf::Style::Close);

std::cout << "Press a key to continue" << std::endl;
int i = 0;
sf::Event event;

while(window.isOpen()) {

while(window.pollEvent(event)) {
std::cout << "Polling event" << std::endl;
if(event.type == sf::Event::Closed) {

window.close();
}
}

if(window.waitEvent(event)) {
if(event.type == sf::Event::Closed)std::cout << "Event Activated" << i++ << std::endl;
}
}
}

Output当我单击关闭按钮时,您看到的所有事件 Activated 输出都被打印出来,当我移入或移出窗口时,所有轮询事件都被打印出来。即使我在窗口中单击,它也没有打印 Polled Event!我的问题是,pollEvents,为什么当您首先(显然)获得关闭事件时,您没有首先被调用并关闭窗口,因为您是第一个被写入的?为什么首先调用等待事件方法并弹出队列是没有意义的。请帮忙。

最佳答案

如果没有事件,pollEvent 将不会返回 true,然后它不会进入您的循环。但是 waitEvent 会停止程序,直到收到一个事件。

阅读the documentation of SFML Events :

sf::Window::waitEvent - This function is blocking: if there's no pending event then it will wait until an event is received.
sf::Window::pollEvent - This function is not blocking: if there's no pending event then it will return false and leave event unmodified.

关于c++ - SFML:在 pollEvent 循环之前调用 waitEvent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44457613/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com