python - How to get mouse position using PySDL2? -
do not understand @ how achieve getting mouse's position except listening events, in circumstance event queue empty, how achieved?
the documentation pysdl pygamers suggests using sdl2.mouse.sdl_getmousestate()
(doc here) function actially requires x, y coordinates of cursor want ask about. meanwhile, calling sdl2.mouse.sdl_getcursor()
returns cursor object, can find no way coordinates (ie. it's wrapping c object, has empty .__dict__
attribute).
i have been trying can think of i've never programmed in c before. simple wrapper function i'm trying produce just:
def mouse_pos(self): # ideally, return <some.path.to.mouse_x_y> event_queue = sdl2.sdl_pumpevents() state = sdl2.mouse.sdl_getmousestate(none, none) # returns 0, tried next few lines print state event in event_queue: if event.type == sdl2.sdl_mousemotion: # works, except if mouse hasn't moved yet, in case it's none return [event.x, event.y]
sdl_getmousestate()
wrapper of sdl2 c function. have use ctypes retrieve values it. original sdl2 function receives 2 pointers (x , y) store cursor position.
the snippet below right thing you:
import ctypes ... x, y = ctypes.c_int(0), ctypes.c_int(0) # create 2 ctypes values # pass x , y references (pointers) sdl_getmousestate() buttonstate = sdl2.mouse.sdl_getmousestate(ctypes.byref(x), ctypes.byref(y)) # print x , y "native" ctypes values print(x, y) # print x , y python values print(x.value, y.value)
`
Comments
Post a Comment