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\n";
}

 

 

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

 

by karl07


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM