我刚刚进入 Sfml,并且正在关注 youtube 上的教程。我的问题是,由于某种原因,窗口没有响应。 这是我的代码:
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>
#include <iostream>
int main()
{
sf::RenderWindow window(sf::VideoMode(512, 512), "A* Algorithm", sf::Style::Close | sf::Style::Resize);
while (window.isOpen());
{
sf::Event event;
while (window.pollEvent(event))
{
if (event.type == sf::Event::Closed)
{
window.close();
}
}
}
return EXIT_SUCCESS;
}
当我下载 SFML 时,我还获得了一些使用 SFML 的程序示例,例如 pong。这些程序确实响应,而且如果我编译它们,它们仍然响应,而且我似乎找不到在那里打开窗口的方式和我打开窗口的方式之间的区别。 我的操作系统是 Fedora 36。
你打错字了!
不应该有“;”在第一个 while() 语句的末尾。
您有两个问题:
以下是如何使用它们:
#include <SFML/Graphics.hpp>
int main(int, char const**)
{
// Create the main window
sf::RenderWindow window(sf::VideoMode(512, 512), "A* Algorithm", sf::Style::Close | sf::Style::Resize);
// Declare a drawable
sf::RectangleShape rectangle;
rectangle.setSize(sf::Vector2f(64.f, 64.f));
rectangle.setPosition(sf::Vector2f(window.getSize().x/2.f - rectangle.getGlobalBounds().width/2.f, window.getSize().y/2.f - rectangle.getGlobalBounds().height/2.f));
// Start the game loop
while (window.isOpen())
{
// Process events
sf::Event event;
while (window.pollEvent(event))
{
// Close window: exit
if (event.type == sf::Event::Closed) {
window.close();
}
}
// Clear screen
window.clear();
// Draw stuff between function calls
window.draw(rectangle);
// Update the window
window.display();
}
return EXIT_SUCCESS;
}