C++ SFML 文本在绘制时闪烁

C++ SFML Text flickers when drawn

我有一个 SFML RenderWindow,当它关闭时,它会显示一条确认消息。我已经 运行 多次遇到同样的问题:调用 Event::Closed 时会绘制确认消息(sf::Text),但它仅在调用事件时保留;我想直到关闭按钮的点击被注册(调用关闭事件);瞬间消失(这与 C++ 的速度有关吗?)。我该如何解决这个问题?文本应在绘制后显示,并且不应在调用关闭事件后消失。

这是代码(在 main.cpp 中):

while (app.isOpen())
    {

        // Process events
        sf::Event event;
        while (app.pollEvent(event))
        {

            // Close window : exit
            if (event.type == sf::Event::Closed)
            {
                app.clear();
                Text close("Are you sure you want to close this?", Arial);
                app.draw(close);
                close.setCharacterSize(40);
                close.setPosition(300, 300);
                app.display();
            }

        }

        // Clear screen
        app.clear();

        // Draw the sprite
        app.draw(sprite);
        app.draw(text);
        text.setPosition(500, 500);

        // Update the window
        app.display();
    }

解决方法给我一个错误:
我做了一个函数来完成这项工作,但它给了我这个错误:

error: use of deleted function 'sf::RenderWindow::RenderWindow(const sf::RenderWindow&)'.

Here 是代码。我做错了什么?

保留一个带有游戏状态的显式变量,并根据该变量更改游戏代码:

enum class GameState { Playing, Closing };
GameState phase = GameState::Playing;
while (app.isOpen()) {

    // Process events
    sf::Event event;
    while (app.pollEvent(event)) {

        // Close window : exit
        if (event.type == sf::Event::Closed) {
            phase = GameState::Closing;
        }

    }

    // Clear screen
    app.clear();

    if (phase == GameState::Closing) {
        Text close("Are you sure you want to close this?", Arial);
        close.setCharacterSize(40);
        close.setPosition(300, 300);
        app.draw(close);
    } else if (phase == GameState::Playing) {
        app.draw(sprite);
        app.draw(text);
        text.setPosition(500, 500);
    }

    // Update the window
    app.display();
}