diff --git a/vloed.py b/vloed.py index 60cc290..5c4e07f 100755 --- a/vloed.py +++ b/vloed.py @@ -35,7 +35,7 @@ class Canvas(object): self.screen = None self.udp_ip = UDP_IP self.udp_port = UDP_PORT - self.canvas() + self.canvas() self.set_title() self.queue = queue @@ -60,32 +60,41 @@ class Canvas(object): def Pixel(self, x, y, r, g, b, a=255): # pylint: disable=C0103 """Print a pixel to the screen""" - self.screen.set_at((x, y), (r, g, b, a)) + self.pixels[x][y] = (r, g, b) def CanvasUpdate(self): """Updates the screen according to self.fps""" lasttime = time.time() changed = False while True: - nextdraw = time.time() + (1 / self.fps) - changed = self.Draw(nextdraw) or changed + changed = self.Draw() or changed if time.time() - lasttime >= 1 / self.fps: + del(self.pixels) # release the lock on these pixels so we can flip pygame.display.flip() changed = False lasttime = time.time() + else: + time.sleep(1 / self.fps) - def Draw(self, returntime): + def Draw(self): """Draws pixels specified in the received packages in the queue""" if self.queue.empty(): + # indicat that nothing was done, and w can skip flipping the screen return False + #access the pixel array and lock it + self.pixels = pygame.surfarray.pixels2d(self.screen) + returntime = time.time() + (1 / self.fps) + # while we have stuff in the queue, and its not our next time to draw a + # frame, lets process packets from the queue while not self.queue.empty() and time.time() < returntime: data = self.queue.get() preamble = struct.unpack_from("