窗口没有响应 SFML

问题描述 投票:0回答:2

我刚刚进入 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。

c++ sfml
2个回答
1
投票

你打错字了!
不应该有“;”在第一个 while() 语句的末尾。


0
投票

您有两个问题:

  1. 第9行有错字
  • 不应该有“;”在第 9 行末尾
  1. 记得调用OpenGL渲染函数
  • window.clear() & window.display()

以下是如何使用它们:

#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;
}
© www.soinside.com 2019 - 2024. All rights reserved.