简体   繁体   中英

More Pyglet - Supress mouse motion events?

In the same game as last time, I've ran into a new problem. When I move the mouse, FPS increases from around 60 to over 500. I know what you're thinking; it's not because of on_draw() getting fired each event (I think), since I already did override of pyglet.app.EventLoop.idle :

class EventLoop:
    def idle(self):
        pyglet.clock.tick(poll=True)
        return pyglet.clock.get_sleep_time(sleep_idle=True)

pyglet.app.EventLoop = EventLoop()

Also I call flip() on the window in my drawing function. All the useless mouse motion events take up a lot of CPU, which is annoying. What can I do about it?

Edit

I added window.invalid = False to my drawing function and window.invalid = True to my update function, this seems to reduce CPU usage with other mouse actions.

Edit 2

The drawing function is a typical on_draw() function.

Edit 3

After some more investigating, it seems that all those events don't take that much CPU as I thought they would. Still it would be good to know if this is the way Pyglet is supposed to act, or if it's something that should be avoided.

All you should be doing on mouse events is updating your apps' model of the input control state and using that in the next regular scheduled update and redisplay of the world model (which is presumably much more complex with physics and rendering and stuff).

ie just because mouse events come in at ~300fps doesn't mean you actually have to do all the stuff you want to do at 300fps.

It's been a while since I did any Pyglet, but the pattern I seemed to use was to subclass Pyglet's window.Window as MyGameWindow , then that registered event handlers like

   @self.event
    def on_mouse_motion(x,y,dx,dy):
        self.mouse_position=(x,y)
        self.mouse_buttons=0
        self.invalid = False

(also on_mouse_drag , on_mouse_press , on_mouse_release ). Hmmm... actually, I think that assignment to self.invalid might have been crucial for overriding Pyglet's default behaviour and defering any further updating/drawing until the next "clock tick".

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM