Make some loops only run when needed, defer pygame updates if nothing changed since last update

Draw all packets in queue at once if we get the chance.
This commit is contained in:
Jan Klopper 2016-03-31 14:00:47 +02:00
parent 31b02f426b
commit 83d1c4b46c

View file

@ -65,20 +65,21 @@ class Canvas(object):
def CanvasUpdate(self): def CanvasUpdate(self):
"""Updates the screen according to self.fps""" """Updates the screen according to self.fps"""
lasttime = time.time() lasttime = time.time()
changed = False
while True: while True:
currenttime = time.time() currenttime = time.time()
self.Draw() changed = self.Draw() or changed
if currenttime - lasttime >= 1 / self.fps: if currenttime - lasttime >= 1 / self.fps:
pygame.display.flip() pygame.display.flip()
changed = False
lasttime = time.time() lasttime = time.time()
def Draw(self): def Draw(self):
"""Draws pixels specified in the received packages in the queue""" """Draws pixels specified in the received packages in the queue"""
try: if self.queue.empty():
return False
while not self.queue.empty():
data = self.queue.get() data = self.queue.get()
except:
raise
else:
preamble = struct.unpack_from("<?", data)[0] preamble = struct.unpack_from("<?", data)[0]
protocol = struct.unpack_from("<B", data, 1)[0] protocol = struct.unpack_from("<B", data, 1)[0]
packetformat = ("<2H4B" if preamble else "<2H3B") packetformat = ("<2H4B" if preamble else "<2H3B")
@ -96,6 +97,7 @@ class Canvas(object):
if self.debug: if self.debug:
print pixel print pixel
self.Pixel(*pixel) self.Pixel(*pixel)
return True
class PixelVloed(DatagramServer): class PixelVloed(DatagramServer):
"""PixelVloed server class""" """PixelVloed server class"""