如何通过多个 class "Union variable" (sfml) 使用 pollevent
How to use a pollevent through out multiple class "Union variable" (sfml)
根据我对 sf::Event
工作原理的理解,只能有一个循环,因为它们都共享相同的内存 space 所以我的问题是我可以在我的整个过程中使用 sf::Event
其他 class 与:
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
sf::CircleShape shape(100.f);
shape.setFillColor(sf::Color::Green);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(shape);
window.display();
}
return 0;
}
那么我可以参考我的 sf::Event
或者如果我想与其他 classes
分享它是不正确的
class a{
void handleEvent(sf::Event& event){
if (event.type == sf::Event::KeyPressed)
{
if (event.key.code == sf::Keyboard::Escape)
{
std::cout << "the escape key was pressed" << std::endl;
std::cout << "control:" << event.key.control << std::endl;
std::cout << "alt:" << event.key.alt << std::endl;
std::cout << "shift:" << event.key.shift << std::endl;
std::cout << "system:" << event.key.system << std::endl;
}
}
}
};
sf::Event
被实现为不同事件数据的联合,这意味着当时只有一个数据结构处于活动状态,它取决于 type
字段的值,但是这是针对每个实例而不是每个类型的东西。
因此您可以安全地传递事件,如果我理解正确的话,您的代码在这种情况下没问题。
根据我对 sf::Event
工作原理的理解,只能有一个循环,因为它们都共享相同的内存 space 所以我的问题是我可以在我的整个过程中使用 sf::Event
其他 class 与:
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(200, 200), "SFML works!");
sf::CircleShape shape(100.f);
shape.setFillColor(sf::Color::Green);
while (window.isOpen())
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
window.close();
}
window.clear();
window.draw(shape);
window.display();
}
return 0;
}
那么我可以参考我的 sf::Event
或者如果我想与其他 classes
class a{
void handleEvent(sf::Event& event){
if (event.type == sf::Event::KeyPressed)
{
if (event.key.code == sf::Keyboard::Escape)
{
std::cout << "the escape key was pressed" << std::endl;
std::cout << "control:" << event.key.control << std::endl;
std::cout << "alt:" << event.key.alt << std::endl;
std::cout << "shift:" << event.key.shift << std::endl;
std::cout << "system:" << event.key.system << std::endl;
}
}
}
};
sf::Event
被实现为不同事件数据的联合,这意味着当时只有一个数据结构处于活动状态,它取决于 type
字段的值,但是这是针对每个实例而不是每个类型的东西。
因此您可以安全地传递事件,如果我理解正确的话,您的代码在这种情况下没问题。