我是SFML的新手,并且一直在观看将所有内容都放在一个主要功能中的教程。在制作自己的程序时,我尝试将其拆分为多个功能,但是它无法正常运行,任何人都可以解释其工作原理:
#include <SFML/Graphics.hpp>
#include <iostream>
int main()
{
sf::RenderWindow window(sf::VideoMode(512, 512), "window", sf::Style::Resize | sf::Style::Close);
while (window.isOpen())
{
sf::Event evnt;
while (window.pollEvent(evnt))
{
if (evnt.type == evnt.Closed)
{
window.close();
}
}
window.clear();
window.display();
}
return 0;
}
这不是:
#include <SFML/Graphics.hpp>
#include <iostream>
sf::RenderWindow window;
void setup()
{
sf::RenderWindow window(sf::VideoMode(512, 512), "window", sf::Style::Resize | sf::Style::Close);
}
int main()
{
setup();
while (window.isOpen())
{
sf::Event evnt;
while (window.pollEvent(evnt))
{
if (evnt.type == evnt.Closed)
{
window.close();
}
}
window.clear();
window.display();
}
return 0;
}
它们都将编译并运行,但是在前者中,窗口将保持打开状态,而在后者中,窗口将不会打开。
答案 0 :(得分:0)
您在window
中声明的setup()
变量正在覆盖全局window
。宾语。请尝试以下操作:
void setup()
{
window.create(sf::VideoMode(512, 512), "window", sf::Style::Resize | sf::Style::Close);
}