Merge branch 'master' of github.com:JanKlopper/pixelvloed

Conflicts:
	vloed.py
This commit is contained in:
Jan Klopper 2016-04-25 12:34:38 +02:00
commit 562ae26d7f
2 changed files with 28 additions and 25 deletions

View file

@ -17,7 +17,6 @@ import random
import time import time
class MaxSizeList(list): class MaxSizeList(list):
maxsize = 0
def __init__(self, maxcount=100): def __init__(self, maxcount=100):
self.maxsize = maxcount self.maxsize = maxcount
@ -28,7 +27,6 @@ class MaxSizeList(list):
if self.__len__() == self.maxsize: if self.__len__() == self.maxsize:
raise IndexError('max size reached') raise IndexError('max size reached')
def RGBPixel(x, y, r, g, b, a=None): # pylint: disable=C0103 def RGBPixel(x, y, r, g, b, a=None): # pylint: disable=C0103
"""Generates the packed data for a pixel""" """Generates the packed data for a pixel"""
if a: if a:
@ -58,7 +56,7 @@ def RandomFill(width=640, height=480):
message.append(pixel) message.append(pixel)
except IndexError: except IndexError:
yield ''.join(message) yield ''.join(message)
message[:] = [] message[2:] = []
message.append(pixel) message.append(pixel)
yield ''.join(message) yield ''.join(message)

View file

@ -39,7 +39,7 @@ class Canvas(object):
self.udp_port = UDP_PORT self.udp_port = UDP_PORT
self.width = width self.width = width
self.height = height self.height = height
self.canvas() self.canvas()
self.set_title() self.set_title()
self.queue = queue self.queue = queue
self.limit = 140 self.limit = 140
@ -98,31 +98,36 @@ class Canvas(object):
# indicat that nothing was done, and w can skip flipping the screen # indicat that nothing was done, and w can skip flipping the screen
return False return False
#access the pixel array and lock it #access the pixel array and lock it
self.pixels = pygame.surfarray.pixels2d(self.screen) self.pixels = pygame.surfarray.pixels2d(self.screen)
returntime = time.time() + (1.0 / self.fps) returntime = time.time() + (1.0 / self.fps)
# while we have stuff in the queue, and its not our next time to draw a # while we have stuff in the queue, and its not our next time to draw a
# frame, lets process packets from the queue # frame, lets process packets from the queue
while time.time() < returntime and not self.queue.empty(): while time.time() < returntime and not self.queue.empty():
data = self.queue.get() try:
preamble = struct.unpack_from("<?", data)[0] data = self.queue.get()
protocol = struct.unpack_from("<B", data, 1)[0] preamble = struct.unpack_from("<?", data)[0]
packetformat = ("<2H4B" if preamble else "<2H3B") protocol = struct.unpack_from("<B", data, 1)[0]
pixellength = (8 #xx,yy,r,g,b,a packetformat = ("<2H4B" if preamble else "<2H3B")
if preamble else pixellength = (8 #xx,yy,r,g,b,a
7 #xx,yy,r,g,b if preamble else
) 7 #xx,yy,r,g,b
pixelcount = min(((len(data)-1) / pixellength), )
self.limit) pixelcount = min(((len(data)-1) / pixellength),
if self.debug: self.limit)
print '%d pixels received, protocol V %d ' % (pixelcount, protocol)
for i in xrange(0, pixelcount):
pixel = struct.unpack_from(
packetformat,
data,
self.pixeloffset + (i*pixellength))
if self.debug: if self.debug:
print pixel print '%d pixels received, protocol V %d ' % (pixelcount, protocol)
self.Pixel(*pixel) for i in xrange(0, pixelcount):
pixel = struct.unpack_from(
packetformat,
data,
self.pixeloffset + (i*pixellength))
if self.debug:
print pixel
self.Pixel(*pixel)
except Exception as e:
if self.debug:
# All exceptions will be printed, but won't result in a crash.
print e
# indicate that we have been drawing stuff # indicate that we have been drawing stuff
return True return True