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