Reputation: 1150
I have two processes communicating over queues. One is altering the application state, the other one is running the pyglet event-loop. (just a side note, this is likely not a multiprocessing issue). I only want to draw when the state has changed or possibly with interpolated steps in between.
I'm confused about two things here.
This is the loop from the documentation:
while True:
pyglet.clock.tick()
for window in pyglet.app.windows:
window.switch_to()
window.dispatch_events()
window.dispatch_event('on_draw')
window.flip()
My on_draw() would look something like this:
def on_draw(self):
if self.check_if_state_changed():
self.draw_the_new_state()
I know that I could prevent the stuttering like this:
def on_draw(self):
if self.check_if_state_changed():
self.draw_the_new_state()
else:
self.draw_the_old_state_again()
But I don't want to do that, because re-drawing the old state requires time and performance is crucial for this application.
Upvotes: 2
Views: 482
Reputation: 1150
My solution to both problems is this, which will repeatedly call on_draw() as fast as possible:
class CustomLoop(app.EventLoop):
def idle(self):
dt = self.clock.update_time()
self.clock.call_scheduled_functions(dt)
# Redraw all windows
for window in app.windows:
window.switch_to()
window.dispatch_event('on_draw')
window.flip()
window._legacy_invalid = False
# no timout (sleep-time between idle()-calls)
return 0
app.event_loop = CustomLoop()
app.run() # locks the thread
I think their documentation should add a best-practice example for changing the event-loop.
Upvotes: 1