티스토리 뷰

이 포스트에서 알 수 있는 것

  • sf::Keyboard
  • sf::Mouse
  • sf::Joystick

이벤트 로직을 구현했는데 원하는데로 작동되지 않아...

sf::Event::Keypressed 를 통해서 키보드 입력 이벤트를 받아서 플레이어 이동 기능을 구현하려고 하는데 특정 상황에서 나의 의도에 맞지 않는 현상이 나왔다. 

 

키보드 입력을 한번만 입력 받았을 때, 입력 이벤트는 빠르게 생성되는데  키보드 입력을 계속 누르고 있을 때 일정 시간이 지난 후, 이벤트가 지속적으로 생성된다. 

 

나는 모든 입력 반응이 빨랐으면 좋겠는데, sf::Event 를 사용해서는 한계가 있어 보였다.

 


이럴 때 사용하는게 바로 Global Input 

Global Input은 sf::Event 보다 빠르게 입력 상태를 받아낼 수 있는 (모듈?)클래스이다. 

Global Input은  3종류(키보드, 마우스, 조이스틱)의 클래스가 존재한다. 

 

Global Input 과 sf::Event 의 차이점 

이들의 분명한 차이점은 바로 '호출 시점' 이다. Global Input은 키보드, 마우스, 조이스틱이 주어진 입력이 입력되었는지 즉각 확인하고 결과를 반환하는 반면 sf::Event는 주어진 입력이 입력되고 입력된 과거의 상태를 봔환한다. 

 


sf::Keyboard 

if (sf::Keyboard::isKeyPressed(sf::Keyboard::Key::Left))
{
    // left key is pressed: move our character
    character.move({-1.f, 0.f});
}

if (sf::Keyboard::isKeyPressed(sf::Keyboard::Scan::Right))
{
    // right key is pressed: move our character
    character.move({1.f, 0.f});
}

 

sf::Mouse

마우스 버튼 입력

if (sf::Mouse::isButtonPressed(sf::Mouse::Button::Left))
{
    // left mouse button is pressed: shoot
    gun.fire();
}

현재 마우스 커서 위치 반환

// get the global mouse position (relative to the desktop)
sf::Vector2i globalPosition = sf::Mouse::getPosition();

// get the local mouse position (relative to a window)
sf::Vector2i localPosition = sf::Mouse::getPosition(window); // window is a sf::Window

마우스 커서 위치 설정 

// set the mouse position globally (relative to the desktop)
sf::Mouse::setPosition({10, 50});

// set the mouse position locally (relative to a window)
sf::Mouse::setPosition({10, 50}, window); // window is a sf::Window

sf::Joystick 

조이스틱 연결 확인 

if (sf::Joystick::isConnected(0))
{
    // joystick number 0 is connected
    ...
}

조이스틱 버튼 누름 카운트

// check how many buttons joystick number 0 has
unsigned int buttonCount = sf::Joystick::getButtonCount(0);

// check if joystick number 0 has a Z axis
bool hasZ = sf::Joystick::hasAxis(0, sf::Joystick::Axis::Z);

조이스틱 입력 축 현재 위치 가져오기

// is button 1 of joystick number 0 pressed?
if (sf::Joystick::isButtonPressed(0, 1))
{
    // yes: shoot!
    gun.fire();
}

// what's the current position of the X and Y axes of joystick number 0?
float x = sf::Joystick::getAxisPosition(0, sf::Joystick::Axis::X);
float y = sf::Joystick::getAxisPosition(0, sf::Joystick::Axis::Y);
character.move({x, y});

 

공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/05   »
1 2 3
4 5 6 7 8 9 10
11 12 13 14 15 16 17
18 19 20 21 22 23 24
25 26 27 28 29 30 31
글 보관함