2014-12-05 3 views
2

이벤트를 수신하는 것 외에는 마우스의 위치를 ​​얻는 방법을 전혀 이해하지 못하지만 이벤트 큐가 비어있는 상황에서는 어떻게 이루어 집니까?PySDL2를 사용하여 마우스 위치를 얻는 방법은 무엇입니까?

pysdl for pygamers에 대한 문서는 sdl2.mouse.SDL_GetMouseState() (doc here)를 사용하여 제안하지만,이 기능은 actially 는 x, 당신에 대해 물어보고 싶은 커서의 y 좌표를 필요로한다. 한편 sdl2.mouse.SDL_GetCursor()을 호출하면 커서 객체가 반환되지만 좌표를 가져올 방법이 없습니다 (즉, C 객체를 래핑하는 것만 큼 .__dict__ 속성이 비어 있음).

나는 생각할 수있는 모든 것을 시도해 왔지만 이전에는 C로 프로그래밍 한 적이 없었습니다. 내가 생산하기 위해 노력하고있어 간단한 래퍼 함수는 다음과 같습니다

def mouse_pos(self): 
      # ideally, just return <some.path.to.mouse_x_y> 
      event_queue = sdl2.SDL_PumpEvents() 
      state = sdl2.mouse.SDL_GetMouseState(None, None) # just returns 0, so I tried the next few lines 
      print state 
      for event in event_queue: 
       if event.type == sdl2.SDL_MOUSEMOTION: 
      # this works, except if the mouse hasn't moved yet, in which case it's none   
      return [event.x, event.y] 

답변

4

SDL_GetMouseState()가 SDL2 C 함수의 래퍼입니다. 따라서 ctypes를 사용하여 값을 검색해야합니다. 원래 SDL2 함수는 커서 위치를 저장하기 위해 두 개의 포인터 (x와 y)를받습니다.

코드 조각 아래 당신을 위해 옳은 일을 할 것입니다 :

import ctypes 
... 
x, y = ctypes.c_int(0), ctypes.c_int(0) # Create two ctypes values 
# Pass x and y as references (pointers) to SDL_GetMouseState() 
buttonstate = sdl2.mouse.SDL_GetMouseState(ctypes.byref(x), ctypes.byref(y)) 
# Print x and y as "native" ctypes values 
print(x, y) 
# Print x and y as Python values 
print(x.value, y.value) 

`

관련 문제