[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]
Re: [pygame] Speed of mouse position capture
On 5/15/06, Brian Bull <Brian.Bull@xxxxxxxxxxxxxxxxxxxxxxxxxxxxx> wrote:
> The only thing I found that could help was to call one of the
> event handling functions (pygame.event.pump works) because
> they would call a windows message handling func which would
> make windows keep another mouse position, but you'd have to
> have a good way to call it a bunch of times during a frame.
Oh. Could you please post an example of this? Wd be much appreciated.
Well, I made a contrived example of how event functions are required
in order to get mouse messages.
I modified the code Lenard posted so that if you press "p" it will
sleep for half a second. If you do that while drawing, you can see
that you don't get the mouse messages from during the pause (on
windows anyways)
If you press "e" however, it will sleep for half a second but call
event.pump frequently during that time period. If you do THAT while
drawing, then you will see you can get the mouse messages during the
sleep time.
...As far as the whole call something in a different thread thing, as
far as I can tell, you can only get the event loop pumping in the
thread that created your window. So if you want to get lots of mouse
messages, you need to call event messages frequently in your main
thread (again, on windows)
import pygame
from pygame.locals import *
import time
cont = True
last_posn = None
dirty_rects = []
screen = pygame.display.set_mode((500, 400))
screen.fill(Color('white'))
pygame.display.flip()
blue = Color('blue')
while cont:
event = pygame.event.wait()
while event.type != NOEVENT:
event_type = event.type
if event_type == QUIT:
cont = False
break
if event_type == KEYDOWN:
if event.key == K_q or event.key == K_ESCAPE:
cont = False
break
elif event.key == K_p:
time.sleep(.50)
elif event.key == K_e:
for i in xrange(50):
time.sleep(.01)
pygame.event.pump()
elif event_type == MOUSEBUTTONDOWN:
last_posn = event.pos
elif event_type == MOUSEBUTTONUP:
last_posn = None
elif event_type == MOUSEMOTION:
if last_posn is not None:
dirty_rects.append(pygame.draw.line(screen, blue, last_posn, event.pos, 1))
last_posn = event.pos
event = pygame.event.poll()
if dirty_rects:
pygame.display.update(dirty_rects)
dirty_rects = []