【问题标题】:SFML window does not update unless a key/mouse is pressed除非按下键/鼠标,否则 SFML 窗口不会更新
【发布时间】:2016-04-17 12:37:30
【问题描述】:

我正在使用 SMFL 2.3.2

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

问题是动画只有在我将鼠标不停地悬停在窗口上或按住某个键时才会发生。

我认为这是由于某些视频卡设置/驱动程序造成的,因为它在我工作时使用的 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();
    }
}

热烈欢迎任何建议!

感谢您的关注。

【问题讨论】:

    标签: c++11 window sfml


    【解决方案1】:

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

    因此,当您将鼠标悬停在窗口上方时,会有一个可用的 mouseover 事件,window.pollEvent(event) 返回 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();
    }
    

    希望这会有所帮助!

    【讨论】:

      猜你喜欢
      • 2019-12-21
      • 1970-01-01
      相关资源
      最近更新 更多