除非按下 key/mouse,否则 SFML window 不会更新

SFML window does not update unless a key/mouse is pressed

我正在使用 SMFL 2.3.2

我的代码绘制了一个正方形,并添加了一个 Rotate(int degrees) 函数,该函数在每次游戏循环执行时旋转正方形。

问题是只有当我将鼠标不停地悬停在 window 上或按住某个键时才会出现动画。

我认为这是由于某些视频卡引起的 settings/drivers 因为它在我工作时使用的 PC 上工作正常。

我的笔记本电脑运行 INTEL HD Graphics 4000 显卡。

下面是我使用的代码:

#include "SFML\Graphics.hpp"
#include <iostream>
#include <string>

using namespace std;

int main(){

    //initialize window object
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML");

    sf::CircleShape polygon(50,4);
    polygon.rotate(45);
    polygon.move(sf::Vector2f(200,200));

    //as long as we haven't closed the window
    while (window.isOpen())
    {
        sf::Event event;

        //check for events
        while (window.pollEvent(event))
        {           
            switch (event.type)
            {           

            case sf::Event::Closed:       //check for CLOSED event
                window.close();
                break;              

            }
            polygon.rotate(0.4);
        }

        window.clear();
        window.draw(polygon);
        window.display();
    }
}

热烈欢迎任何建议!

感谢您的关注。

上面代码的问题是它只会在事件队列中有事件时才旋转多边形。

因此,当您将鼠标悬停在 window 上方时,有一个可用的鼠标悬停事件 window.pollEvent(event) returns true 和 polygon.rotate(0.4) 被调用。

参考SFML doc了解详情

如果您将 polygon.rotate(0.4) 移出 while (window.pollEvent(event)),我想您将获得您想要的行为。

// code snip from above
while (window.isOpen())
{
    sf::Event event;

    //check for events
    while (window.pollEvent(event))
    {           
        switch (event.type)
        {           

        case sf::Event::Closed:       //check for CLOSED event
            window.close();
            break;              

        }
        // polygon.rotate(0.4);  Moved this out of the while loop
    }

    polygon.rotate(0.4);   // rotate in each game loop

    window.clear();
    window.draw(polygon);
    window.display();
}

希望对您有所帮助!