SFML从入门到放弃(1) 窗口和交互

SFML从入门到放弃(1) 窗口和交互

创建一个新窗口:

sf::RenderWindow window(sf::VideoMode(500,500),"new window");

但是光创建一个窗口并不能显示

还要加一个循环

    while (window.isOpen()){
        sf::Event event;  //接受窗口事件
        while (window.pollEvent(event)){
            if (event.type == sf::Event::Closed){ // 检测关闭
                window.close();
            }
        }
    }

然后就能看到一个黑色的窗口了

Event是一个union 可以通过 event.type 来判断

具体可以参考官网

键盘鼠标交互:

鼠标的操作信息可以通过event来检测

void check_mouse(const sf::Event &event)
{
    if (event.type == sf::Event::MouseButtonPressed){ //检测鼠标 输出鼠标坐标
        if (event.mouseButton.button == sf::Mouse::Right){
            std::cout << event.mouseButton.x << std::endl;
            std::cout << event.mouseButton.y << std::endl;
        }
    }

    if (event.type == sf::Event::MouseButtonReleased){ //检测鼠标释放
        std::cout << "realease" << std::endl;
    }

}

键盘的话一种是类似于鼠标的方式通过event检测

另外一种就是直接检测当前键有没有按下

if (sf::Keyboard::isKeyPressed(sf::Keyboard::Up)){ //检测键盘信息 上键是否按下
    std::cout << "up
";
}

参考:https://www.sfml-dev.org/tutorials/2.5

by karl07

原文地址:https://www.cnblogs.com/karl07/p/10285692.html